homelab/docs/ffmpeg Notes.md

27 KiB

Table of Contents

About ffmpeg

FFmpeg is a free and open-source software project consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the command-line ffmpeg tool itself, designed for processing of video and audio files. Wikipedia. ffmpeg.org.

Get Video Info with mediainfo

MediaInfo is a free, cross-platform and open-source program that displays technical information about media files, as well as tag information for many audio and video files. Wikipedia.

To install on Debian, run sudo apt install mediainfo. To get comprehensive media information about a file, simply use mediainfo <file>.

Get Test Image with ffmpeg

To get the test image, we extract frame of timestamp 15:20.01 from the test video with ffmpeg -ss 00:15:20.01 -i "$input" -frames:v 1 "$output".

Batch-ify ffmpeg Command

For a given ffmpeg -i $video <do stuff>, apply the command to all .mp4 videos in a directory and send all outputs to ./Transcodes/ with the following structure:

for video in ./*.mp4
do
    ffmpeg -i "$video" <do stuff> "Transcodes/${video%.*}.mp4"
done

Transcode Profiles

Original [40.3 Mb/s]

The pre-transcode data for the sample video. It was recorded using OBS with the following encoder settings at 1440p60:

Parameter Value
Rate Control CBR
Bitrate 40000 Kbps
Keyframe interval 0s (auto)
Preset Quality
Profile high
Look-ahead Yes
Psycho Visual Tuning Yes
GPU Nvidia RTX 3060 Ti
Max B-Frames 2

Mediainfo

General
Complete name                            : ffmpeg test file.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 4.57 GiB
Duration                                 : 16 min 12 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 40.3 Mb/s
Writing application                      : Lavf59.16.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Source duration                          : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 40.0 Mb/s
Width                                    : 2 560 pixels
Height                                   : 1 440 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 60.000 FPS
Minimum frame rate                       : 58.824 FPS
Maximum frame rate                       : 62.500 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.181
Stream size                              : 4.53 GiB (99%)
Source stream size                       : 4.53 GiB (99%)
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
mdhd_Duration                            : 972666
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 318 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 36.9 MiB (1%)
Title                                    : Track1
Default                                  : Yes
Alternate group                          : 1

Test Image

Test Image - Original

CRF 21 [19.9 Mb/s]

ffmpeg -i "$input" -movflags +faststart -c:v libx264 -preset slower -crf 21 "$output"

Mediainfo

General
Complete name                            : CRF 21.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 2.25 GiB
Duration                                 : 16 min 12 s
Overall bit rate                         : 19.9 Mb/s
Writing application                      : Lavf58.45.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5.1
Format settings                          : CABAC / 8 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Bit rate                                 : 19.8 Mb/s
Width                                    : 2 560 pixels
Height                                   : 1 440 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.089
Stream size                              : 2.24 GiB (99%)
Writing library                          : x264 core 160 r3011 cde9a93
Encoding settings                        : cabac=1 / ref=8 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=9 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=24 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=21.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 129 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 14.9 MiB (1%)
Default                                  : Yes
Alternate group                          : 1

Test Image

Test Image - CRF 21

CRF 27 [10.3 Mb/s]

ffmpeg -i "$input" -movflags +faststart -c:v libx264 -preset slower -crf 27 "$output"

Mediainfo

General
Complete name                            : CRF 27.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 1.17 GiB
Duration                                 : 16 min 12 s
Overall bit rate                         : 10.3 Mb/s
Writing application                      : Lavf59.9.102

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5.1
Format settings                          : CABAC / 8 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Bit rate                                 : 10.2 Mb/s
Width                                    : 2 560 pixels
Height                                   : 1 440 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.046
Stream size                              : 1.15 GiB (99%)
Writing library                          : x264 core 164 r3075 66a5bc1
Encoding settings                        : cabac=1 / ref=8 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=9 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=24 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=27.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Source duration                          : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 132 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 15.2 MiB (1%)
Source stream size                       : 15.2 MiB (1%)
Default                                  : Yes
Alternate group                          : 1
mdhd_Duration                            : 972629

Test Image

Test Image - CRF 27

1080p CRF 21 [11.9 Mb/s]

ffmpeg -i "$input" -movflags +faststart -vf scale=1920:1080 -c:v libx264 -preset slower -crf 21 "$output"

Mediainfo

General
Complete name                            : 1080p CRF 21.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 1.35 GiB
Duration                                 : 16 min 12 s
Overall bit rate                         : 11.9 Mb/s
Writing application                      : Lavf58.45.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5
Format settings                          : CABAC / 8 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Bit rate                                 : 11.8 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.095
Stream size                              : 1.33 GiB (99%)
Writing library                          : x264 core 160 r3011 cde9a93
Encoding settings                        : cabac=1 / ref=8 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=9 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=24 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=21.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 129 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 14.9 MiB (1%)
Default                                  : Yes
Alternate group                          : 1

Test Image

Test Image - 1080p CRF 21

1080p CRF 27 [6,232 kb/s]

ffmpeg -i "$input" -movflags +faststart -vf scale=1920:1080 -c:v libx264 -preset slower -crf 27 "$output" Our test file described in the Get Video Info section was compressed from 40.5 Mbps (over 16m12s) to

Mediainfo

General
Complete name                            : ffmpeg test file - 1080p crf 27.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 723 MiB
Duration                                 : 16 min 12 s
Overall bit rate                         : 6 232 kb/s
Writing application                      : Lavf58.45.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5
Format settings                          : CABAC / 8 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Bit rate                                 : 6 089 kb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.049
Stream size                              : 706 MiB (98%)
Writing library                          : x264 core 160 r3011 cde9a93
Encoding settings                        : cabac=1 / ref=8 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=9 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=24 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=27.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 129 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 14.9 MiB (2%)
Default                                  : Yes
Alternate group                          : 1

Test Image

Test Image - 1080p CRF 27

720p CRF 21 [6,314 kb/s]

ffmpeg -i "$input" -movflags +faststart -vf scale=1280:720 -c:v libx264 -preset slower -crf 21 "$output"

Mediainfo

General
Complete name                            : 720p CRF 21.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 732 MiB
Duration                                 : 16 min 12 s
Overall bit rate                         : 6 314 kb/s
Writing application                      : Lavf59.9.102

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings                          : CABAC / 8 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Bit rate                                 : 6 169 kb/s
Width                                    : 1 280 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.112
Stream size                              : 715 MiB (98%)
Writing library                          : x264 core 164 r3075 66a5bc1
Encoding settings                        : cabac=1 / ref=8 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=9 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=22 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=21.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Source duration                          : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 132 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 15.2 MiB (2%)
Source stream size                       : 15.2 MiB (2%)
Default                                  : Yes
Alternate group                          : 1
mdhd_Duration                            : 972629

Test Image

Test Image - 720p CRF 21

720p CRF 27 [3,305 kb/s]

ffmpeg -i "$input" -movflags +faststart -vf scale=1280:720 -c:v libx264 -preset slower -crf 27 "$output"

Mediainfo

General
Complete name                            : 720p CRF 27.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 383 MiB
Duration                                 : 16 min 12 s
Overall bit rate                         : 3 305 kb/s
Writing application                      : Lavf58.45.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings                          : CABAC / 8 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 8 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 16 min 12 s
Bit rate                                 : 3 162 kb/s
Width                                    : 1 280 pixels
Height                                   : 720 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.057
Stream size                              : 367 MiB (96%)
Writing library                          : x264 core 160 r3011 cde9a93
Encoding settings                        : cabac=1 / ref=8 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh / subme=9 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=22 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=27.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Codec configuration box                  : avcC

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : mp4a-40-2
Duration                                 : 16 min 12 s
Bit rate mode                            : Constant
Bit rate                                 : 129 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 14.9 MiB (4%)
Default                                  : Yes
Alternate group                          : 1

Test Image

Test Image - 720p CRF 27

Test a New Profile

  1. Name the profile. profile="<profile name>" (E.g. "720p CRF 21")
  2. Transcode the file. ffmpeg -i "original.mp4" <transcode settings> "$profile.mp4"
  3. Extract sample frame from transcoded file. ffmpeg -ss 00:15:20.01 -i "$profile.mp4" -frames:v 1 "$profile.png"
  4. Print media info for the new file. mediainfo "$profile.mp4"
  5. Update this doc!