User Tools

Site Tools


doc:appunti:linux:video:ffmpeg_final_rendering

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
doc:appunti:linux:video:ffmpeg_final_rendering [2023/11/13 11:22] – [Encoding for High Definition Video] niccolodoc:appunti:linux:video:ffmpeg_final_rendering [2024/07/31 09:57] (current) – [Two pass encoding] niccolo
Line 1: Line 1:
-====== Final Rendering in x264 with ffmpeg ======+====== Final Rendering with libx264 and ffmpeg ====== 
 + 
 +Whether you are working with **Ffmepg**, **Avidemux** or **Olive Video Editor**, you should be wondering what parameters to use to final render a video montage. Here are my choices for some specific purpose.
  
 ===== Encoding for High Definition Video ===== ===== Encoding for High Definition Video =====
Line 13: Line 15:
 ^ 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 4096 kb/s (about 1.8 Gb per hour)  | ^ Video encoding    | Average Bitrate (Two Pass), Average Bitrate 4096 kb/s (about 1.8 Gb per hour)  |
-^ Pixel format      |  | +^ Pixel format      | Our source videos use **yuvj420p pixel format**, so we use the same for the final rendering (see the considerations below).  | 
-^ Bits per channel  |  | +^ Bits per sample   Our source videos use **8 bits per raw sample**, so we stay on this.  | 
-^ Color range        |+^ Color range       We use the **full range** of colors. The **j** letter in the //yuvj420p// pixel format means that YUV values are stored as full 0-255 range (8 bits), as in JPEG. This is better than the so called //studio swing//, with Y in the 16-235 range and UV in the 16-240 range.  |
 ^ 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)  |
Line 33: Line 35:
     -filter:v "scale=1366x768" -aspect "16:9" \     -filter:v "scale=1366x768" -aspect "16:9" \
     -vcodec 'libx264' -pix_fmt 'yuvj420p' -preset 'veryslow' -tune 'film' -profile:v 'high' -level:v 5 \     -vcodec 'libx264' -pix_fmt 'yuvj420p' -preset 'veryslow' -tune 'film' -profile:v 'high' -level:v 5 \
 +    -b:v 4M -maxrate:v 6M -bufsize:v 6M \
     -acodec copy \     -acodec copy \
     "2022-05_balcani.mkv"     "2022-05_balcani.mkv"
 </code> </code>
  
 +Notice that we selected a target bitrate of **4 Mbps** and a maximum bitrate of **6 Mbps**. Sepcifying both the //bitrate// and the //max bitrate tolerance// requires also to specify a //buffer size//; ''ffmpeg'' will re-adjust the bitrate on each buffer size, so if the buffer size is equal to the max rate, ffmpeg will re-calculate the bitrate at least every second.
 +
 +=====  A warning about the H.264 profile level =====
 +
 +It turned out that **Kodi 19.4** (running on a Raspberry Pi 4 with **Raspbian GNU/Linux 11 Bullseye** o.s.) has some problems playing H.264 video files depending on the video format profile used for the encoding. The video actually starts to play, but nothing is seen on the screen; sometimes the screen goes totally green. Similarly a **Google TV** with Kodi 21.0 has problems with the same files, showing stuttering and video artifacts.
 +
 +Here it is a table of playing capabilities based on experimental testing:
 +
 +^ Format profile  ^ Resolution   ^ Mbps  ^ Can play on Kodi  ^
 +| High@L5         | 1366x768        4 | yes  |
 +| High@L4         | 1920x1080    |    6 | Yes  |
 +| High@L4.1       | 1920x1080    |    6 | Yes  |
 +| High@5          | 1920x1080    |    4 | No   |
 +| High@5          | 1920x1080    |    6 | No   |
 +
 +=====  Two pass encoding =====
 +
 +Here it is a recipe of **two-pass encoding** from an high quality //master// produced with [[olive_editor_tools#export_media|Olive Video Editor]]. The target video will be a **6 Mbit** stream, **1920x1080@30**. The profile level selected is **High@L4.1**, so that the video will play nicely with Kodi 19.4 on the Raspberry Pi 4:
 +
 +<code bash>
 +TITLE='Marocco - Maggio 2024'
 +ffmpeg \
 +    -i full32bit-veryslow-crf18-fullcolor.mkv \
 +    -metadata title="$TITLE" -metadata:s:v:0 title="$TITLE" \
 +    -vcodec 'libx264' -pix_fmt 'yuvj420p' -preset 'veryslow' -tune 'film' \
 +    -profile:v 'high' -level:v 4.1 \
 +    -b:v 6M -maxrate:v 9M -bufsize:v 18M \
 +    -x264-params 'keyint=64' \
 +    -an \
 +    -pass 1 \
 +    -f matroska -y /dev/null
 +# First pass produces two files: ffmpeg2pass-0.log and ffmpeg2pass-0.log.mbtree.
 +ffmpeg \
 +    -i full32bit-veryslow-crf18-fullcolor.mkv \
 +    -metadata title="$TITLE" -metadata:s:v:0 title="$TITLE" \
 +    -vcodec 'libx264' -pix_fmt 'yuvj420p' -preset 'veryslow' -tune 'film' \
 +    -profile:v 'high' -level:v 4.1 \
 +    -b:v 6M -maxrate:v 9M -bufsize:v 18M \
 +    -x264-params 'keyint=64' \
 +    -an \
 +    -pass 2 \
 +    -f matroska marocco-twopass-6max9-level41.mkv
 +</code>
 +
 +The transcoding works on the video only, suppressing any audio stream (**%%-an%%** option). Using the **keyint=64** option will produce a keyframe every 64 frames (about two seconds), this make seeking back and forward in the video more smooth.
 +
 +=====  Pixel format considerations =====
 +
 +We used the **yuvj420p pixel format**. What does it means?
 +
 +First of all consider the **420** code; this means tat for each matrix of 4x2 pixels the stream encode all the values for the luminance, only 2 values for the chrominance on the X axis and zero values for the chrominance on the Y axis. This figure explains clearly the 4:4:4, 4:2:2 and 4:2:0 subsampling methods:
 +
 +{{subsampling.png?400|Chroma Subsampling}}
 +
 +Is file size is not a concern, we might ask ourselves whether a pixel format with less loss of chroma information would be preferable. Obviously is useless to add more chroma information if the final rendering has the same resolution of the original video, but if we are **scaling down** the video resolution we may **retain chroma information** using a different pixel format.
 +
 +The fact is that **in movies 4:2:0 is almost lossless visually**, which is why it can be found used in Blu-ray discs and a lot of modern video cameras. There is virtually no advantage to using 4:4:4 for consuming video content.
 +
 +Furthermore, it may happen that some players (software or hardware) are not compatible with pixel formats other than 4:2:0.
 +
 +===== How to probe a video =====
 +
 +How to get the **piexel format**:
 +
 +<code>
 +ffprobe -loglevel error \
 +    -show_entries stream=pix_fmt \
 +    -select_streams v YDXJ4050.mp4
 +</code>
 +
 +How to get the **bits per raw sample**:
 +
 +<code>
 +ffprobe -loglevel panic \
 +    -show_entries stream=bits_per_raw_sample \
 +    -select_streams v YDXJ4050.mp4 
 +</code>
 +
 +===== Web Resources =====
 +
 +  * **[[https://stackoverflow.com/questions/32829514/which-pixel-format-for-web-mp4-video|Which pixel format for web mp4 video?]]**
 +  * **[[https://stackoverflow.com/questions/56829755/using-ffmpeg-or-ffprobe-to-get-the-pixel-bit-depth-of-a-video|Using ffmpeg or ffprobe to get the pixel bit depth of a video]]**
 +  * **[[https://www.rtings.com/tv/learn/chroma-subsampling|Chroma Subsampling]]**
 +  * **[[https://en.wikipedia.org/wiki/YCbCr|YCbCr]]**
  
doc/appunti/linux/video/ffmpeg_final_rendering.1699870943.txt.gz · Last modified: by niccolo