ffmpeg


1. 编码质量,编码速度和编码时间的关系

You can have fast, high-quality encoding, but the file will be large

You can have high-quality, smaller file size, but the encoding will take longer

You can have small files with fast encoding, but the quality will be bad

ffmpeg -i -c:v libx264 -crf 23 -preset ultrafast -an output.mkv

ffmpeg -i -c:v libx264 -crf 23 -preset medium -an output.mkv

All presets: ultrafastsuperfastveryfastfasterfastmediumslowslowerveryslow

2. 改变帧率

Simple way to change the framerate by dropping or duplicating frames:

ffmpeg -i -r 24

More complex ways involve filtering, see fpsmpdecimateminterpolate filters:

ffmpeg -i -filter:v fps=24

3. Each file and its streams have a unique ID, starting with 0.

Examples:

  • 0:0 is the first stream of the first input file
  • 0:1 is the second stream of the first input file
  • 2:a:0 is the first audio stream of the third input file

You can map input streams to output, e.g. to add audio to a video:

ffmpeg -i input.mp4 -i input.m4a -c copy -map 0:v:0 -map 1:a:0 output.mp4

4. ffmpeg has lots of video, audio, subtitle filters:

ffmpeg -i  -filter:v ",," 

 has a name and several options, and some pre-defined variables:

 -filter:v ==:=

5. SCALING

Scale to 320×240:

ffmpeg -i  -vf "scale=w=320:h=240" 

Scale to a height of 240 and keep aspect ratio divisible by 2:

ffmpeg -i  -vf scale=w=-2:h=240 

Scale to 1280×720 or smaller if needed:

ffmpeg -i -vf "scale=1280:720:force_original_aspect_ratio=decrease"

6.

Add black borders to a file, e.g. 1920×800 input to 1920×1080:

ffmpeg -i -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2"

You can use mathematical expressions

ow and oh are output width and height

iw and ih are input width and height

7.

Simple fade-in and fade-out at a specific time for a specific duration.

ffmpeg -i -filter:v "fade=t=in:st=0:d=5,fade=t=out:st=30:d=5" 

8.DRAWING TEXT

Complex system for printing text on video:

ffmpeg -i  -vf \
drawtext="text='Test Text':x=100:y=50:\
fontsize=24:fontcolor=yellow:box=1:boxcolor=red" \

COMPLEX FILTERING

Complex filters have more than one in- and/or output:

ffmpeg -i  -i  -filter_complex \
    "[0:v:0][1:v:0]overlay[outv]" \
    -map "[outv]" 

ffmpeg -i ndh.mp4 -i swimming.mp4 -i qq.mp4 -filter_complex "[0:0][0:1][1:0][1:1][2:0][2:1]concat=n=3:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" concat.mp4

TIMELINE EDITING

Enable filters only at a specific point in time.

Example:

  • Show a watermark in the top left corner
  • Between seconds 1 and 2 only
ffmpeg -i 

DEBUGGING MOTION VECTORS

Simple way to visualize motion in FFmpeg with H.264 codecs (does not work for other codecs):

ffplay -flags2 +export_mvs input.mp4 -vf codecview=mv=pf+bf+bb
ffmpeg -flags2 +export_mvs -i input.mp4 -vf codecview=mv=pf+bf+bb 
  • pf – forward predicted motion vectors of P pictures
  • bf – forward predicted motion vectors of B pictures
  • bb – backward predicted motion vectors of B pictures

DEBUGGING MOTION VECTORS

Simple way to visualize motion in FFmpeg with H.264 codecs (does not work for other codecs):

ffplay -flags2 +export_mvs input.mp4 -vf codecview=mv=pf+bf+bb
ffmpeg -flags2 +export_mvs -i input.mp4 -vf codecview=mv=pf+bf+bb 
  • pf – forward predicted motion vectors of P pictures
  • bf – forward predicted motion vectors of B pictures
  • bb – backward predicted motion vectors of B pictures

DEBUGGING MOTION VECTORS

More info: http://trac.ffmpeg.org/wiki/Debug/MacroblocksAndMotionVectors

VIDEO STREAM ANALYZERS

Different software for analyzing bitstreams graphically:

  • Elecard Stream Analyzer (commercial)
  • CodecVisa (commercial)
  • Intel Video Pro Analyzer (commercial)
  • AOMAnalyzer (free, AV1/VP9 video)
 https://slhck.info/ffmpeg-encoding-course/#/