master-of-zen / Av1an

Cross-platform command-line AV1 / VP9 / HEVC / H264 encoding framework with per scene quality encoding
GNU General Public License v3.0
1.51k stars 155 forks source link

x265-encoded video flickers on mac OS / Safari native codec #218

Open kripod opened 3 years ago

kripod commented 3 years ago

I’ve just encoded a lossless video with the following media information (gathered via ffprobe):

  Duration: 00:00:45.00, start: 0.000000, bitrate: 717448 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p(tv, progressive), 1920x1080, 60 fps, 60 tbr, 1k tbn, 120 tbc (default)
    Metadata:
      ENCODER         : Lavc58.117.101 libx264
      DURATION        : 00:00:45.000000000
    Stream #0:1: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s (default)
    Metadata:
      DURATION        : 00:00:45.000000000

And with the following commands to aim for Safari compatibility:

av1an --input source.mkv --target_quality 85 --vmaf_path ~/Downloads/vmaf_float_v0.6.1.pkl --chunk_method vs_ffms2 --output video-2-tmp.mkv --encoder x265 --audio_params="-c:a libfdk_aac -profile:a aac_he_v2 -vbr 3"
ffmpeg -i video-2-tmp.mkv -c:v copy -tag:v hvc1 -c:a copy video-2.mp4

Unfortunately, the result below:

https://user-images.githubusercontent.com/14854048/105613789-27a50a00-5dc5-11eb-8fef-596503113e92.mp4

Flickers when played back in Safari:

https://user-images.githubusercontent.com/14854048/105613913-eb25de00-5dc5-11eb-99b7-0072a38ed37f.mov

I suspect this has something to do with the yuv444p color profile of the source (as the output --pix_format is yuv420p10le by default), but I may be totally wrong on that.

When encoding the source via ffmpeg (without prior processing through Av1an), the following command produces acceptable results, but at twice the file size:

ffmpeg -i source.mkv -c:v libx265 -tag:v hvc1 -profile:v main10 -pix_fmt yuv420p10le -x265-params crf=25 -c:a libfdk_aac -profile:a aac_he_v2 -vbr 3 video-2.mp4
Felixkruemel commented 3 years ago

First of all in you bottom ffmpeg command don't use -x265-params to specify crf, just use -crf directly. Then also set -preset slow, the medium preset is way worse efficiency wise and that's why the file is so large.

In av1an just try specifying another pix fmt

master-of-zen commented 3 years ago

@kripod plays just fine with mpv. That issue is not related to pix_formats

BlueSwordM commented 3 years ago

Yeah, I think it's the decoder really not liking yuv444p or something.

kripod commented 3 years ago

@master-of-zen The result also plays fine with other media players like IINA, that’s why I’m surprised by the decoding issue within Safari.

@Felixkruemel Thank you for the tip! Honestly, I’m a bit confused and overwhelmed by all these options available within the various software used for video processing. I would appreciate some clarification about where each option should be passed. (I feel like I’m not confident about the separation between FFmpeg, codecs and Av1an itself.

@BlueSwordM Do you have an idea about how this might be resolved? I would like to keep the input source without loss (in yuv444p), but the output should be emitted in yuv420p10le.

BlueSwordM commented 3 years ago

Actually, it is 4:2:0, so something must be up with the video stream, or Safari,

kripod commented 3 years ago

@BlueSwordM As I’ve mentioned, the source video stream is a lossless h264 file encoded in yuv444p. Not sure if that gets processed as it’s intended to be. What’s more interesting is that many media players play the resulting video just fine. Really not sure about what’s going on with Safari, but that’s the main target I would like to play the compressed video on.

kripod commented 3 years ago

Specifying --mkvmerge as an option solves the problem. Now sure why FFmpeg-based concatenation causes problems, but the issue possibly lies inside concat.py’s concatenate_ffmpeg method, then.