🎥 How to use FFmpeg

FFmpeg is a wonderful tool that can take almost any form of audio or video and convert it to almost any other form. It can downsample, upsample, transcode media, extract clips and do many more treatments edit that may be needed.

This blog post is an FFmpeg primer and contains a number of “receipes” that I have found most useful, and I hope you find them useful too.

Installation

The quickest way to to install ffmpeg is through Homebrew

brew install ffmpeg

How to get information from a video file

The following command will help to retrieve meta-information on a video file:

ffmpeg - i first.mkv

How to crop a video file

FFmpeg provides a crop filter for this specific purpose:

ffmpeg -i first.mkv -filter:v "crop=out_w:out_h:x:y" second.mp4

The options are as follows:

  • out_w is the width of the output rectangle
  • out_h is the height of the output rectangle
  • x and y specify the top left corner of the output rectangle
  • second.mp4 is the output file

Say, we download a video from YouTube and we need to remove the small black bars on the left and right sides of the video. We know we need to take off about 10 pixels total; 5 on both the left and right sides. We can do so by using the following video filter:

ffmpeg -i .mp4 -vf "crop=in_w-10:in_h" cropped.mp4

The crop parameter to the -vf (video filter) parameter indicates what the resolution of the width and then height of the output video should be. We use the in_w and in_h macros to indicate the source width and height; then we subtract 10 from the width.

Trimming a video clip

How to take a clip from a video from 00:50:22 through to 00:52:22

ffmpeg -ss 00:50:22 -i mymovie.mkv -c copy -to 00:02:00 mymovie-clip.mp4

How to remove ads from a video and split the video into multiple parts

In the example below, the source file is first.mp4 and it will be split into two parts. The first ending at 4 minutes from the start and the second starting at 5 minutes to the end. Note, this is done quickly as the file is not re-encoded.

ffmpeg -i first.mp4 -t 04:00 -c copy part-1.mp4 -ss 05:00 -codec copy part-2.mp4

How to join two or more video files together

To combine two or more files together, you will need to create a text file and list the files that you want to join together. In the example below, the file is called combine.txt. Add the files in the text file as below.

file 'part1.mp4' 
file 'part2.mp4' 
file 'part3.mp4'
ffmpeg -f concat -i combine.txt -c copy newoutput.mp4

How to re-encode one video format into another

ffmpeg -i firstmovie.mkv secondmovie.mp4

The -i parameter is used to specify the input file to FFmpeg.

How to change one video format into another without re-encoding

ffmpeg -i firstmovie.mov -vcodec copy -acodec copy secondmovie.mp4

How to extract audio from a video file

ffmpeg -i first.mkv second.mp3

How to extract audio from a video file with a variable bit rate

ffmpeg -i input.mkv -codec:a libmp3lame -qscale:a 2 output.mp3

How to extract audio from a video file and encode into FLAC

ffmpeg -i audio.xxx -c:a flac audio.flac

How to convert a FLAC file into ALAC with no audio compression loss

ffmpeg -i audio.flac -vn -acodec alac audio.m4a

Codecs

FFmpeg supports litterally hundreds of parameters and options. Very often, FFmpeg infers the parameters from the context, for example the input or output format from the file extention and it also applies default values to unspecified parameters. Sometimes it is instead necessary to specify some important parameters to avoid errors or to optimize the encoding.

Let’s start with a selection of the most important, not codec related, parameters:

-formats print the list of supported file formats
-codecs print the list of supported codecs (E=encode,D=decode)
-i set the input file. Multiple -i switchs can be used
-f set video format (for the input if before of -i, for output otherwise)
-an ignore audio
-vn ignore video
-ar set audio rate (in Hz)
-ac set the number of channels
-ab set audio bitrate
-acodec choose audio codec or use “copy” to bypass audio encoding
-vcodec choose video codec or use “copy” to bypass video encoding
-r video fps. You can also use fractional values like 30000/1001 instead of 29.97
-s frame size (w x h, ie: 320x240)
-aspect set the aspect ratio i.e: 4:3 or 16:9
-sameq ffmpeg tries to keep the visual quality of the input
-t N encode only N seconds of video (you can use also the hh:mm:ss.ddd format)
-croptop, -cropleft, -cropright, -cropbottom crop input video frame on each side
-y automatic overwrite of the output file
-ss select the starting time in the source file
-vol change the volume of the audio
-g Gop size (distance between keyframes)
-b Video bitrate
-bt Video bitrate tolerance
-metadata add a key=value metadata

The syntax of some commands have changed. Commands like -b (is the bitrate related to audio or video?) have now a different syntax:

Use:

	-b:a instead of -ab to set audio bitrate
	-b:v instead of -b to set video bitrate
	-codec:a or -c:a instead of -acodec
	-codec:v or -c:v instead of -vcodec

Author: Jas Dhaliwal

Hi, I’m Jas! I’m a digital marketer, technologist, and storyteller. I’m passionate about exploring the intersection of business, technology, and humanity. I hope you found this post informative. You can also follow me as @Jas on Twitter