byjjg Video Encoding Guides

website·Indexed 31 days ago

https://guides.byjjg.fyi

Comprehensive guides on video encoding, audio, and muxing. Includes instructions for FFmpeg installation, CPU-based AV1 encoding with SVT-AV1, NVIDIA GPU-accelerated AV1 encoding, VVC encoding with libvvenc, and metadata remuxing.

Tokens
4.8K
Snippets
17
Records
34
Agent score
50%

What's inside byjjg Guides

  1. Install FFmpeg on Windows

    1. Download the latest git-full version (ffmpeg-git-full.7z) from the gyan.dev builds link on the FFmpeg download page.
    2. Extract ffmpeg.exe, ffplay.exe, and ffprobe.exe to a permanent directory (e.g., C:\FFmpeg).
    3. Add the folder path containing ffmpeg.exe to the system-wide 'Path' environment variable: Start Menu → 'Edit the system environment variables' → Environment Variables → System variables → Path → Edit → New → Enter folder path.
    4. Verify installation by opening Command Prompt (cmd) and typing ffmpeg to check the version and available libraries (e.g., av1_nvenc).
  2. Mux and remux videos with MKVToolNix

    MKVToolNix GUI is used for managing tracks, adding attachments, and creating chapters in Matroska (.mkv) files.

    • Streams: Right-click in the multiplexer to add tracks or uncheck them to exclude them.
    • Cover Art: Go to Multiplexer → Attachments and add a JPEG image named cover.jpg.
    • Blu-ray Chapters: Drag a .mpls file into the Source files window, then go to the Output tab → Chapters sub-tab and click Generate chapters.
    • Manual Chapters: Use the Chapter Editor on the sidebar. To find precise timestamps, use LosslessCut to locate the exact keyframe (down to .001s) before entering the time in MKVToolNix.
  3. Preserve HDR metadata in FFmpeg and Av1an

    To preserve HDR metadata, ensure the output matches the input's color primaries and transfer characteristics. In mpv player, press Shift + I to verify playback metadata (pix_fmt, color_primaries).

    Using FFmpeg: Use -map_metadata 0 to copy existing HDR metadata.

    Using Av1an: You must manually specify HDR values. First, check the chroma sample position using ffprobe. If unspecified, use topleft (the HDR10 standard).

    # Check chroma sample position
    ffprobe -show_streams -select_streams v:0 4k_HDR_input.m2ts
    
    # Av1an example with HDR metadata
    av1an -i input.mkv -e svt-av1 -v "--crf 22 --preset 3 --enable-qm 1 --qm-min 5 --chroma-qm-min 5 --keyint 320 --ac-bias 1.0 --tune 0 --chroma-sample-position topleft --color-primaries bt2020 --transfer-characteristics smpte2084 --matrix-coefficients bt2020-ncl --mastering-display G(0.170000,0.790000)B(0.131000,0.046000)R(0.708000,0.292000)WP(0.312700,0.329000)L(1000.000000,0.000100) --content-light 501,230" -w 6 -m lsmash -c ffmpeg -l log -a "-an" -o "4k HDR output.mkv"
  4. Encode SVT-AV1 using FFmpeg only

    For a simpler setup without Av1an, SVT-AV1 can be encoded directly via FFmpeg. This method is slower because it does not utilize chunked parallel encoding, but it is easier to install and start. The following example uses a high-quality preset and specific SVT-AV1 parameters for tuning.
    ffmpeg -y -i input.mkv -map 0 -c:v libsvtav1 -preset 3 -crf 22 -svtav1-params "tune=0:enable-qm=1:qm-min=5:chroma-qm-min=5:keyint=320:ac-bias=1.0" -an encoded_output.mkv
  5. Install and Configure Av1an on Windows

    Av1an uses chunk-based parallel encoding to improve CPU-based encoding speed.

    1. Download the latest pre-release build of av1an.exe from the GitHub releases page.
    2. Place av1an.exe in a permanent folder (e.g., C:\Av1an).
    3. Add the av1an.exe folder path to the system-wide 'Path' environment variable.
    4. Requirements: Place the following in the Av1an folder:
      • Encoder: SvtAv1EncApp.exe and SvtAv1Enc.dll (recommended to build SVT-AV1 from source).
      • FFmpeg: A 'full shared' build of FFmpeg that includes .dll dependencies.
      • Optional: mkvmerge.exe (from MKVToolNix) for file concatenation instead of FFmpeg.
    # To obtain SVT-AV1 source for building
    git clone https://gitlab.com/AOMediaCodec/SVT-AV1.git
  6. Export and verify remuxed videos with MKVToolNix and MediaInfo

    After configuring streams and metadata in MKVToolNix, export the finished video into an MKV container. Once exported, use MediaInfo to verify that the new contents and metadata are correct.
  7. Select an audio format for stereo and surround sound

    Choose an audio codec based on the channel layout and target quality/bitrate requirements:

    • Opus: Open-source. Recommended for stereo (64–128 kbps), 5.1 surround (192–256 kbps), and 7.1 surround (320–448 kbps).
    • Dolby Digital Plus (E-AC-3): Ideal for 5.1 and 7.1 sources, especially for AV receivers. Larger files than Opus but high quality.
    • FDK-AAC: Best for 2-channel TV audio at bitrates > 160 kbps.
    • xHE-AAC: Open-source; outperforms Opus at very low bitrates (24–80 kbps stereo).
    • CoreAudio AAC (QAAC64): Best for music encoding at 160 kbps and higher.
    • MPEG-H Audio: High quality/compression for 5.1/7.1 when paired with VVC, but lacks support for MKV or mpv playback.
    • AC-4: High efficiency (half the bitrate of E-AC-3), but requires specific ffmpeg builds for mpv playback.
  8. Create a lossless test clip for encoding

    Before encoding a full-length video, create a 10-second test clip containing both low and high motion scenes. Because videos can only be losslessly trimmed at keyframe points, use tools like LosslessCut (preferred) or Avidemux to perform this task quickly.
  9. Install FFmpeg on Arch Linux

    FFmpeg can be installed on Arch Linux via the official repositories, the AUR for bleeding-edge git versions, or by building from source.
    # Official Arch build
    sudo pacman -S ffmpeg
    
    # Bleeding-edge (Git) build via yay
    yay -S ffmpeg-git
    
    # Build FFmpeg Git from AUR manually
    git clone https://aur.archlinux.org/ffmpeg-full-git.git
    cd ffmpeg-full-git
    makepkg -si
  10. Install FFmpeg on macOS

    1. Download the latest git versions of ffmpeg, ffprobe, and ffplay binaries.
    2. Move the binaries to /usr/local/bin/ and set execution permissions using the terminal.
    3. Verify installation by opening Terminal (Command + Space → 'Terminal') and typing ffmpeg to check the version and available libraries.
    sudo mv ~/Downloads/ffmpeg /usr/local/bin/
    sudo mv ~/Downloads/ffprobe /usr/local/bin/
    sudo mv ~/Downloads/ffplay /usr/local/bin/
    
    sudo chmod +x /usr/local/bin/ffmpeg
    sudo chmod +x /usr/local/bin/ffprobe
    sudo chmod +x /usr/local/bin/ffplay
  11. Encode video to VVC (H.266) using libvvenc

    The VVenC encoder (libvvenc) provides high-efficiency compression for VVC/H.266. It is particularly effective at analyzing and synthesizing film grain automatically, removing the need for external denoising or manual grain settings. CPU-based encoding with libvvenc offers superior compression efficiency compared to hardware-accelerated alternatives.
    ffmpeg -i input.mkv -map 0 -c:v libvvenc -preset medium -qp 30 -vvenc-params Threads=22:MTProfile=2:fga=1 -an encoded_output.mkv