How do you convert an entire directory with ffmpeg?

前端 未结 26 2134
长发绾君心
长发绾君心 2020-11-22 04:33

How do you convert an entire directory/folder with ffmpeg via command line or with a batch script?

26条回答
  •  时光说笑
    2020-11-22 05:01

    The following script works well for me in a Bash on Windows (so it should work just as well on Linux and Mac). It addresses some problems I have had with some other solutions:

    • Processes files in subfolders
    • Replaces the source extension with the target extension instead of just appending it
    • Works with files with multiple spaces and multiple dots in the name (See this answer for details.)
    • Can be run when the target file exists, prompting before overwriting

    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
    

    Example call:

    $ sh ffmpeg-batch-convert.sh mp3 wav

    As a bonus, if you want the source files deleted, you can modify the script like this:

    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
    

    Example call:

    $ sh ffmpeg-batch-convert.sh mp3 wav delete

提交回复
热议问题