User Tools

Site Tools


doc:appunti:linux:video:ffmpeg

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
doc:appunti:linux:video:ffmpeg [2020/06/17 12:35] niccolodoc:appunti:linux:video:ffmpeg [2023/01/07 15:21] – [AVC (x264) is better than ASP (xvid4)] niccolo
Line 271: Line 271:
 ^ Video filter    | swresize, 1366x768, Bilinear  | ^ Video filter    | swresize, 1366x768, Bilinear  |
 ^ Basic x264      | Preset: **slow** (or less), Tuning: **film**, Profile: **High**, IDC Level: **Auto** | ^ Basic x264      | Preset: **slow** (or less), Tuning: **film**, Profile: **High**, IDC Level: **Auto** |
-^ Video encoding  | Average Bitrate (Two Pass), Average Bitrate 6000 kb/s (about Gb per hour)  |+^ Video encoding  | Average Bitrate (Two Pass), Average Bitrate 4096 kb/s (about 1.8 Gb per hour)  |
 ^ Audio codec     | <del>Lame MP3</del> Vorbis  | ^ Audio codec     | <del>Lame MP3</del> Vorbis  |
 ^ Audio bitrate   | CBR 192 (or higher)  | ^ Audio bitrate   | CBR 192 (or higher)  |
  
-We can use **Avidemux** to make the final rendering (re-encoding). For a **comman line only** solution you can consider ffmpeg to perfomr the re-encoding and **mkvmerge** (contained into the **mkvtoolnix** Debian packageto merge all into a Matroska container.+We can use **Avidemux** to make the final rendering (re-encoding). For a **command line only** solution you can consider **ffmpeg** to perfomr the re-encoding and to make the merge (mux) all into a Matroska container.
  
-==== AVC (x264) is better than ASP (xvid4) ====+<code bash> 
 +#!/bin/sh 
 +TITLE="Balcani, maggio 2022" 
 +ffmpeg \ 
 +    -i "video-high-quality.mkv"
 +    -i 'audio-music.ogg' -i 'audio-live.ogg'
 +    -map '0:v:0' -map '1:a:0' -map '2:a:0'
 +    -metadata title="$TITLE" -metadata:s:v:0 title="$TITLE"
 +    -metadata:s:a:0 title="Accompagnamento musicale"
 +    -metadata:s:a:1 title="Audio in presa diretta"
 +    -filter:v "scale=1366x768" -aspect "16:9"
 +    -vcodec 'libx264' -pix_fmt 'yuvj420p' -preset 'veryslow' -tune 'film' -profile:v 'high' -level:v 5 \ 
 +    -acodec copy \ 
 +    "2022-05_balcani.mkv" 
 +</code> 
 + 
 + 
 +====== Re-encoding with tonal correction ====== 
 + 
 +We had some video clips recorded with an **SJCAM Sj8 Pro** camera with a **bad color balance and saturation** due some bad tables [[..:..:hardware:sjcam-8pro-custom-firmware|loaded into the firmware]]. It is possibile to re-encode all the video clips applying an equalization filter keeping all the encoding paramteres as similar as possibile to the original ones. 
 + 
 +The video clips were **extracted from the original MP4 container** as **[[wp>MPEG transport stream|MPEG-TS]]** snippets containing only video (no audio). To re-encode each clip we used the following **ffmpeg** recipe: 
 + 
 +<code bash> 
 +#!/bin/sh 
 +
 +# Re-encode video clips in MPEG transport stream (MPEG-TS) format applying 
 +# some saturation and gamma correction. 
 +
 +# saturation:           In range 0.0 to 3.0. The default value is "1"
 +# gamma_{r|g|b}         In range 0.1 to 10.0. The default value is "1"
 + 
 +INPUT="$1" 
 +OUTPUT="$INPUT.eq.ts" 
 +EQ_FILTER="eq=saturation=0.88:gamma_r=0.917:gamma_g=1.007:gamma_b=1.297" 
 + 
 +# Produces MPEG segments like the ones produced by the SJCAM SJ8Pro: 
 +ffmpeg -i "$INPUT"
 +    -vf "$EQ_FILTER"
 +    -codec:v libx264 \ 
 +    -preset veryslow -profile:v main -level:v 4.2 -pix_fmt yuvj420p \ 
 +    -x264-params 'vbv-maxrate=38000:vbv_bufsize=20000:nal-hrd=vbr:force-cfr=1:keyint=8:bframes=0:scenecut=-1:ref=1'
 +    -keyint_min 8 -brand avc1 -f 3gp \ 
 +    -bsf:v h264_mp4toannexb -f mpegts \ 
 +    "$OUTPUT" 
 +</code> 
 + 
 +The gamma correction for the three RGB channels was determined with the GIMP, using the //Colors// => //Levels// => //Pick the gray point for all channels// tool. The use of MPEG-TS clips allowed the montage of the final video by just concatenating them. 
 + 
 +===== AVC (x264) is better than ASP (xvid4) =====
  
 See this page: **[[https://www.avidemux.org/admWiki/doku.php?id=general:common_myths|Common myths]]** to understand the differences between formats (standards) and codecs (pieces of software). Read also this simple page: **[[https://www.cyberlink.com/support/product-faq-content.do?id=1901|Difference between MPEG-4 AVC and MPEG-4 ASP]]**. See also the Wikipedia article about **[[wp>Advanced Video Coding]]**. See this page: **[[https://www.avidemux.org/admWiki/doku.php?id=general:common_myths|Common myths]]** to understand the differences between formats (standards) and codecs (pieces of software). Read also this simple page: **[[https://www.cyberlink.com/support/product-faq-content.do?id=1901|Difference between MPEG-4 AVC and MPEG-4 ASP]]**. See also the Wikipedia article about **[[wp>Advanced Video Coding]]**.
Line 469: Line 518:
         -x264-params 'force-cfr=1:keyint=8:bframes=0:ref=1' \         -x264-params 'force-cfr=1:keyint=8:bframes=0:ref=1' \
         output-video.mkv         output-video.mkv
 +</code>
 +
 +====== Problem in MKV Remux ======
 +
 +It seems there is a bug in ffmpeg **[[https://trac.ffmpeg.org/ticket/6037|#6037 mkv muxing not broken]]**: muxing two working files into a mkv produces a broken file: seeking around can break (mute) audio. I experienced this bug (with ffmpeg 4.1.6) trying to mux one mkv file containing one audio and one subtitle streams to another mkv file conaining video and audio. The resulting file did not play good in mplayer: seeking into the file caused audio or vido to stop playing.
 +
 +This was the first try command line:
 +
 +<code>
 +# The resulting video is broken.
 +ffmpeg -i input_file1.mkv -i input_file2.mkv \
 +    -map '0:v:0' -map '0:a:0' \
 +    -map '1:a:0' -map '1:s:0' \
 +    -codec:v copy -codec:a copy -codec:s copy \
 +    output_file.mkv
 +</code>
 +
 +The workaround was to extract each individual stream, and mux then together:
 +
 +<code>
 +ffmpeg -i input_file1.mkv -map 0:v:0 -codec:v copy input-v_env.mkv
 +ffmpeg -i input_file1.mkv -map 0:a:0 -codec:a copy input-a_ita.mkv
 +ffmpeg -i input_file2.mkv -map 0:a:0 -codec:a copy input-a_eng.mkv
 +ffmpeg -i input_file2.mkv -map 0:s:0 -codec:s copy input-s_eng.mkv
 +ffmpeg \
 +    -i input-v_env.mkv \
 +    -i input-a_ita.mkv \
 +    -i input-a_eng.mkv \
 +    -i input-s_eng.mkv \
 +    -codec:v copy -codec:a copy -codec:s copy \
 +    -map '0' -map '1' -map '2' -map '3' \
 +    output_file.mkv
 </code> </code>
  
doc/appunti/linux/video/ffmpeg.txt · Last modified: 2023/11/13 11:24 by niccolo