zs3.me

FFMPEG Quick Reference

Revision 15
© 2021-2023 by Zack Smith. All rights reserved.

What is FFMPEG?

FFMPEG is the Swiss Army Knife of video conversion and alteration. It can't do everything, but it can do quite a lot.

It is very much a manually-operated utility and requires using various command line options to achieve the desired effects and the best video output quality.

FFMPEG is accompanied by ffplay which is a video player built on top of the same underlying code as ffmpeg.

To preview what ffmpeg's video filters, you can typically use ffplay with the same options.

Useful commands

Remove audio from a video

 ffmpeg -i input.mp4 -vcodec copy -an output.mp4

Increase audio volume of a video

 ffmpeg -i input.mp4 -filter:a "volume=1.6" output.mp4

Extract audio from a video

 ffmpeg -i input.mp4 output.wav

You can then convert WAV to MP3 with:

 lame -V0 output.wav

This generates output.mp3.

Replace audio track of a video file with another one

Don't specify an audio codec.

 ffmpeg -i video.mp4 -i audio.wav -vcodec copy -map 0:v -map 1:a output.mp4

Extract part of a video

The following extracts a segment starting at the 5 minute mark with a length of 1 minute:

 ffmpeg -i video.mp4 -ss 5:00 -t 1:00 -c copy output.mp4

Drop/skip every other frame (speed up video)

 ffmpeg -i video.mp4 -vf 'setpts=0.5*PTS' output.mp4

Crop a video

Specify the rectangle to crop to.

 ffmpeg -i input.mp4 -filter:v "crop=width:height:x:y" output.mp4

Strip out metadata

 ffmpeg -i input.mp4 -map_metadata -1 -c copy output.mp4

Stabilize a shaky video

This is an alternative to using the Windows program Virtualdub's deshaker function.

 ffmpeg -i input.mp4 -vf deshake output.mp4

Sharpen a video

 ffmpeg -i input.mp4 -vf unsharp=3:3:1.5 output.mp4

Increase video brightness

 ffmpeg -i input.mp4 -vf eq=brightness=0.1 output.mp4
 or
 ffmpeg -i input.mp4 -vf lutyuv=y=val*2 output.mp4

To take a nighttime video and increase the brightness to where you can see people, use the video filter eq=brightness=0.5.

Note! To find the perfect brightness level, you can use this filter with ffplay thus:

 ffplay -vf eq=brightness=0.5 input.mp4

Rotate a video by a multiple of 90 degrees

 ffmpeg -i input.mp4 -vf "transpose=1" output.mp4

Where:

  • 1 = 90 degrees clockwise
  • 2 = 90 degrees counter-clockwise

Rotate a video by an arbitrary angle

 ffmpeg -i input.mp4 -vf "rotate=angle*(PI/180)" output.mp4

As you can see, ffmpeg expects the angle to be in radians. If you want to provide it in degrees, multiply by the PI/180 expression.

Horizontally flip a video

 ffmpeg -i input.mp4 -vf "hflip" output.mp4

Vertically flip a video

 ffmpeg -i input.mp4 -vf "vflip" output.mp4

Concatenate videos with reencode

 ffmpeg -i 1.mp4 -i 2.mp4 \
  -filter_complex "[0:v] [0:a] [1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]" \
  -map "[v]" -map "[a]" -b 9M output.mp4

Concatenate videos without reencode

 echo 'file 1.mp4' > files.txt
 echo 'file 2.mp4' >> files.txt
 ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4

Extact all frames from a video

 ffmpeg -i input.mp4 '%05d.bmp'

Concatenate images (frames) to make a video

Let's say your images are IMG_1000.jpg with increasing numbers. This is useful for creating a time lapse or stop motion movie.

  ffmpeg -i IMG_%04d.png -qscale:v 1 output.mov

Play a video

FFMPEG includes the movie player ffplay. It's roughly similar in quality to MPlayer.

 ffplay myfile.mp4

Play a video with decreased brightness

 ffplay -vf eq=brightness=-0.5:saturation=2 myfile.mp4
 or
 ffplay -vf lutyuv=y=val*0.5 myfile.mp4

About h264 Video quality

Many computers and mobile devices now have built-in h264 decoding hardware, so it's a good idea to encode in that format.

The encoding quality is represented mainly as the h264 profile.

The default video encoding quality for h264 that ffmpeg uses however is the Low profile, which allows for rapid encoding but will often result in sub-par videos. Even if you set the bitrate to a high value, if the profile is Low, the quality will still be poor.

This is due to the fact that the profile tells ffmpeg how much algorithmic effort should go into producing a quality encoding, or essentially how much thinking the encoder should do.

To achieve a smaller, higher quality output the h264 profile must be set to High which results in more careful processing.

There is also a level setting that corresponds roughly to the resolution of your video, with 6 being appropriate up to 4K video.

Finally you can specify the amount of time to let ffmpeg think over how to best encode your video. This is the preset, with veryslow being the second-to-last choice, the last being very time consuming.

Thus I recommend these settings:

 -vcodec h264 -profile:v high -level:v 6.0 -preset veryslow

An example usage might be:

 ffmpeg input.mp4 -vcodec h264 -profile:v high -level:v 6.0 -preset veryslow -acodec mp2 -b:a 160k output.mp4

Links

1362616665