x264 Segmented Encoding Option to Prevent Artifacts

2014-11-10 12:00Edit this page

Recently I’ve been using x264’s 2-pass encoding to perform segmented encoding on the same file, then merging the segments. This enables multi-process transcoding and even distributed cluster transcoding to speed up the encoding process.

$ mencoder -really-quiet \
    /root/video/2159034.mp4 \
    -ss 0 -endpos 60 -of rawvideo \
    -ovc raw -nosound  -ofps 23  \
    -vf-add scale=404:720 \
    -vf-add format=i420 \
    -vf-add hqdn3d \
    -vf-add harddup \
    -o - 2>/dev/null | x264 \
    --no-progress --preset slower \
    --tune film --weightp 2 \
    --keyint 250 --min-keyint 25 \
    --non-deterministic --bframes 3 \
    --no-fast-pskip --qcomp 0.6 \
    --merange 24 --threads auto \
    --input-csp i420  --level 3.1 \
    --profile high --crf 20 \
    --input-res 404x720 \
    --fps 23 - --pass 1 \
    --stats \
    /tmp/video/1415072454_part1/3000/part_12159034/1415072454.passLog \
    -o /dev/null

$ mencoder -really-quiet \
    /root/video/2159034.mp4 \
    -ss 0 -endpos 60 -of rawvideo \
    -ovc raw -nosound  -ofps 23 \
    -vf-add scale=404:720 \
    -vf-add format=i420 \
    -vf-add hqdn3d \
    -vf-add harddup \
    -o - 2>/dev/null |x264 \
    --no-progress --preset slower \
    --tune film --weightp 2 \
    --keyint 250 --min-keyint 25 \
    --non-deterministic --bframes 3 \
    --no-fast-pskip --qcomp 0.6 \
    --merange 24 --threads auto \
    --input-csp i420  --level 3.1 \
    --profile high -B 3000 \
    --input-res 404x720 --fps 23 - --pass 2 \
    --stats /tmp/video/1415072454_part1/3000/part_12159034/1415072454.passLog \
    -o part1.264

However, I encountered a problem during transcoding. The individual .264 output files with MP4 container could play normally. But after merging the .264 files into one, seeking would cause visual artifacts.

$ MP4Box -add part1.264#video -cat part2.264 -fps 29.97 -add video.m4a#audio -new result.mp4

After re-transcoding multiple times with the same result, it became clear that simply appending files was causing the artifacts.

After Googling, I finally discovered that x264 by default optimizes the header of the output .264 file, which causes errors when merging .264 files. Simply adding the --stitchable option prevents the artifacts.

--stitchable        Don't optimize headers based on video content
                    Ensures ability to recombine a segmented encode

The result is as follows:

$ mencoder -really-quiet \
    /root/video/2159034.mp4 \
    -ss 0 -endpos 60 -of rawvideo \
    -ovc raw -nosound  -ofps 23  \
    -vf-add scale=404:720 \
    -vf-add format=i420 \
    -vf-add hqdn3d \
    -vf-add harddup \
    -o - 2>/dev/null | x264 \
    --no-progress --stitchable \
    --preset slower \
    --tune film --weightp 2 \
    --keyint 250 --min-keyint 25 \
    --non-deterministic --bframes 3 \
    --no-fast-pskip --qcomp 0.6 \
    --merange 24 --threads auto \
    --input-csp i420  --level 3.1 \
    --profile high --crf 20 \
    --input-res 404x720 \
    --fps 23 - --pass 1 \
    --stats \
    /tmp/video/1415072454_part1/3000/part_12159034/1415072454.passLog \
    -o /dev/null

$ mencoder -really-quiet \
    /root/video/2159034.mp4 \
    -ss 0 -endpos 60 -of rawvideo \
    -ovc raw -nosound  -ofps 23 \
    -vf-add scale=404:720 \
    -vf-add format=i420 \
    -vf-add hqdn3d \
    -vf-add harddup \
    -o - 2>/dev/null |x264 \
    --no-progress --stitchable \
    --preset slower \
    --tune film --weightp 2 \
    --keyint 250 --min-keyint 25 \
    --non-deterministic --bframes 3 \
    --no-fast-pskip --qcomp 0.6 \
    --merange 24 --threads auto \
    --input-csp i420  --level 3.1 \
    --profile high -B 3000 \
    --input-res 404x720 --fps 23 - --pass 2 \
    --stats /tmp/video/1415072454_part1/3000/part_12159034/1415072454.passLog \
    -o part1.264

Unless otherwise stated, articles on this blog are licensed under the Creative Commons Attribution 4.0 International License. Please credit the original author and source when sharing.


Tags: archive

No comments yet

Leave a comment

Creative Commons © 2013 — 2025 xiaocang | Theme based on fzheng.me & NexT | Hosted by Netlify