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

Subtitle codec 96257 is not supported

[matroska @ 0x55e337388d80] Subtitle codec 96257 is not supported.
Could not write header for output file #0 (incorrect codec parameters ?): Function not implemented
Error initializing output stream 0:1 -- 
Conversion failed!

https://video.stackexchange.com/a/17009

stream apparently is a CEA-608 subtitle stream, which during the days of analog television signal transmission was caption data embedded within the video data. Apparently FFmpeg can extract it but can't mux it to a new container.

It doesn't seem like there's a whole lot that can be done.

Subtitle codec 94213 is not supported

Video uses a non-standard and wasteful way to store B-frames

[mpeg4 @ 0x5607fe47e0c0] Video uses a non-standard and wasteful way to store B-frames ('packed B-frames'). Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.

B-frames are a frame type used in video compression to represent frames of a video. B-frames can use information from both previous and future frames to represent each video frame.

Older DivX-encoded videos commonly use an ugly method called packed bitstream which puts several video frames into a single AVI chunk. Packed bitstream isn't standard MPEG-4, uses more space, requires more CPU power to encode/decode, and (most importantly) may cause problems if copied into another container type. This is the main reason for the warning.

Since you're re-encoding the video instead of just copying it, you should be fine. If you wanted to keep the original video, but copy it into another type of container (say MP4 or MKV), it'd be best to unpack the B-frames first using the FFmpeg filter mpeg4_unpack_bframes.

You could unpack the B-frames with something simple like
ffmpeg -i INPUT.avi -codec copy -bsf:v mpeg4_unpack_bframes OUTPUT.avi
public/it/ffmpeg.1649173731.txt.gz · Last modified: 2022/04/05 10:48 by phil