Bash 删除路径中的前导/

标签 bash loops variables ffmpeg

这个问题在这里已经有了答案:





While loop stops reading after the first line in Bash

(5 个回答)



Execute "ffmpeg" command in a loop [duplicate]

(3 个回答)


1年前关闭。




我有一个简单的脚本来调整图像的大小:

#!/usr/bin/bash

right_size='/home/scripts/images_correct_size'
user=$(whoami)

case $user in
    'sonarr')
        folder='/var/lib/sonarr/MediaCover'
        ;;
    'radarr')
        folder='/var/lib/radarr/MediaCover'
        ;;
    'jellyfin')
        folder='/var/lib/jellyfin/metadata'
        ;;
    *)
        echo "This user cannot run the script."
        ;;
esac

echo "Running script as $user."

find $folder -type f -name "*.jpg" | while IFS= read -r line
do
    echo "Current file: $line"
    # Grep has a match
    if grep -q $line $right_size; then
        :
    else
        size=$(ffprobe -hide_banner -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 $line)
        width=$(cut -d ',' -f 1 <<<$size)
        height=$(cut -d ',' -f 2 <<<$size)

        if [[ $width -gt 1024 ]] && [[ $width -ge $height ]]; then
            echo "File width too large, reducing."
            ffmpeg -hide_banner -y -i $line -vf scale=1024:-1 $line > /dev/null 2>&1
            echo $line >> $right_size
            continue
        elif [[ $height -gt 1024 ]] && [[ $height -ge $width ]]; then
            echo "File width too large, reducing."
            ffmpeg -hide_banner -y -i $line -vf scale=-1:1024 $line > /dev/null 2>&1
            echo $line >> $right_size
            continue
        else 
            echo "File is right, nothing to do."
            echo $line >> $right_size
        fi
    fi
done
当它运行时,ffmpeg被执行,下一行变量 line没有首字母/ ,使脚本在一次迭代中无用。在循环的下一次迭代中,一切都恢复正常。
这是输出的一部分,其中问题是可见的:
Current file: /var/lib/radarr/MediaCover/154/fanart-180.jpg
File is right, nothing to do.
Current file: /var/lib/radarr/MediaCover/154/fanart.jpg
File width too large, reducing.
Input #0, image2, from '/var/lib/radarr/MediaCover/154/fanart.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 78855 kb/s
    Stream #0:0: Video: mjpeg (Progressive), yuvj420p(pc, bt470bg/unknown/unknown), 3840x2160 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0x5603ac596680] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/var/lib/radarr/MediaCover/154/fanart.jpg':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: mjpeg, yuvj420p(pc), 1024x576 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.91.100 mjpeg
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
frame=    1 fps=0.0 q=7.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.754x    
video:38kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Current file: var/lib/radarr/MediaCover/154/poster-500.jpg
var/lib/radarr/MediaCover/154/poster-500.jpg: No such file or directory
File is right, nothing to do.
Current file: /var/lib/radarr/MediaCover/3/poster.jpg
为什么会这样?

最佳答案

ffmpeg根据 shellcheck.net 命令消耗一个字节.
使用 -nostdin 运行它选项解决了这个问题。

关于Bash 删除路径中的前导/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63205923/

相关文章:

c# - 如何迭代两个集合?

windows - 如何通过bat文件递归地将文件名从大写更改为小写

javascript - 在 JavaScript 中使用全局变量

linux - 如何通过ssh获取远程命令的退出代码

linux - 如果输出为十进制,则为整数

ruby - 我可以在 Ruby 的 Proc 中使用顶层中断来中断循环吗?

WHERE 子句中的 MySQL 变量

c - 为什么静态全局变量可以在其他文件中访问?

bash:join 和 comm 之间的区别

python - Bash 命令可以在 shell 中运行,但不能通过 python suprocess.run 运行