Konubinix' opinionated web of thoughts

Ffmpeg

Fleeting

make it run of mobile

Most mobile devices and browsers — including Chrome on tablets — do not support H.264 with 4:2:2 chroma subsampling (yuvj422p). They only support 4:2:0 (yuv420p or yuvj420p) for H.264 video streams in MP4 containers

[…]

ffmpeg -i input_ko.mp4 -c:v libx264 -pix_fmt yuv420p -c:a aac -ar 44100 -movflags +faststart output_fixed.mp4

[…]

To extract and display only the chroma subsampling format (e.g., 4:2:2 or 4:2:0) from a video using ffmpeg, you can use the following command-line one-liner:

[…]

ffprobe -v error -select_streams v:0 -show_entries stream=pix_fmt -of default=noprint_wrappers=1:nokey=1 input.mp4

[…]

Format Color Range Typical Use Compatibility Pros / Cons yuv420p Limited (TV range) Most standard video codecs & players Very wide compatibility (web, broadcast, players) Standard for video playback; better for broadcast and streaming; some slight color compression due to limited range yuvj420p Full (JPEG range) JPEG-based codecs, still images, some special cases Less widely supported for video playback Better color precision if your workflow expects full range; can cause compatibility issues in players expecting limited range

[…]

Try forcing limited range explicitly with -color_range option, e.g.:

ffmpeg -i input.mp4 -pix_fmt yuv420p -color_range tv output.mp4

https://chatgpt.com/c/ ([2025-05-21 Wed])

To track all my video for bad ones (taking advantage of the situation to also check that they are streamable)

source _docs.sh
sql_raw -c "\copy ( select web_cid from video where web_cid is not null order by cid ) to '~/tmp/webcid.txt'"
COPY 8378
source ~/Prog/Devel/lib/lib_bash.sh
source _docs.sh
set -eu
while popline ~/tmp/webcid.txt webcid
do
    cid="$(sql_raw -c "\copy ( select cid from video where web_cid = '${webcid}' ) to stdout")"
    echo "${cid} -> ${webcid}"
    chromasubsampling="$(ffprobe -v error -select_streams v:0 -show_entries stream=pix_fmt -of default=noprint_wrappers=1:nokey=1 "${webcid}")"
    moovok="$(ffprobe -v trace -i "${webcid}" 2>&1 | awk '/type:.moov./ {moov=NR} /type:.mdat./ {mdat=NR} END {if (moov && mdat && moov < mdat) print "moov ok"; else print "moov after mdat"}')"
    if ! echo ${chromasubsampling} | grep -q "yuvj\?420p" || test "${moovok}" != "moov ok"
    then
        msg="${webcid} is BAD! -> ${chromasubsampling}, ${moovok}"
        if ! echo ${chromasubsampling} | grep -q "yuvj\?420p"
        then
            clk ntfy "${msg}"
        fi
        echo "${msg}"
        cid="$(sql_raw -c "\copy ( update video set web_cid = null, web_allocations = '[]' where web_cid = '${webcid}' returning cid ) to stdout")"
        echo "cid was ${cid}"
        exit 1
    else
        echo "ok, chromasubsampling=${chromasubsampling}"
    fi
done

subtitle encoding currently only possible from text to text or bitmap to bitmap

Change the format or the subtitle codec

ffmpeg -i input output.newformat

or

ffmpeg -i input -scodec webvtt output.newformat

speed up/slow down a video

This does not affect the audio speed. Use -an to not include audio in the output

https://superuser.com/questions/1261678/how-do-i-speed-up-a-video-by-60x-in-ffmpeg

ffmpeg -i input.mkv -filter:v “setpts=PTS/60” output.mkv

https://superuser.com/questions/1261678/how-do-i-speed-up-a-video-by-60x-in-ffmpeg

To double the speed of the video with the setpts filter, you can use:

ffmpeg -i input.mkv -filter:v “setpts=0.5*PTS” output.mkv

https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video

faster method, but which can have unexpected results with audio (pauses or async):

ffmpeg -itsscale 0.01666 -i input.mkv -c copy output.mkv

https://superuser.com/questions/1261678/how-do-i-speed-up-a-video-by-60x-in-ffmpeg

re-encode i.e. a timelapse, e.g. ffmpeg -i input -vf framestep=60,setpts=N/30/TB -r 30 -an out.mp4

https://superuser.com/questions/1261678/how-do-i-speed-up-a-video-by-60x-in-ffmpeg

encode VP8

there’s a constant quality mode (like in the x264 encoder) that will ensure that every frame gets the number of bits it deserves to achieve a certain quality level, rather than forcing the stream to have an average bit rate. This results in better overall quality and should be your method of choice when you encode video with libvpx.

https://trac.ffmpeg.org/wiki/Encode/VP8

this case, the target bitrate becomes the maximum allowed bitrate. You enable the constant quality mode with the CRF parameter:

ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm

https://trac.ffmpeg.org/wiki/Encode/VP8

By default the CRF value can be from 4–63, and 10 is a good starting point. Lower values mean better quality.

https://trac.ffmpeg.org/wiki/Encode/VP8

Important: If neither -b:v nor -crf are set, the encoder will use a low default bitrate and your result will probably look very bad. Always supply one of these options—ideally both.

If you want to tweak the quality even further, you can set two more parameters:

-qmin – the minimum quantizer (default 4, range 0–63) -qmax – the maximum quantizer (default 63, range qmin–63)

These Q values are quantization parameters, and lower generally means “better quality”. If you set the bounds from 0 to 63, this means the encoder has free choice of how to assign the quality. For a better overall quality, you can try to set -qmin to 0 and -qmax to 50 or lower. For example:

ffmpeg -i input.mp4 -c:v libvpx -qmin 0 -qmax 50 -crf 5 -b:v 1M -c:a libvorbis output.webm

https://trac.ffmpeg.org/wiki/Encode/VP8

should use constant bitrate encoding if you need your video files to have a certain size, or if you’re streaming the videos over a channel that only allows a certain bit rate.

https://trac.ffmpeg.org/wiki/Encode/VP8

subtitles

set subtitles track as default

setting metadata in subtitles (like the language in english)

add a bunch of subtitles in a file stored in ipfs

ffmpeg add a bunch of subtitles in a file stored in ipfs Using

while read filename srt
do
    echo "$srt -> $filename"
    ffmpeg -nostdin -y -hide_banner -loglevel warning -stats -i $filename -i $srt -vcodec copy -acodec copy -map 0:a:0 -map 1:s:0 -metadata:s:s:0 language=eng -map 0:v:0 -disposition:s:0 default $srt.webm
done < data.txt

In short,

  1. take the videos and the subtitles put in data.txt,
  2. get the video and audio from the file and the subtitle from the subtitle file,
  3. set the subtitle as shown by default,
  4. tell that it is in English,

making mp4 videos streamable

Fast start Flag: -movflags +faststart. Moves some data to the beginning of the file, allowing the video to be played before it is completely downloaded

https://gist.github.com/jaydenseric/220c785d6289bcfd7366

more silent, quiet, less verbose

make ffmpeg more silent (less verbose)

-hide_banner -loglevel warning -stats

What are reasonable values for -aq when creating mp3?

cut from start time to end time

ffmpeg -ss 00:01:00 -to 00:02:00 -i input.mp4 -ss 00:01:00 -to 00:02:00 -c copy output.mp4

Beware the order of arguments, -i after the -ss and -to

Also, think about adding -movflags +faststart or otherwise, in the resulting video, the moov will get after the mdat (making mp4 videos streamable)

Interestingly, changing the parameter order solves the problem: ffmpeg -ss 00:01:00 -to 00:02:00 -i input.mp4 -ss 00:01:00 -to 00:02:00 -c copy output.mp4. Now, however, skipping is broken and the end time is not correct (in my case)

https://stackoverflow.com/questions/18444194/cutting-multimedia-files-based-on-start-and-end-time-using-ffmpeg ([2025-05-21 Wed])

crop a video

ffmpeg -y -i output_video.mp4 -filter:v “crop=500:200:130:100” a.mp4

set the creation time (for my timelapses)

ffmpeg -i a.mp4 -y -metadata creation_time=2024-12-07T08:12:49 -c copy b.mp4

keeping quality during conversion

generally speaking, smaller values here mean better results

  • qmin integer (encoding,video) Set min video quantizer scale (VBR). Must be included between -1 and 69, default value is 2.
  • qmax integer (encoding,video) Set max video quantizer scale (VBR). Must be included between -1 and 1024, default value is 31.

https://ffmpeg.org/ffmpeg-codecs.html

Notes linking here