Styles

2014-04-12

FFMPEG Cheatsheet

FFMPEG is a nifty console-based video and audio manipulation tool.

The thing is: It can be a bit hard to find the right options. So here they are...

For the following examples, by default "source.mp4" is the source input movie, and "result.mp4" is the result output, transformed movie.


Preamble

To simplify everything, the files are considered to be all in the same folder.

How to convert a file?

Let's say that I want to convert a file from one format to another (here, mp4 to mp3):

ffmpeg -i source.mp4 result.mp3

How to convert all files in the same directory with Bash?

Let's say that I want to convert all files in a directory from one format to another (here, mp4 to mp3), with Bash, using:

for i in *.mp4; do ffmpeg -i "$i" "${i%.*}.mp3"; done

How to cut a movie?

Let's say that I want only the subset of a movie that starts at the 4m42s time, and goes of for 1m17.5 duration.

ffmpeg -i source.mp4 -ss 00:04:32 -t 00:01:17.5 result.mp4

How to fade-in/fade-out a movie?

Fade-in is when we start from silence/blackness and it evolves into normal source/video. Fade-out is the opposite process: The normal sound/video fades out into silence/blackness.

Let's say that I want to fade-in the video with the following constraints:

  • The first 1.0 seconds must be black
  • Then, there should be a fade-in of 3 seconds:

ffmpeg -i source.mp4 -filter:v 'fade=in:st=1.0:d=3.0' result.mp4

Let's say that I want to fade-out the video with the following constraints:

  • The last 1.0 seconds must be black
  • Before that, at 20 minutes, should be a fade-out of 3 seconds:
This means that:

  • The whole process must start at: 1200.0 (seconds, which is 20 minutes)
  • The fade-out duration must be 3 seconds
  • After that, everything will be black

ffmpeg -i source.mp4 -filter:v 'fade=out:st=1200.0:d=3.0' result.mp4

Let's say that I want to fade-out the audio with the following constraints:

  • The last 1.0 seconds must be silence
  • Before that, at 20 minutes, there should be a fade-out of 3 seconds:

ffmpeg -i source.mp4 -filter:a 'afade=out:st=1200.0:d=3.0' result.mp4

Now, let's say you have a 20 seconds movie you want to fade-in (both in video and audio) from 0.5 seconds for 2 seconds, and then fade-out (both in video and audio) from 18.5 seconds for 1 second, all in one pass:

ffmpeg -i source.mp4 -filter:v 'fade=in:st=0.5:d=2.0,fade=out:st=18.5:d=1.0' -filter:a 'afade=in:st=0.5:d=2.0,afade=out:st=18.5:d=1.0' result.mp4


How to scale a movie?

I have a movie in the format 1920x1080, and I want to scale it to 640 pixels wide, respecting the proportions:

ffmpeg -i source.mp4 -vf scale=640:-1 result.mp4


How to change the aspect ratio of a movie?

Sometimes, we have a badly scaled movie, and after scaling it (see above) to a correct ratio, the viewed ratio aspect remains unchanged, negating the scaling. The solution is to set the aspect.

For example, the following source movie kept believing itself to be 1920x1080, despite having been resized into 744:418. So we set force the aspect:

ffmpeg -i source.mp4 -vcodec copy -acodec copy -aspect 744:418 result.mp4

How to crop the screen of a movie?

I have a movie, but I only want a subset of the screen. More precisely, I want a "submovie" of 320x200 wide, whose upper top corner start at the position 200x150 of the original movie:

ffmpeg -i source.mp4 -filter:v "crop=320:200:200:150" result.mp4


How to change a movie into an animated gif?

This is a simple conversion, gif being seen as another movie format:


ffmpeg -i source.mp4 result.gif


How to join multiple movies into one?

This is a bit more tricky. Let's say you have three files, a.mp4, b.mp4 and c.mp4, you want to merge them into a result.mp4.

First, you create a text file, say list.txt where you put your three files:

file 'a.mp4'
file 'b.mp4'
file 'c.mp4'

Then, you can use ffmepg:

ffmpeg -f concat -i list.txt -c copy result.mp4


How to join multiple movies into one (v2)?

Sometimes, this simple way of concatenating movies doesn't work. Then you must use the heavy weaponry.

Let's say you have 5 movies: a.mp4, b.mp4, c.mp4, d.mp4 and e.mp4.
Let's say you have one video stream, and one audio stream per movie.

The command is them:

ffmpeg -i a.mp4 -i b.mp4 -i c2.mp4 -i d.mp4 -i e.mp4 -filter_complex '[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] [3:0] [3:1] [4:0] [4:1] concat=n=5:v=1:a=1 [v] [a]' -map '[v]' -map '[a]' result.mp4


The full filter command description can be found at: http://ffmpeg.org/ffmpeg-filters.html#concat


How to increase/decrease the audio volume?

The command is simple. Let's say you want to increase the volume to 150% of the original:

ffmpeg -i source.wav -af "volume=1.5" result.wav




How to increase/decrease the speed of a movie?

Either video or audio can be increased/decreased.

Source: https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video


For video only, the instruction is:

ffmpeg -i source.mp4 -filter:v "setpts=0.5*PTS" result.mp4

Here the "setpts" parameter will multiply the speed by two (by dropping one frame for each two frames). There is a way to avoid that by artificially increasing the number of frames of the input. For example, assuming the original has 24 frames per seconds, and I want to multiply the speed by two, I will need to multiply the number of input frames by two:

ffmpeg -i source.mp4 -r 48 -filter:v "setpts=0.5*PTS" result.mp4

For audio only, the instruction is:

ffmpeg -i source.mp4 -filter:a "atempo=2.0" -vn result.mp4

Yes, speeding up is the opposite of the video's way to do (here, greater than one).

The above instructions are useless if you want to modify the speed of both the video and audio. In that case, the instruction (to multiply the speed by two) is:

fffmpeg -i source.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" result.mp4

Et voilĂ !