How do you convert an entire directory with ffmpeg?

前端 未结 26 2011
长发绾君心
长发绾君心 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 04:56

    This will create mp4 video from all the jpg files from current directory.

    echo exec("ffmpeg -framerate 1/5 -i photo%d.jpg -r 25 -pix_fmt yuv420p output.mp4");
    
    0 讨论(0)
  • 2020-11-22 04:57

    Getting a bit like code golf here, but since nearly all the answers so far are bash (barring one lonely cmd one), here's a windows cross-platform command that uses powershell (because awesome):

    ls *.avi|%{ ffmpeg -i $_ <ffmpeg options here> $_.name.replace($_.extension, ".mp4")}
    

    You can change *.avi to whatever matches your source footage.

    0 讨论(0)
  • 2020-11-22 05:00

    For Windows:

    Here I'm Converting all the (.mp4) files to (.mp3) files.
    Just open cmd, goto the desired folder and type the command.

    Shortcut: (optional)
    1. Goto the folder where your (.mp4) files are present
    2. Press Shift and Left click and Choose "Open PowerShell Window Here"
    or "Open Command Prompt Window Here"
    3. Type "cmd" [NOTE: Skip this step if it directly opens cmd instead of PowerShell]
    4. Run the command

    for %i in (*.mp4) do ffmpeg -i "%i" "%~ni.mp3"
    

    If you want to put this into a batch file on Windows 10, you need to use %%i.

    0 讨论(0)
  • 2020-11-22 05:00

    I'm using this one-liner in linux to convert files (usually H265) into something I can play on Kodi without issues:

    for f in *.mkv; do ffmpeg -i "$f" -c:v libx264 -crf 28 -c:a aac -b:a 128k output.mkv; mv -f output.mkv "$f"; done
    

    This converts to a temporary file and then replaces the original so the names remain the same after conversion.

    0 讨论(0)
  • 2020-11-22 05:01

    I know this might be redundant but I use this script to batch convert files.

    old_extension=$1
    new_extension=$2
    
    for i in *."$old_extension";
      do ffmpeg -i "$i" "${i%.*}.$new_extension";
    done
    

    It takes 2 arguments to make it more flexible :

    1. the extension you want to convert from
    2. the new extension you want to convert to

    I create an alias for it but you can also use it manually like this:

    sh batch_convert.sh mkv mp4
    

    This would convert all the mkv files into mp4 files.

    As you can see it slightly more versatile. As long as ffmpeg can convert it you can specify any two extensions.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题