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 <input> -c:v libx264 -crf 23 -preset ultrafast -an output.mkv
ffmpeg -i <input> -c:v libx264 -crf 23 -preset medium -an output.mkv
All presets: ultrafast
, superfast
, veryfast
, faster
, fast
, medium
, slow
, slower
, veryslow
2. 改变帧率
Simple way to change the framerate by dropping or duplicating frames:
ffmpeg -i <input> -r 24 <output>
More complex ways involve filtering, see fps
, mpdecimate
, minterpolate
filters:
ffmpeg -i <input> -filter:v fps=24 <output>
3. Each file and its streams have a unique ID, starting with 0.
Examples:
0:0
is the first stream of the first input file0:1
is the second stream of the first input file2: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 <input> -filter:v "<filter1>,<filter2>,<filter3>" <output>
A <filter>
has a name and several options, and some pre-defined variables:
-filter:v <name>=<option1>=<value1>:<option2>=<value2>
5. SCALING
Scale to 320×240:
ffmpeg -i <input> -vf "scale=w=320:h=240" <output>
Scale to a height of 240 and keep aspect ratio divisible by 2:
ffmpeg -i <input> -vf scale=w=-2:h=240 <output>
Scale to 1280×720 or smaller if needed:
ffmpeg -i <input> -vf "scale=1280:720:force_original_aspect_ratio=decrease" <output>
6.
Add black borders to a file, e.g. 1920×800 input to 1920×1080:
ffmpeg -i <input> -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2" <output>
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 <input> -filter:v "
fade=t=in:st=0:d=5,fade=t=out:st=30:d=5"
<output>
8.DRAWING TEXT
Complex system for printing text on video:
ffmpeg -i <input> -vf \
drawtext="text='Test Text':x=100:y=50:\
fontsize=24:fontcolor=yellow:box=1:boxcolor=red" \
<output>
COMPLEX FILTERING
Complex filters have more than one in- and/or output:
ffmpeg -i <input1> -i <input2> -filter_complex \ "[0:v:0][1:v:0]overlay[outv]" \ -map "[outv]" <output>
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 <video> -i <watermark> -filter_complex \
"[0:v][1:v]overlay=10:10:enable='between(t,1,2)'[outv]" \
-map "[outv]" <output>
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 <output>
pf
– forward predicted motion vectors of P picturesbf
– forward predicted motion vectors of B picturesbb
– 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 <output>
pf
– forward predicted motion vectors of P picturesbf
– forward predicted motion vectors of B picturesbb
– 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/#/