A First Look at FFmpeg

Sean Sain
Sean Sain
Apr 26 · 4 min read

2020 — God… Where to start? These last few weeks have been anything but normal. An almost “Deja Vu”-esque effect, I’ve found myself reliving my pre-college days, pestered by mother’s never-ending demands, and dumbfounded by the sheer slowness of the ticking clock. Yet beneath the absurdity that it is Today, I’ve had the chance to immerse myself in a pastime, only available as a result of this quarantine: watching childhood memories — memories kept alive through albums and homemade DVDs. There’s truly nothing quite like it. The problem is, our Xbox is the only place I can play said DVDs, but soon, even that may be changing.

Being a programmer, I took to Google and looked for tools to convert our precious family memories into a more modern format. Looking online, all I saw were suspicious websites and sketchy downloads enticing me with free trials and high quality conversions. I did not have the patience to navigate websites riddled with ads or carefully download seemingly malicious software. There had to be a better way.

After doing some research, I found an open source tool called FFmpeg that “supports the most obscure ancient formats up to the cutting edge,” according to their website. I installed it through Homebrew and spent some time getting familiar with the software, while learning about video and audio files in the process.

Image for post
Image for post

DVDs are usually made up of VOB files in the video_ts directory that can be can be combined into one file using the following:

cat *.VOB > input.vob

We can then use learn about our file using the following:

ffmpeg -i input.vob
  • The -i (input) flag is where we feed in an input file that we want to learn about.

This command will show us the different streams (video, audio, subtitle, attachment, data) that make up the given input:

Input #0, mpeg, from ‘input.vob’:
Duration: 00:00:53.24, start: 0.476900, bitrate: 446736 kb/s
Stream #0:0[0x1bf]: Data: dvd_nav_packet
Stream #0:1[0x1e0]: Video: mpeg2video (Main), yuv420p(tv, top first), 352x576 [SAR 24:11 DAR 4:3], 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:2[0x80]: Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s

In this case, there is some navigation data, a video stream, and an audio stream. I want to convert it to an mp4 so that I can easily save and watch on different devices.

ffmpeg -i input.vob -c copy output.mp4

This effectively just copies the contents of the file and changes the container. While I’m at it, I could just encode the streams to save space while maintaining quality.

ffmpeg -i input.vob -c:v libx264 -c:a aac output.mp4

Let’s break down the above command:

  • The -c stands for codec: “Codec is a portmanteau of coder-decoder
  • After the colon, we specify the type of stream we want to encode. In the first case, v for video; and the second case, a for audio.
  • After the codec flag, we specify the encoder, libx264 (H.264 video encoder) to encode the video; and aac (Advanced Audio Coding) to encode the audio.

In just a few minutes I had my DVD backed up onto my computer as an mp4 file that I could send on the family WhatsApp group to annoy all my relatives. There are many codecs and containers that are supported so be sure to choose one that fits your needs.

FFmpeg has a lot to offer; I also use it to create GIFs from screen recordings for demos I want to include in presentations. I use QuickTime to record my screen which creates an mov file. It is very easy to create a GIF from that using the following:

ffmpeg -i input.mov output.gif

However, this uses the entire screen recording. We probably want to highlight the important part of the video by selecting the parts of the recording that are most interesting:

ffmpeg -t 20 -ss 00:00:02 -i input.mov output.gif
  • The -t flag specifies that we want the output to be 20 seconds
  • The -ss flag is the starting position for the input video

This gives us more control over the length of our output. However, it still probably feels a bit choppy. The output is still using every frame from the selected part of the video causing the GIF to play slowly and use up a lot of space. To smooth it out, we can adjust the frame rate and speed of the output:

ffmpeg -i input.mov -r 24 -filter:v “setpts=0.5*PTS” output.gif
  • The -r flag sets the frame rate of the output to 24
  • The -filter flag takes in a filter and here we are passing in setpts which sets the precision timestamp of all the frames. Since we are halving the value for each frame, this will double the speed of the output.

I tend to increase the speed and reduce the frame rate to keep the GIF quick and smooth. I usually play with the frame rate and speed a bit until I am happy with the results. We can combine the time and position flags with the frame rate and filter flags creating a nice polished GIF that can light up any presentation. GIPHY also has a cool tutorial on converting between video and GIF.

FFmpeg has already helped me by enhancing my presentations and backing up my childhood memories. I will definitely use it for any future video needs I might have. I encourage you to explore and learn more about FFmpeg as it has so much to offer.

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store