ffmpeg批量重新编码子文件夹

ffmpeg batch reencode with subfolders

我正在尝试将数百个视频重新编码为 X265,但许多目录的文件名中都有空格,有些文件也是如此。我查看了一堆脚本,正在努力寻找一个适用于空格和不同目录级别的脚本。

这个可以,只要没有子目录:

#!/bin/bash
for i in *.avi;
do 
    ffmpeg -i "$i" -c:v libx265 -c:a copy X265_"$i"
done

我一直在尝试使用这个 bash 脚本,但它因我猜测的空格而失败。

#!/bin/bash
inputdir=$PWD
outputdir=$PWD
while IFS= read -r file ; do
  video=`basename "$file"`
  dir=`dirname "$file"`
 ffmpeg -fflags +genpts -i "$file" -c:v libx265 -c:a copy "$dir"/X265_"$video"
done < <(find "$inputdir" -name "*.avi" | head -100)

在这个线程上,它看起来像是 windows 用户的一个很好的解决方案,但不是 linux。 FFMPEG - Batch convert subfolders

FOR /r %%i in (*.mp4) DO ffmpeg32 -i "%%~fi" -map 0:0 -map 0:1 -map 0:1 -c:v copy -c:a:0 aac -b:a 128k -ac 2 -strict -2 -cutoff 15000 -c:a:1 copy "%%~dpni(2)%%~xi"

如果您能指出适合 bash 的正确解决方案,我将不胜感激。

这是 find and xargs

的典型场景
find /path/to/basedir -name '*.avi' -print0 | xargs -0 convert.sh

其中 -print0-0 确保正确处理带空格的名称。

并且在 convert.sh 中,您有 for 循环,几乎与第一个脚本中的相同

#!/bin/bash

for i; do
    d=$(dirname "$i")
    b=$(basename "$i")
    ffmpeg -i "$i" -c:v libx265 -c:a copy "$d/X265_$b"
done

for i不带任何东西的意思和"for all arguments given"一样,都是xargs传过来的文件。

要在文件名前加上字符串,必须将名称拆分为目录和基础部分,然后再将其组合在一起。

使用 xargs + sh -c,也可以通过传递 -I 变量来使用 bourne 脚本替换——在本例中为 FILE——到 sh -c 作为位置参数用作 </code> 扩展:</p> <pre><code>find . -name '*.flac' -print0 | \ xargs -0 -I FILE \ sh -c 'ffmpeg -i "" -c:a libfdk_aac -vbr 3 "${1%.flac}.m4a"' -- FILE

在这里,我只是用 ${1%.flac} 砍掉 .flac 扩展名并添加 m4a。