How do I programmatically convert FLV video files to MP4 using a shell script in OS X?

GeneQ picture GeneQ · Aug 20, 2010 · Viewed 8.7k times · Source

I want to batch convert a directory containing hundreds of FLV files so that each file has a MP4 equivalent. I'm trying to automate this process by writing a shell script and running it from the Terminal. How do I go about doing that? Most of the instructions available are for Linux using ffmpeg but I think OS X doesn't have it. Thanks.

Answer

Michael Aaron Safyan picture Michael Aaron Safyan · Aug 20, 2010

You can install ffmpeg via Homebrew or MacPorts. The commnd to install ffmpeg with Homebrew is brew install ffmpeg; similarly, the command to install ffmpeg with MacPorts is sudo port install ffmpeg. Once you've installed ffmpeg, here is a simple (and somewhat naive) script for converting the files. You may need to add more flags, depending on your desired options.

#! /bin/bash
function convert_all_to_mp4() {
  for file in *.flv ; do
    local bname=$(basename "$file" .flv)
    local mp4name="$bname.mp4"
    ffmpeg -i "$file" "$mp4name"
  done
}
convert_all_to_mp4

Just make whatever file you put the script above in executable (for example, chmod a+x path/to/convert_all_to_mp4.sh) and invoke it by its fully qualified path or add the directory containing it to the PATH environment varaible and invoke the script by the name you gave it.