在无损模式下使用h264会带来小的意外结果


11

我对ffmpeg屏幕捕获功能感到好奇,然后开始在h264中进行简单的实时捕获测试。

ffmpeg -f dshow -i video="screen-capture-recorder" -video_size 1920x1080 -framerate 30 -c:v libx264 -crf 0 -preset ultrafast capture.mkv

根据ffmpeg h264上所说的内容,使用-qp 0-crf 0选项libx264应该可以在无损模式下工作。

您可以使用-qp 0或-crf 0编码无损输出。建议使用-qp而不是-crf进行无损,因为8位和10位x264使用不同的-crf值进行无损。

在谈论使用较慢的预设尝试尝试保存一些大小的可选重新编码时,实时捕获部分的帮助中也会重复此操作:

注意,由于初始记录是无损的,并且重新编码也是无损的,因此在此过程中不会以任何方式引入质量损失。

基于此,我相信该指南,并假设使用-qp 0可以实现完全无损的工作流程;)

但是我发现它在特定情况下会带来一些损失。

因此,我使用此代码对huffyuv编解码器进行了另一个测试:

ffmpeg -f dshow -i video="screen-capture-recorder" -video_size 1920x1080 -framerate 30 -c:v huffyuv capture.mkv

结果:

屏幕截图以h264编码 屏幕截图以huffyuv编码

屏幕1:无损模式下的h264
屏幕2:huffyuv

基于屏幕huffyuv是完美的,一个真正的无损编解码器却h264在这里压缩了一些内容,我无法理解为什么应将其设置为无损模式。

(huffyuv等同于桌面的位图屏幕截图,使用h264可以实现相同的效果)

有人可以帮我弄清楚吗?


编辑:根据需要添加一些ffmpeg转储;)

h264运行:

ffmpeg -f dshow -i video="screen-capture-recorder" -video_size 1920x1080 -framerate 30 -c:v libx264 -qp 0 -preset ultrafast capture.mkv  
ffmpeg version N-73411-g5233f25 Copyright (c) 2000-2015 the FFmpeg developers  
  built with gcc 4.9.2 (GCC)  
  configuration: --arch=x86_64 --target-os=mingw32 --cross-prefix=/home/user/san  
dbox/mingw-w64-x86_64/bin/x86_64-w64-mingw32- --pkg-config=pkg-config --enable-g  
pl --enable-libsoxr --enable-fontconfig --enable-libass --enable-libutvideo --en  
able-libbluray --enable-iconv --enable-libtwolame --extra-cflags=-DLIBTWOLAME_ST  
ATIC --enable-libzvbi --enable-libcaca --enable-libmodplug --extra-libs=-lstdc++  
 --extra-libs=-lpng --enable-libvidstab --enable-libx265 --enable-decklink --ext  
ra-libs=-loleaut32 --enable-libx264 --enable-libxvid --enable-libmp3lame --enabl  
e-version3 --enable-zlib --enable-librtmp --enable-libvorbis --enable-libtheora  
--enable-libspeex --enable-libopenjpeg --enable-gnutls --enable-libgsm --enable-  
libfreetype --enable-libopus --disable-w32threads --enable-frei0r --enable-filte  
r=frei0r --enable-libvo-aacenc --enable-bzlib --enable-libxavs --enable-libopenc  
ore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-libschroed  
inger --enable-libvpx --enable-libilbc --enable-libwavpack --enable-libwebp --en  
able-libgme --enable-dxva2 --enable-libdcadec --enable-avisynth --enable-static  
--disable-shared --extra-cflags= --prefix=/home/user/sandbox/mingw-w64-x86_64/x8  
6_64-w64-mingw32 --extra-libs=-lpsapi --enable-nonfree --enable-libfdk-aac --dis  
able-libfaac --enable-nvenc --enable-runtime-cpudetect  
  libavutil      54. 28.100 / 54. 28.100  
  libavcodec     56. 46.101 / 56. 46.101  
  libavformat    56. 40.100 / 56. 40.100  
  libavdevice    56.  4.100 / 56.  4.100  
  libavfilter     5. 20.100 /  5. 20.100  
  libswscale      3.  1.101 /  3.  1.101  
  libswresample   1.  2.100 /  1.  2.100  
  libpostproc    53.  3.100 / 53.  3.100  
leaving aero onInput #0, dshow, from 'video=screen-capture-recorder':  
  Duration: N/A, start: 362931.503000, bitrate: N/A  
    Stream #0:0: Video: rawvideo, bgr0, 1920x1080, 30 tbr, 10000k tbn, 30 tbc  
No pixel format specified, yuv444p for H.264 encoding chosen.  
Use -pix_fmt yuv420p for compatibility with outdated media players.  
[libx264 @ 00000000004c7e00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2  
AVX FMA3 AVX2 LZCNT BMI2  
[libx264 @ 00000000004c7e00] profile High 4:4:4 Predictive, level 4.0, 4:4:4 8-b  
it  
[libx264 @ 00000000004c7e00] 264 - core 144 r2533 c8a773e - H.264/MPEG-4 AVC cod  
ec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 r  
ef=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=0 mixed_ref=0 me_range=16 chro  
ma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=0 chroma_qp_offset=0  
threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 blur  
ay_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 sce  
necut=0 intra_refresh=0 rc=cqp mbtree=0 qp=0  
Output #0, matroska, to 'capture.mkv':  
  Metadata:  
    encoder         : Lavf56.40.100  
    Stream #0:0: Video: h264 (libx264) (H264 / 0x34363248), yuv444p, 1920x1080,  
q=-1--1, 30 fps, 1k tbn, 30 tbc  
    Metadata:  
      encoder         : Lavc56.46.101 libx264  
Stream mapping:  
  Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))  
Press [q] to stop, [?] for help  
frame=   11 fps=0.0 q=0.0 size=    2421kB time=00:00:00.20 bitrate=99163.6kbits/  
frame=   22 fps= 22 q=0.0 size=    2538kB time=00:00:00.73 bitrate=28368.6kbits/  
frame=   33 fps= 22 q=0.0 size=    2647kB time=00:00:01.26 bitrate=17116.4kbits/  
frame=   46 fps= 23 q=0.0 size=    2770kB time=00:00:01.80 bitrate=12608.4kbits/  
frame=   58 fps= 23 q=0.0 size=    2842kB time=00:00:02.23 bitrate=10427.1kbits/  
frame=   71 fps= 23 q=0.0 size=    2908kB time=00:00:02.80 bitrate=8508.6kbits/s  
frame=   83 fps= 23 q=0.0 size=    2977kB time=00:00:03.26 bitrate=7467.0kbits/s  
frame=   96 fps= 24 q=0.0 size=    3085kB time=00:00:03.80 bitrate=6649.8kbits/s  
frame=  108 fps= 24 q=0.0 size=    3195kB time=00:00:04.30 bitrate=6084.5kbits/s  
frame=  120 fps= 24 q=0.0 size=    3309kB time=00:00:04.80 bitrate=5645.8kbits/s  
frame=  133 fps= 24 q=0.0 size=    3398kB time=00:00:05.33 bitrate=5219.0kbits/s  
frame=  147 fps= 24 q=0.0 size=    3492kB time=00:00:05.86 bitrate=4876.1kbits/s  
frame=  160 fps= 24 q=0.0 size=    3568kB time=00:00:06.36 bitrate=4591.4kbits/s  
frame=  173 fps= 24 q=0.0 size=    3660kB time=00:00:06.86 bitrate=4366.2kbits/s  
frame=  186 fps= 25 q=0.0 size=    3720kB time=00:00:07.36 bitrate=4136.5kbits/s  
frame=  187 fps= 24 q=-1.0 Lsize=    3737kB time=00:00:07.63 bitrate=4010.9kbits  
/s  
video:3735kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing  
overhead: 0.052628%  
[libx264 @ 00000000004c7e00] frame I:1     Avg QP: 0.00  size:1345491  
[libx264 @ 00000000004c7e00] frame P:186   Avg QP: 0.00  size: 13327  
[libx264 @ 00000000004c7e00] mb I  I16..4: 100.0%  0.0%  0.0%  
[libx264 @ 00000000004c7e00] mb P  I16..4: 16.5%  0.0%  0.0%  P16..4:  0.3%  0.0  
%  0.0%  0.0%  0.0%    skip:83.3%  
[libx264 @ 00000000004c7e00] coded y,u,v intra: 6.1% 3.8% 3.9% inter: 0.2% 0.1%  
0.1%  
[libx264 @ 00000000004c7e00] i16 v,h,dc,p: 96%  4%  0%  0%  
[libx264 @ 00000000004c7e00] kb/s:4008.11  
[dshow @ 00000000004bf760] real-time buffer [screen-capture-recorder] [video inp  
ut] too full or near too full (545% of size: 3041280 [rtbufsize parameter])! fra  
me dropped!  
Exiting normally, received signal 2.  
Terminate batch job (Y/N)?

huffyuv运行:

ffmpeg -f dshow -i video="screen-capture-recorder" -video_size 1920x1080 -framerate 30 -c:v huffyuv capture.mkv  
ffmpeg version N-73411-g5233f25 Copyright (c) 2000-2015 the FFmpeg developers  
  built with gcc 4.9.2 (GCC)  
  configuration: --arch=x86_64 --target-os=mingw32 --cross-prefix=/home/user/san  
dbox/mingw-w64-x86_64/bin/x86_64-w64-mingw32- --pkg-config=pkg-config --enable-g  
pl --enable-libsoxr --enable-fontconfig --enable-libass --enable-libutvideo --en  
able-libbluray --enable-iconv --enable-libtwolame --extra-cflags=-DLIBTWOLAME_ST  
ATIC --enable-libzvbi --enable-libcaca --enable-libmodplug --extra-libs=-lstdc++  
 --extra-libs=-lpng --enable-libvidstab --enable-libx265 --enable-decklink --ext  
ra-libs=-loleaut32 --enable-libx264 --enable-libxvid --enable-libmp3lame --enabl  
e-version3 --enable-zlib --enable-librtmp --enable-libvorbis --enable-libtheora  
--enable-libspeex --enable-libopenjpeg --enable-gnutls --enable-libgsm --enable-  
libfreetype --enable-libopus --disable-w32threads --enable-frei0r --enable-filte  
r=frei0r --enable-libvo-aacenc --enable-bzlib --enable-libxavs --enable-libopenc  
ore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-libschroed  
inger --enable-libvpx --enable-libilbc --enable-libwavpack --enable-libwebp --en  
able-libgme --enable-dxva2 --enable-libdcadec --enable-avisynth --enable-static  
--disable-shared --extra-cflags= --prefix=/home/user/sandbox/mingw-w64-x86_64/x8  
6_64-w64-mingw32 --extra-libs=-lpsapi --enable-nonfree --enable-libfdk-aac --dis  
able-libfaac --enable-nvenc --enable-runtime-cpudetect  
  libavutil      54. 28.100 / 54. 28.100  
  libavcodec     56. 46.101 / 56. 46.101  
  libavformat    56. 40.100 / 56. 40.100  
  libavdevice    56.  4.100 / 56.  4.100  
  libavfilter     5. 20.100 /  5. 20.100  
  libswscale      3.  1.101 /  3.  1.101  
  libswresample   1.  2.100 /  1.  2.100  
  libpostproc    53.  3.100 / 53.  3.100  
leaving aero onInput #0, dshow, from 'video=screen-capture-recorder':  
  Duration: N/A, start: 362514.497000, bitrate: N/A  
    Stream #0:0: Video: rawvideo, bgr0, 1920x1080, 30 tbr, 10000k tbn, 30 tbc  
[huffyuv @ 0000000000380ae0] using huffyuv 2.2.0 or newer interlacing flag  
[huffyuv @ 0000000000377280] using huffyuv 2.2.0 or newer interlacing flag  
[huffyuv @ 00000000003b0fc0] using huffyuv 2.2.0 or newer interlacing flag  
[huffyuv @ 00000000003b1700] using huffyuv 2.2.0 or newer interlacing flag  
[huffyuv @ 0000000000357a00] using huffyuv 2.2.0 or newer interlacing flag  
Output #0, matroska, to 'capture.mkv':  
  Metadata:  
    encoder         : Lavf56.40.100  
    Stream #0:0: Video: huffyuv (HFYU / 0x55594648), rgb24, 1920x1080, q=2-31, 2  
00 kb/s, 30 fps, 1k tbn, 30 tbc  
    Metadata:  
      encoder         : Lavc56.46.101 huffyuv  
Stream mapping:  
  Stream #0:0 -> #0:0 (rawvideo (native) -> huffyuv (native))  
Press [q] to stop, [?] for help  
frame=   12 fps=0.0 q=0.0 size=   23668kB time=00:00:00.50 bitrate=386999.4kbits  
frame=   22 fps= 22 q=0.0 size=   44696kB time=00:00:00.96 bitrate=379033.1kbits  
frame=   35 fps= 23 q=0.0 size=   72074kB time=00:00:01.46 bitrate=402750.2kbits  
frame=   49 fps= 24 q=0.0 size=  101620kB time=00:00:02.00 bitrate=416236.4kbits  
frame=   63 fps= 25 q=0.0 size=  131190kB time=00:00:02.50 bitrate=429712.0kbits  
frame=   78 fps= 26 q=0.0 size=  162896kB time=00:00:03.03 bitrate=439829.0kbits  
frame=   83 fps= 26 q=0.0 Lsize=  175587kB time=00:00:03.23 bitrate=444776.1kbit  
s/s  
video:175582kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxin  
g overhead: 0.002479%  
[dshow @ 000000000034f420] real-time buffer [screen-capture-recorder] [video inp  
ut] too full or near too full (545% of size: 3041280 [rtbufsize parameter])! fra  
me dropped!  
Exiting normally, received signal 2.  
Terminate batch job (Y/N)?  

注意:即使我已经为一般目的接受了答案,我还是倾向于将@ paul-b-mahol提供的解决方案与libx264rgb编码器一起使用。



您应该显示每个ffmpeg命令的完整控制台输出。
llogan

@LordNeckbeard完成了!
user3450548

谢谢。您正在使用什么来播放H.264视频?
llogan

1
结果可能是由于自动插入的缩放器执行了从yuv444p到yuv420p的色度子采样转换以进行回放。我不知道为什么需要发生这种情况,以便您可以观看它(我从没发现事情的播放方面非常有趣,因此在这种情况下我仍然一无所知)。
llogan

2
libx264编码器不支持bgr进行转码,因此您会得到不同的结果,请尝试使用libx264rgb编码器。
Paul B. Mahol '16

Answers:


7

这是播放器的人工产物,而不是编码器。

我使用以下命令在HuffYUV中生成RGB格式捕获

ffmpeg -f dshow -video_size 1920x1080 -framerate 30 -i video="screen-capture-recorder" -c:v huffyuv -t 5 cap.mkv

然后将文件转码为

x264无损RGB

ffmpeg -i cap.mkv -c:v libx264rgb -crf 0 -preset ultrafast h264rgb.mkv

HuffYUV YUV 4:2:2

ffmpeg -i cap.mkv -c:v huffyuv -pix_fmt yuv422p huffyuv.mkv

x264无损YUV 4:2:2

ffmpeg -i cap.mkv -c:v libx264 -crf 0 -preset ultrafast -pix_fmt yuv422p h264yuv.mkv

然后,使用SSIM指标比较了HuffYUV和x264 RGB文件

ffmpeg -i h264rgb.mkv -i cap.mkv -filter_complex ssim -f null -

导致

SSIM R:1.000000 (inf) G:1.000000 (inf) B:1.000000 (inf) All:1.000000 (inf)

还有HuffYUV和x264 YUV文件

ffmpeg -i h264yuv.mkv -i huffyuv.mkv -filter_complex ssim -f null -

结果

SSIM Y:1.000000 (inf) U:1.000000 (inf) V:1.000000 (inf) All:1.000000 (inf)

因此,x264确实会产生无损输出。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.