[FFmpeg-user] Preserve colour space automatically when changing pixel format

‍小太 nospam at kota.moe
Sat Sep 21 16:49:06 EEST 2024


(Cross-post from Stack Exchange:
https://video.stackexchange.com/questions/37660/ffmpeg-how-to-preserve-colour-space-automatically
)

I currently have a source video with the following properties:

Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuvj444p(pc,
> bt709/bt709/iec61966-2-1, progressive), 2560x1440 [SAR 1:1 DAR 16:9], 60
> fps, 60 tbr, 1k tbn


I want to transcode it into the following formats after applying cuts and
other effects:
 - H.264 YUV444 (yuvj444p)
 - H.264 YUV420 (yuvj420p)
 - AV1 YUV444 10-bit (yuv444p10le)
 - AV1 YUV420 10-bit (yuv420p10le)

However, except for the yuvj444p → yuvj444p transcode, the resulting files
have significantly different colours than the original when played back.
This seems to be because ffmpeg stripped the colour space information (to
`unknown`) from the output files:

$ ffmpeg -i source.mkv -c:v libx264 -preset veryslow -pix_fmt yuvj444p -crf
> 16 x264-444.mkv
> ...
> Output #0, matroska, to 'x264-444.mkv':
>   Stream #0:0: Video: h264 (H264 / 0x34363248), yuvj444p(pc,
> bt709/bt709/iec61966-2-1, progressive), 2560x1440 [SAR 1:1 DAR 16:9],
> q=2-31, 60 fps, 1k tbn
> $ ffmpeg -i source.mkv -c:v libx264 -preset veryslow -pix_fmt yuvj420p
> -crf 16 x264-420.mkv
> ...
> Output #0, matroska, to 'x264-420.mkv':
>   Stream #0:0: Video: h264 (H264 / 0x34363248), yuvj420p(pc,
> unknown/bt709/iec61966-2-1, progressive), 2560x1440 [SAR 1:1 DAR 16:9],
> q=2-31, 60 fps, 1k tbn
> $ ffmpeg -i source.mkv -c:v librav1e -speed 9 -tiles 24 -pix_fmt
> yuv444p10le -qp 64 rav1e-444.mkv
> ...
> Output #0, matroska, to 'rav1e-444.mkv':
>   Stream #0:0: Video: av1 (AV01 / 0x31305641),
> yuv444p10le(unknown/bt709/iec61966-2-1, progressive), 2560x1440 [SAR 1:1
> DAR 16:9], q=2-31, 60 fps, 1k tbn
> $ ffmpeg -i source.mkv -c:v libsvtav1 -preset 5 -svtav1-params
> tune=2:film-grain-denoise=0 -pix_fmt yuv420p10le -crf 16
> ...
> Output #0, matroska, to 'svtav1-420.mkv':
>   Stream #0:0: Video: av1 (AV01 / 0x31305641),
> yuv420p10le(unknown/bt709/iec61966-2-1, progressive), 2560x1440 [SAR 1:1
> DAR 16:9], q=2-31, 60 fps, 1k tbn


My current ffmpeg version is 7.0.2-3 (specifically Debian's 7:7.0.2-3).
The actual source video file is too large to include here, but I clipped
and cropped a small snippet of it here that demonstrates the same issue:
https://files.catbox.moe/q8oy1h.mp4

I can fix it by passing `-colorspace bt709` to ffmpeg, but that relies on
me already knowing the source video to be BT.709, so sounds error prone.
Is there a way to have ffmpeg automatically preserve the source video's
colour space?


More information about the ffmpeg-user mailing list