Master Your Videos From The CLI With FFMPEG

Objective

Learn the basics of video conversion with FFMPEG.

Distributions

FFMPEG is available on most Linux distributions.

Requirements

A working Linux install with FFMPEG installed.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

Video formats can be a real pain. Some programs and devices only support a select few. Others take up loads of space on your hard drive. Even worse, the playback on poorly converted media is terrible.

FFMPEG puts the power in your hands and all from the command line. You can use FFMPEG to wrangle your video files into exactly the right file formats without sacrificing quality or dealing with clunky and incomplete GUI tools that have more dependencies than functionality.

This guide covers some of the most common uses and functions of FFMPEG in handling video, but there are certainly more. Plus, as a command line utility, FFMPEG is fully scriptable, so you can think of some of the possibilities.

Converting Between Video Formats

Probably, one of the most common things that you’ll want to do with FFMPEG is convert videos between formats. It’s also one of the simplest things that you can do.

$ ffmpeg -i input.mp4 output.mkv

The -i flag tells FFMPEG that the next thing it encounters will be input. You can pass and arbitrary amount of input to FFMPEG from a variety of sources including streams and device input.

When FFMPEG runs out of inputs, it looks for options. When it runs out of them too, it assumes that everything else is output. FFMPEG can output to files or even to a URL for streaming.

Retaining Quality

Sometimes, videos lose quality in conversion. You can tell FFMPEG to do everything possible to retain the original quality of the video being transcoded.

$ ffmpeg -i input.mp4 -sameq output.mkv

The -sameq flag stands for, “same quality.”

Using Targets

This feature is used mostly with DVDs. If you’re looking to create a DVD, you want to make sure that the file that you’re burning to it meets proper specifications. The -target flag lets you tell FFMPEG which specifications to use.

$ ffmpeg -i input.mkv -target dvd output.avi

You can also give FFMPEG a specific region.

$ ffmpeg -i input.mkv -target ntsc-dvd output.avi

Extracting Clips

It’s pretty common to want to cut clips out of a long video or film. FFMPEG makes that easy too. You need to specify where you want FFMPEG to start and how long you want it to run for. After that, tell it that it’s making a copy of the original. Take a look.

$ ffmpeg -i input.mkv -ss 00:10:30 -t 00:05:24 -c clip.mkv

The command above will start recording 10 minutes and 30 seconds into the input file and run for 5 minutes and 24 seconds. It’ll then copy that clip to a new file.

Take a look at another one.

$ ffmpeg -i movie.mkv -ss 00:42:00 -t 00:06:12 -c favorite_scene.mkv

In this case, FFMPEG will start at exactly 42 minutes and run for 6 minutes and 12 seconds. It’ll then make a copy of your favorite scene.

Formatting Video

You can use FFMPEG to better format your video. You can change the resolution, aspect ratio, and even crop the video. Check out an example.

$ ffmpeg -i input.mkv -aspect 16:9 -s 1920x1080 output.mkv

FFMPEG will format the video with a 16:9 aspect ratio at a resolution of 1920×1080. Don’t expect miracles when working with poor quality source material. FFMPEG can’t make it high resolution. It can only format it that way.

FFMPEG can also crop a video. Don’t confuse this with scaling it down from a higher resolution. It’ll actually cut out screen space.

$ ffmpeg -i input.mkv -cropbottom 200 output.mkv

So, the resulting video would be missing the bottom 200 pixels. It would also display at an irregular resolution because the other sides weren’t modified proportionally.

$ ffmpeg -i input.mkv -croptop 100 -cropbottom 100 -cropleft 50 -cropright 50 output.mkv

This example is purely theoretical, since there’s no source resolution or target, but you can see that each of the sides is clipped off. In reality, you’d probably use this tactic to scale down a video by eliminating parts of it. You’d try to retain proportions, though.

Closing Thoughts

By now, you should have a fairly good understanding of how to use FFMPEG to manipulate and convert video. FFMPEG is an incredibly powerful tool, and with sufficient exploration, you can uncover even more advanced features, and that says nothing of what can be done with scripting.

If you’re interested in more FFMPEG, check out our audio article to learn how to use FFMPEG to handle audio files.



Comments and Discussions
Linux Forum