fabic974 random (binary) wanderings in the philament empires…

FFmpeg audio/video trans/coding

Extract selected streams from a source video.mkv, typically the video track and one of the audio tracks (use ffprobe to display the streams first) :

ffprobe -hide_banner video.mkv

ffmpeg -loglevel info -i video.mkv \
  -map 0:0 -map 0:2 \
  -c copy test.mkv

¿ -itsoffset 1.0 what is ?

ffmpeg -i video1.mkv -itsoffset 1.0 -i video2.avi \
  -c copy -map 0:v:0 -map 0:a:0 -map 1:a:0 -map 0:s:0 \
  test.mkv
  • Video scaling -vf scale=-1:720
ffmpeg -i video.mp4 \
  -codec:v libx264 -profile:v high -preset slow \
  -b:v 4000k -maxrate 8000k -bufsize 32000k -vf scale=-1:720 -threads 0 \
  -codec:a aac -b:a 128k \
  test.mp4

Extract a slice of video.mkv without re-encoding the streams (copy) :

ffmpeg -i video.mkv -ss 00:46:00 -to 00:46:39 -c copy test.mkv

Extract a slice of video.mkv -ss 00:20:58 -to 00:21:18, with subtitles included -c:s mov_text

ffmpeg -loglevel info -stats -i video.mp4 \
  -ss 00:20:58.250 -to 00:21:18.500 \
  -map 0:0 -map 0:1 -map 0:2 \
  -codec:v libx264 -profile:v main -level 3.1 -preset veryslow \
  -crf 18 -bf 2 -flags +cgop -pix_fmt yuv420p -threads 0 \
  -codec:a aac -strict -2 -b:a 160k -r:a 44000 \
  -c:s mov_text -movflags faststart \
    test.mp4

Likewise with -vf subtitles=video.mp4 for reading subtitles embedded in the source video, and encode these in the output video as hardsubs subtitles :

ffmpeg -loglevel info -stats \
  -i video.mp4 -ss 00:20:59.5 -to 00:21:17.5 \
  -codec:v libx264 -profile:v main -level 3.1 -preset veryslow \
  -crf 18 -bf 2 -flags +cgop -pix_fmt yuv420p \
  -vf subtitles=video.mp4 -threads 0 \
  -codec:a aac -strict -2 -b:a 160k -r:a 44000 \
  -movflags faststart \
    test.mp4

¿ why such a high audio bitrate ?

ffmpeg -i video.mkv -ss 00:45:59 -to 00:46:40 \
  -codec:v libx264 -crf 21 -bf 2 -flags +cgop -pix_fmt yuv420p \
  -codec:a aac -strict -2 -b:a 384k -r:a 48000 \
  -movflags faststart \
  test.mp4

High profile, slow preset, CRF 18… :

ffmpeg -i video.mkv -ss 00:45:59 -to 00:46:40 \
  -codec:v libx264 -profile:v high -preset slow -crf 18 -bf 2 \
  -flags +cgop -pix_fmt yuv420p -vf scale=-1:478 -threads 0 \
  -codec:a aac -strict -2 -b:a 192k -r:a 44000 \
  -movflags faststart \
  test.mkv

Best compatibility with mobile devices. Main profile level 3.1, very slow preset, CRF 21 :

ffmpeg -i video.mkv \
  -codec:v libx264 -profile:v main -level 3.1 -preset veryslow -crf 21 -bf 2 \
  -flags +cgop -pix_fmt yuv420p -vf scale=-1:478 -b:v 3072 -threads 0 \
  -codec:a aac -strict -2 -b:a 160k -r:a 44000 \
  -movflags faststart  test.mp4

Add subtitles

ffmpeg -loglevel info \
  -i video.mp4 \
  -sub_charenc latin1 -i "video-subs.srt" \
  -map 0:0 -map 0:1 -map 1:0 \
  -c:v copy -c:a copy -c:s mov_text \
    output-video.alt.subs.mp4

Fetching MP2T video streams

Used streamlink to fetch the MP2T video stream :

streamlink --loglevel info \
  -o ~/Downloads/video.mp4 \
  https://s18.escdn.co/hls/jg6n...,ifzp...,tnzp...,.urlset/master.m3u8 \
  best

ffprobe

ffprobe -v quiet -print_format json -show_streams -show_format video.mkv

ffprobe -v info -hide_banner video.mkv

Generate thumbnail image

ffmpeg -ss 10 -i video.mp4     \
  -vf "thumbnail,scale=-1:480" \
  -frames:v 1                  \
  -vsync vfr                   \
  -y thumbnail.jpg </dev/null

Adding thumbnail image to video file using mp4art

mp4art -zv --add thumbnail.jpg video.mp4

Pointers

EOF