User Tools

Site Tools


Sidebar

Public
Internal
public:it:ffmpeg

This is an old revision of the document!


ffmepg

Build

Manipulating audio channels

5.1 Surround downmix to stereo

FL, FC, FR, BL, BR, LFE → FL, FR

-ac 2 \
-af "pan=stereo|FL = 0.374107*FC + 0.529067*FL + 0.458186*BL + 0.264534*BR + 0.374107*LFE |FR = 0.374107*FC + 0.529067*FR + 0.458186*BR + 0.264534*BL + 0.374107*LFE" \

7.1 downmix to stereo

FL, FC, FR, SL, SR, BL, BR, LFE → FL, FR

-ac 2 \
-af "pan=stereo|FL = 0.274804*FC + 0.388631*FL + 0.336565*SL + 0.194316*SR + 0.336565*BL + 0.194316*BR + 0.274804*LFE | FR = 0.274804*FC + 0.388631*FR + 0.336565*SR + 0.194316*SL + 0.336565*BR + 0.194316*BL + 0.274804*LFE" \

6.1 downmix to stereo

FL, FC, FR, SL, SR, BC, LFE → FL, FR

-ac 2 \
-af "pan=stereo|FL = 0.321953*FC + 0.455310*FL + 0.394310*SL + 0.227655*SR + 278819*BC + 0.321953*LFE | FR = 0.321953*FC + 0.455310*FR + 0.394310*SR + 0.227655*SL + 278819*BC + 0.321953*LFE" \

5.0 downmix to stereo

FL, FC, FR, BL, BR → FL, FR

-ac 2 \
-af "pan=stereo|FL = 0.460186*FC + 0.650802*FL + 0.563611*BL + 0.325401*BR | FR = 0.460186*FC + 0.650802*FR + 0.563611*BR + 0.325401*BL" \

Quadraphonic Channel downmix to stereo

FL, FR, BL, BR → FL, FR

-ac 2 \
-af "pan=stereo|FL = 0.422650*FL + 0.366025*BL + 0.211325*BR | FR = 0.422650*FR + 0.366025*BR + 0.211325*BL" \

Linear Surround Channel downmix to stereo

FL, FC, FR → FL, FR

-ac 2 \
-af "pan=stereo|FL = 0.414214*FC + 0.585786*FL | FR = 0.414214*FC + 0.585786*FR" \

Scaling

QSV Scale to 1080p

Quality with QSV as not good at all, though this could have been my fault.

ffmpeg \
  -y \
  -hwaccel qsv \
  -qsv_device /dev/dri/card0 \
  -c:v hevc_qsv \
  -i input.mkv \
  -c:v hevc_qsv \
  -vf 'scale_qsv=w=1920' \
  -preset slow \
  -global_quality 25 \
  -c:a copy \
  -c:s copy \
  output.mkv

4k to 1080p

  • libx264
  • libx265
ffmpeg \
  -i MyMovie_4K.mkv \
  -vf scale=1920:-1 \
  -c:v libx265 \
  -preset veryslow \
  -c:a copy \
  MyMovie_1080p.mkv

Not sure if these are necessary.

-crf 28

Subtitles

MKV Embed soft subtitles

ffmpeg \
  -i in.mkv \
  -f srt \
  -i in.srt \
  -map 0:0 \
  -map 0:1 \
  -map 1:0 \
  -c:v copy \
  -c:a copy \
  -c:s srt \
  out.mkv

MKV hardcode subtitles

http://diantokam.blogspot.com/2016/12/adding-subtitle-and-scaling-film-using.html

ffmpeg \
  -i "input.mkv" \
  -vf subtitles=fullsubs.srt \
  "output.mkv"

MKV to MP4 hardcode subtitles and downmix 5.1 to stereo

  # export english subs
  ffmpeg \
    -y \
    -i "./${name}" \
    -map 0:s:0 \
    eng.srt

  # create a version with hardsubs
  # si=0
  ffmpeg \
    -y \
    -i "./${name}" \
    -vf subtitles="${name}":si=0 \
    -ac 2 \
    -af "pan=stereo|FL = 0.374107*FC + 0.529067*FL + 0.458186*BL + 0.264534*BR + 0.374107*LFE |FR = 0.374107*FC + 0.529067*FR + 0.458186*BR + 0.264534*BL + 0.374107*LFE" \
    -c:a aac \
    "${name}-hardsubs.mp4"

Delay subtitle by 1 second

ffmpeg \
  -itsoffset -1 \
  -i full8.srt \
  -c copy \
  full8_delayed.srt

Video Streaming

Clips

ffmpeg \
  -i  input.mkv \
  -ss 29:36 \
  -to 29:50 \
  -c:v copy \
  -c:a aac \
  output.mp4

h264 to hevc using nvenc

  1. Install docker
docker run \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  --runtime=nvidia jrottenberg/ffmpeg:4.1-nvidia \
  -hwaccel cuvid \
  -c:v h264_cuvid \
  -i input.mkv \
  -c:v hevc_nvenc \
  -c:a copy \
  -c:s copy \
  -preset slow \
  output_hevc.mkv

Errors

Error creating a NVDEC decoder

[h264 @ 0x56028de15ec0] Error creating a NVDEC decoder: 1
[h264 @ 0x56028de15ec0] Using more than 32 (34) decode surfaces might cause nvdec to fail.
[h264 @ 0x56028de15ec0] Try lowering the amount of threads. Using 16 right now.
[h264 @ 0x56028de15ec0] Failed setup for format cuda: hwaccel initialisation returned error.
Note that cuda hwaccel is an alias to the newer nvdec implementation, and to my knowledge, has no way of setting up decoder surface limits.

Here's a workaround you can try:
ffmpeg \
  -threads 4 \
  -hide_banner \
  -c:v h264_cuvid \
  -surfaces 8 \
  -nostats \
  -i rtmp://<ip>:<port>/myapp/mystream \
  -filter_complex [0]fps=fps=1[s0] \
  -map [s0] \
  -f rawvideo \
  -pix_fmt rgb24 pipe
Two changes:

1: Call up the h264_cuvid decoder directly, and apply a surface limit count of 8. This should be adequate.
2: Limit the thread count.

Non-monotonous DTS in output stream

None of this worked.

[matroska @ 0x55a1a3738340] Non-monotonous DTS in output stream 0:1; previous: 490791, current: 490731; changing to 490791. This may result in incorrect timestamps in the output file.

Try -use_wallclock_as_timestamps 1 and -fflags +genpts

FFMPEG: Too many packets buffered for output stream 0:1

Add the following after the input file:

-max_muxing_queue_size 9999
public/it/ffmpeg.1648077478.txt.gz · Last modified: 2022/03/23 18:17 by phil