英文:
ffmpeg overlay png on video has color issue
问题
尝试使用简单的叠加命令将标志放在黑色视频上,但发现输出中颜色略有不同,标志绿色部分的原始RGB值为(0,220,90),但在输出中变为(0,191,88)。看起来颜色与黑色背景混合在一起了,有什么想法?
原始标志RGB:
标志叠加到视频上后的RGB:
叠加的命令:
ffmpeg -i video.mp4 -i logo.png -filter_complex "[1:v]format=rgba展开收缩,[0:v]展开收缩overlay=240:1275:format=auto" output.mp4
英文:
Im trying a simple overlay command to put a logo on a black video, but find the color a little different in output, the original RGB value of green part of the logo is (0,220,90), but changed to (0,191,88) in output. Looks like the color gets mixed with the black background, any ideas?
original logo rgb:
rgb after logo overlayed on video:
the command to overlay:
ffmpeg -i video.mp4 -i logo.png -filter_complex "[1:v]format=rgba展开收缩,[0:v]展开收缩overlay=240:1275:format=auto" output.mp4
▶ ffmpeg -i logo.png
ffmpeg version 4.4-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 8 (Debian 8.3.0-6)
configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
libavutil 56. 70.100 / 56. 70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58. 76.100 / 58. 76.100
libavdevice 58. 13.100 / 58. 13.100
libavfilter 7.110.100 / 7.110.100
libswscale 5. 9.100 / 5. 9.100
libswresample 3. 9.100 / 3. 9.100
libpostproc 55. 9.100 / 55. 9.100
Input #0, png_pipe, from 'logo.png':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: png, rgba(pc), 601x81 [SAR 2834:2834 DAR 601:81], 25 fps, 25 tbr, 25 tbn, 25 tbc
▶ ffmpeg -i video.mp4
ffmpeg version 4.4-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 8 (Debian 8.3.0-6)
configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
libavutil 56. 70.100 / 56. 70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58. 76.100 / 58. 76.100
libavdevice 58. 13.100 / 58. 13.100
libavfilter 7.110.100 / 7.110.100
libswscale 5. 9.100 / 5. 9.100
libswresample 3. 9.100 / 3. 9.100
libpostproc 55. 9.100 / 55. 9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.76.100
Duration: 00:00:02.00, start: 0.000000, bitrate: 24 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1080x1920, 18 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
答案1
得分: 2
以下是您要翻译的内容:
问题与颜色格式有关。
FFmpeg的默认YUV颜色标准是BT.601 - FFmpeg使用BT.601颜色转换公式从RGB转换为YUV。
FFmpeg使用BT.601,但不将H.264流标记为BT.601(FFmpeg默认未设置视频流的颜色格式元数据)。
据我所知,视频播放器假定颜色标准是BT.709,因为视频是高清的(超过720条视频线),并且高清视频的推荐标准是BT.709(不同的播放器可能不同)。
视频播放器使用BT.709颜色转换公式从YUV转换为RGB,因此我们得到了颜色差异。
我建议您将视频转换为BT.709,并将视频标记为BT.709:
-
使用
scale
滤镜将视频转换为BT.709:
scale=out_color_matrix=bt709:out_range=tv
-
使用bsf滤镜将视频标记为BT.709 "TV range"(有限范围):
-bsf:v h264_metadata=video_full_range_flag=0:colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1
还建议指定libx264编码器,并设置crf
和pix_fmt
参数:
ffmpeg -y -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=240:1275:format=rgb,scale=out_color_matrix=bt709:out_range=tv" -vcodec libx264 -crf 17 -pix_fmt yuv420p -bsf:v h264_metadata=video_full_range_flag=0:colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 output.mp4
仍然存在由颜色转换舍入引起的小不准确性。
为了防止任何舍入错误,我们可以使用libx264rgb编码器,但视频可能不太便携。
我们可以使用MediaInfo工具检查颜色标准(元数据):
Color range : Limited
Color primaries : BT.709
Transfer characteristics : BT.709
Matrix coefficients : BT.709
英文:
The issue is related to color formats.
FFmpeg default YUV color standard is BT.601 - FFmpeg converts from RGB to YUV using BT.601 color conversion formula.
FFmpeg uses BT.601 but doesn't mark the H.264 stream as BT.601 (FFmpeg default is not setting the color format metadata of the video stream).
As far as I know, the video player assumes the color standard is BT.709, because the video is HD (more than 720 video lines), and the recommended standard for HD video is BT.709 (it may be different in different players).
The video player converts from YUV to RGB using BT.709 color conversion formula, and we are getting the color differences.
I recommend you to convert the video to BT.709, and mark the video as BT.709:
-
Convert the video to BT.709 using
scale
filter:
scale=out_color_matrix=bt709:out_range=tv
-
Mark the video as BT.709 "TV range" (Limited range) using bsf filter:
-bsf:v h264_metadata=video_full_range_flag=0:colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1
It's also recommended to specify libx264 encoder, and set the crf
and pix_fmt
arguments:
ffmpeg -y -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=240:1275:format=rgb,scale=out_color_matrix=bt709:out_range=tv" -vcodec libx264 -crf 17 -pix_fmt yuv420p -bsf:v h264_metadata=video_full_range_flag=0:colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 output.mp4
There are still minor inaccuracies due color conversion rounding.
For preventing any rounding error, we may use libx264rgb encoder, but the video my not be very portable.
output.mp4
played with MPC-HC media player:
output.mp4
played with VLC media player:
We may check the color standard (metadata) using MediaInfo tool:
Color range : Limited
Color primaries : BT.709
Transfer characteristics : BT.709
Matrix coefficients : BT.709
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论