FFmpeg Tricks


[Home] [Technology]

Various movie editing tasks made easy


Installing ffmpeg with libx264

  • 1. Make sure libx264-devs or x264-libs is installed in your system
  • 2. Always install ffmpeg from a repo such that it is compiled with --enable-libx264 or compile it yourself with --enable-libx264 enabled


Using ffmpeg with libx264 and native aac

  • 1. -c:v libx264: Encodes with X.264 codec
    • I. -preset: Determines encoding speed to compression ratio. ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. medium is default. placebo is not useful at all.
    • II. -crf: Constant Rate Factor. Determines output quality. 0 is lossless, 23 is default, and 51(8-bit x264) or 63(10-bit x264) is worst possible. Increasing the CRF value +6 is roughly half the bitrate while -6 is roughly twice the bitrate. A typical example:
      ffmpeg -i input -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv
    • III. -qp 0: Lossless. For fastest encoding:
      ffmpeg -i input -c:v libx264 -preset ultrafast -qp 0 output.mkv
      and for best compression:
      ffmpeg -i input -c:v libx264 -preset veryslow -qp 0 output.mkv
      [Most non-FFmpeg based players will not be able to decode lossless.]
    • IV. two pass: Targets a specfic size. Target Size (kB) * 8 / Video Time (seconds) = total bitrate (kbps). total bitrate (kbps) - target audio bitrate (kbps) = target video bitrate (kbps). Example:
      ffmpeg -y -i input -c:v libx264 -preset medium -b:v 555k -pass 1 -c:a libfdk_aac -b:a 128k -f mp4 /dev/null && \
      ffmpeg -i input -c:v libx264 -preset medium -b:v 555k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4
  • 2. -c:a aac: Encodes with Native ffmpeg AAC codec. As it is experimental -strict -2 should be used.
    • I. -b:a: Constant Bit Rate. Example:
      ffmpeg -i input.wav -strict -2 -c:a aac -b:a 240k output.m4a
    • II. -q:a: Audio Quality (0.1-10). Example:
      ffmpeg -i input.wav -strict -2 -c:a aac -q:a 6 output.m4a
  • Note: You can use libx265 with the same options as libx264 to encode in experimental X.265 codec. Here the default CRF of 28 corresponds to CRF of 23 in X.264.


To remove or drop an audio or a video stream use -an or -vn respectively.

  • 1


Joining or concatenating files

  • 1. concat filter: Concatenates files with different codecs
    • I. Example:
      ffmpeg \
      -i input1.mp4 \
      -i input2.webm \
      -filter_complex '[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]' \
      -map '[v]' -map '[a]' \
      -c:v libx264 -preset slow -crf 22 \
      output.mkv
    • II. In [0:0] [0:1] etc. the first number represents the input file number and the second number represents the stream number (usually 0 for video and 1 for audio). All desired streams are mentioned here.
    • III. The name of the filter is concat, number of input files is n=2, number of output video streams is v=1, number of output audio streams is a=1, names of the output streams are [v] and [a].
    • IV. -map '[v]' -map '[a]' tells ffmpeg to use the result of the filter rather than the streams themselves from input files for further operations.
    • V. As filters are incompatible with stream copying, you can't use -c copy in this method.
  • 2. concat protocol: Concatenates certain file formats with same codecs. Works at file level rather than stream level.
    • I. Example:
      ffmpeg -i 'concat:input1.mpg|input2.mpg|input3.mpg' -c copy output.mpg
    • II. mp4 files can be losslessly concatenated. Example:
      ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
      ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
      ffmpeg -i 'concat:intermediate1.ts|intermediate2.ts' -c copy -bsf:a aac_adtstoasc output.mp4
    • III. Same as II using named pipes. Example:
      mkfifo temp1 temp2
      ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp1 2> /dev/null & \
      ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts temp2 2> /dev/null & \
      ffmpeg -f mpegts -i 'concat:temp1|temp2' -c copy -bsf:a aac_adtstoasc output.mp4


Cutting files

  • 1. -ss and -to: Using start and end position
    • I. Example:
      ffmpeg -i input.mp4 -ss 00:00:03.000 -to 00:00:11.000 -c copy cut.mp4
      or
      ffmpeg -i input.mp4 -ss 3 -to 11 -c copy cut.mp4
    • II. Cuts without Reencoding movies from 3 second to 11 second.
    • III. Time format is either in seconds or in 'hh:mm:ss[.xxx]'


Merging a video and an audio streams into a file

  • 1. -map: Mapping input and output streams
    • I. Example:
      ffmpeg -i video.mp4 -i audio.wav \
      -c:v libx264 -preset slow -crf 22 -c:a aac -strict experimental \
      -map 0:v:0 -map 1:a:0 output.mp4
    • II. Maps video stream from the first file and audio stream from the second file as output streams.
    • III. The order of -map options, specified on cmd line, will create the same order of streams in the output file.
    • IV. More than two streams can be merged into a single file.


Changing speed of a video

  • 1. setpts filter: Change speed of a video
    • I. the audio stream is not changed, so it should ideally be disabled with -an.
    • II. Example:
      ffmpeg -i input.mp4 -filter:v 'setpts=0.5*PTS' output.mp4
      or
      ffmpeg -i input.mp4 -filter:v 'setpts=2.0*PTS' output.mp4
    • III. Doubles or halves the speed of the video respectively.
    • IV. Example:
      ffmpeg -i input.mp4 -r 16 -filter:v 'setpts=0.25*PTS' output.mp4
    • V. Quadruples the speed of the video and avoids framedrop (framerate at input = 4 to framerate at output = 16).


Scaling or resizing a video

  • 1. scale filter: Resize a video
    • I. Example:
      ffmpeg -i input.avi -vf scale=320:240 output.avi
      or
      ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png
    • II. Resizes a video or an image to a specific size (here 320x240).
    • III. Example:
      ffmpeg -i input.jpg -vf scale=320:-1 output_320.png
      or
      ffmpeg -i input.jpg -vf scale=-1:240 output_240.png
    • IV. Resizes and keeps the aspect ratio same as original.


Source:
https://trac.ffmpeg.org/wiki
Effect of different presets on a testfile
preset filesize
ultrafast 3728079
superfast 2339853
veryfast 2133742
faster 2188982
fast 2128437
medium 2096879
slow 1961687
slower 1930088
veryslow 1624916
placebo 1510835
Effect of different crfs on a testfile
crf bitrate
0 147
1 5700
6 5225
12 3282
18 1864
24 974
30 454
36 220
42 112
48 81
51 81