How do you convert an entire directory with ffmpeg?

前端 未结 26 2130
长发绾君心
长发绾君心 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:03

    Of course, now PowerShell has come along, specifically designed to make something exactly like this extremely easy.

    And, yes, PowerShell is also available on other operating systems other than just Windows, but it comes pre-installed on Windows, so this should be useful to everyone.

    First, you'll want to list all of the files within the current directory, so, we'll start off with:

    ls
    

    You can also use ls -Recurse if you want to recursively convert all files in subdirectories too.

    Then, we'll filter those down to only the type of file we want to convert - e.g. "avi".

    ls | Where { $_.Extension -eq ".avi" }
    

    After that, we'll pass that information to FFmpeg through a ForEach.

    For FFmpeg's input, we will use the FullName - that's the entire path to the file. And for FFmpeg's output we will use the Name - but replacing the .avi at the end with .mp3. So, it will look something like this:

    $_.Name.Replace(".avi", ".mp3")
    

    So, let's put all of that together and this is the result:

    ls | Where { $_.Extension -eq ".avi" } | ForEach { ffmpeg -i $_.FullName $_.Name.Replace(".avi", ".mp3") }
    

    That will convert all ".avi" files into ".mp3" files through FFmpeg, just replace the three things in quotes to decide what type of conversion you want, and feel free to add any other arguments to FFmpeg within the ForEach.

    You could take this a step further and add Remove-Item to the end to automatically delete the old files.

    If ffmpeg isn't in your path, and it's actually in the directory you're currently in, write ./ffmpeg there instead of just ffmpeg.

    Hope this helps anyone.

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

    A one-line bash script would be easy to do - replace *.avi with your filetype:

    for i in *.avi; do ffmpeg -i "$i" -qscale 0 "$(basename "$i" .avi)".mov  ; done
    
    0 讨论(0)
  • 2020-11-22 05:04
    for i in *.flac;
      do name=`echo "${i%.*}"`;
      echo $name;
      ffmpeg -i "${i}" -ab 320k -map_metadata 0 -id3v2_version 3 "${name}".mp3;
    done
    

    Batch process flac files into mp3 (safe for file names with spaces) using [1] [2]

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

    And on Windows:

    FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
    
    0 讨论(0)
  • 2020-11-22 05:07

    I needed all the videos to use the same codec for merging purposes
    so this conversion is mp4 to mp4
    it's in zsh but should easily be convertible to bash

    for S (*.mp4) { ffmpeg -i $S -c:v libx264 -r 30  new$S }
    
    0 讨论(0)
  • 2020-11-22 05:08

    If you have GNU parallel you could convert all .avi files below vid_dir to mp4 in parallel, using all except one of your CPU cores with

    find vid_dir -type f -name '*.avi' -not -empty -print0 |
        parallel -0 -j -1 ffmpeg -loglevel fatal -i {} {.}.mp4
    

    To convert from/to different formats, change '*.avi' or .mp4 as needed. GNU parallel is listed in most Linux distributions' repositories in a package which is usually called parallel.

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