如何通过命令行或批处理脚本与ffmpeg转换整个目录/文件夹?


当前回答

这将从当前目录中的所有jpg文件创建mp4视频。

echo exec("ffmpeg -framerate 1/5 -i photo%d.jpg -r 25 -pix_fmt yuv420p output.mp4");

其他回答

复制@HiDd3n,你也可以这样做,如果你想递归搜索文件目录,以防你有很多文件夹:

for /r %i in (*.webm) do "C:\Program Files\ffmpeg\bin\ffmpeg.exe" -i "%i" "%~ni.mp3"

在Windows上:

FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
for i in *.flac;
  do name=`echo "${i%.*}"`;
  echo $name;
  ffmpeg -i "${i}" -ab 320k -map_metadata 0 -id3v2_version 3 "${name}".mp3;
done

使用[1][2]批量处理flac文件到mp3(安全的文件名与空格)

下面的脚本在Windows上的Bash中工作得很好(所以它在Linux和Mac上也应该工作得很好)。它解决了我在其他解决方案中遇到的一些问题:

处理子文件夹中的文件 用目标扩展替换源扩展,而不仅仅是附加它 适用于名称中有多个空格和多个点的文件 (详见这个答案。) 可以运行时,目标文件存在,提示之前覆盖

ffmpeg-batch-convert.sh:

sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
    targetFile="${sourceFile%.*}.$targetExtension"
    ffmpeg -i "$sourceFile" "$targetFile"
done
unset IFS; set +f

示例调用:

$ sh ffmpeg-batch-convert.sh mp3

作为奖励,如果你想删除源文件,你可以像这样修改脚本:

sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
deleteSourceFile=$3 # "delete" or omitted
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
    targetFile="${sourceFile%.*}.$targetExtension"
    ffmpeg -i "$sourceFile" "$targetFile"
    if [ "$deleteSourceFile" == "delete" ]; then
        if [ -f "$targetFile" ]; then
            rm "$sourceFile"
        fi
    fi
done
unset IFS; set +f

示例调用:

$ sh ffmpeg-batch-convert.sh mp3

我需要所有的视频使用相同的编解码器合并的目的 这个转换是mp4到mp4 它在ZSH中,但应该很容易转换为bash

for S (*.mp4) { ffmpeg -i $S -c:v libx264 -r 30  new$S }