FFMPEG Strings

FFMPEG is one the most widely used cross-platform solutions to record, convert and stream audio and video. Here I share some of my FFMPEG command strings that I’ve used over the years. These are the result of many hours of research and trial and error. 

Some applications that use FFMPEG are Google Chrome (Chromium), VLC and Handbrake, just to name a few. Usually developers compile their own version of FFMPEG from source to better serve their specific need but the FFMPEG group has some precompiled FFMPEG binaries that anyone can download. You can grab one of these here. I personally use FFMPEG to create high quality MP4 movies and to transcode flac audio files to aac. Since most of my transcoding includes converting audio I compiled my own FFMPEG binary and included the Fraunhofer FDK AAC codec library which is currently the highest-quality AAC encoder available with FFMPEG (more info here). All the examples given here use libfdk_aac to handle audio, from batch processing of files to using FFMPEG with custom scripts, let’s dig in.

Remuxing Files

The example below creates a mp4 from a mkv source while transcoding the audio stream to aac using libfdk_aac. The video stream gets copied over to the mp4 without alterations. No need to re-encode the video stream if the source video stream is already encoded using the h.264 codec.

ffmpeg -i input.mkv -c:v copy -c:a libfdk_aac -b:a 256k output.mp4

If the source file has already a h.264 video stream and a aac audio stream and you don’t want to re-encode them you could tell FFMPEG to just copy the video and audio streams over to the new file.

ffmpeg -i input.mkv -codec copy output.mp4

If we where to use one of the examples above to process many files, on the bash terminal you could do something like this:

find . -iname "*.mkv" -exec ffmpeg -i {} -c:v copy -c:a libfdk_aac -b:a 256k {}.mp4 \;

Bash will perform a search for all .mkv files in the current directory and will remux all of them one at a time.

Remuxing Files (Advanced)

In my particular case I have a 2.1 audio setup in my house and I do watch lots of movies using my Bose bluetooth headphones. The FFMPEG command string I actually use for remuxing movies is shown below. I specifically tell FFMPEG that I want a stereo audio output filtered using matrix encoding Dolby Pro Logic II. Dolby Pro Logic II transforms traditional stereo audio into 5.1-channel surround sound. In short, is surround sound for headphones.

ffmpeg -hide_banner -y -v quiet -stats -i input.mkv -metadata title="Some Movie Title" -c:v copy -c:a libfdk_aac -ac 2 -b:a 256k -af "aresample=matrix_encoding=dplii" output.mp4

On the previous example I also added other options I commonly use on my FFMPEG strings. The option -hide_banner tells FFMPEG to not show the compiled settings which is all that big paragraph you see every time you run a FFMPEG. The -y option tells FFMPEG to overwrite the file if it already exists, -v quiet tells FFMPEG that the verbose level should be set to quiet and -stats tells FFMPEG to show you the progress. This last option is useful if you are wrapping FFMPEG on a GUI and want an update to be displayed on your interface. The -metadata title=”Some Movie Title” will set the title metadata tag for the new file. Since I use Plex to manage my media files setting this tag will prevent Plex from using the file name as the title for my movie.

Transcoding Streams

A simple command string you could use to re-encode both the audio and video streams of a multimedia file is shown below followed by a more detailed version. In the first example we will encode the video stream using the libx264 codec but we’ll let FFMPEG guess the best profile for transcoding your video stream. The audio stream will be encoded using libfdk_aac with a bitrate of 256k. On the second example we will tell FFMPEG exactly the profile and preset we want for our video stream. I have specified a bitrate of 1200kbps for transcoding the video stream but you can change this to whatever bitrate you want, if you don’t specify any bitrate FFMPEG will match the bitrate of the source.

ffmpeg -i input.wmv -c:v libx264 -b:v 1200k -c:a libfdk_aac -b:a 256k output.mp4
ffmpeg -i input.wmv -c:v libx264 -profile:v main -level 4.0 -preset slow -pix_fmt yuv420p -b:v 1200k -ac 2 -c:a libfdk_aac -b:a 256k output.mp4

For more options for the h.264 encoder please read this guide. The options I included are the ones that worked best in my experience but there are many other tweaks you can add.

Embed Subtitle File

If you have a subtitle file (.srt, .ass, etc…)  file that you want to mux with your movie file you can use the command string below.

ffmpeg -i input.mp4 -i sub_file.srt -c copy -c:s mov_text -metadata:s:s:0 language=spa output.mp4

You can actually combine this with all the examples I’ve given you, you only need to include the .srt file in the input and use [-c:s mov_text -metadata:s:s:0 language=spa]. Keep in mind that the example is telling FFMPEG that the subtitle file is in spanish (language=spa).

Audio Files

The following examples focus only on audio files. There are two popular approaches to transcoding audio files; constant bitrate (CBR) and variable bitrate (VBR). VBR is very popular because the file sizes tend to be smaller, FFMPEG will adjust the bitrate to match the input while CBR will use a fix bitrate regardless of the input. For example, you may have an audio file that only needs a bitrate of 234kbps in order to be represented digitally. If you encode this file using a CBR of 256kbps, you are using more precision to represent that digital file and thus, wasting data that won’t make the audio file sound better. Although I use CBR 99% of the time I’ll show you how to do it both ways. Why I use CBR you say? Well I do listen to classical music and Jazz, you have so much information in there that the encoder might think is empty space but in reality is a soft passage that the string section is playing, with CBR I’m making sure I get everything.For more information read this guide.

In the example below I have a .flac input file that I want to transcode to aac. I always run into trouble with flac files that have album artwork embedded. The -vn option forces FFMPEG to skip artwork. When using VBR you target quality rather than bitrate. Specifying 5 tells FFMPEG to use the highest quality for our audio output.

ffmpeg -i input.flac -c:a libfdk_aac -vn -vbr 5 output.m4a

If you want to transcode the same input file using CBR, you only need to remove the -vbr 5 and specify a bitrate.

ffmpeg -i input.flac -c:a libfdk_aac -vn -b:a 256k output.m4a

If you want to save the album artwork of a media file you can do so with the command below.

ffmpeg -i input.flac -an -vcodec copy /path/cover.jpg

And of course to process multiple files you can do so with this command.

find . -iname "*.flac" -exec ffmpeg -i {} -c:a libfdk_aac -vn -b:a 256k {}.m4a \;

Merging Files
If you have two or more files that need to be concatenated (merged) and these files have the same streams (same codecs, same time base, etc.) you can use the string below to do so. You can use -c copy or you can specify the codecs to be used.

ffmpeg-hi -f concat -safe 0 -i <(for f in ./*.mp4; do echo "file '$PWD/$f'"; done) -c copy "output_file.mp4"

I uploaded my ffmpeg wrapper to github. If you want more example you can check that out here.

No Comments, Be The First!

Your email address will not be published.