8

I got curious on ffmpeg screen capture capabilities and started messing around with a simple realtime capture test in h264.

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

Based on what is said on ffmpeg h264 documentation with the -qp 0 or -crf 0 options libx264 should work in lossless mode.

You can use -qp 0 or -crf 0 to encode a lossless output. Use of -qp is recommended over -crf for lossless because 8-bit and 10-bit x264 use different -crf values for lossless.

This is also repeated in the realtime capture section help when talking about an optional re-encoding with a slower preset to try saving some size:

Note that since the initial recording was lossless, and the re-encode is lossless too, no quality loss is introduced in this process in any way.

Based on this I trusted the guide and assumed using -qp 0 I would achieve a fully lossless workflow ;)

However I found it introduce some loss in particular situations.

So I made another test with the huffyuv codec with this code:

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

the results:

Screen Capture encoded in h264 Screen Capture encoded in huffyuv

Screen 1: h264 in lossless mode
Screen 2: huffyuv

Based on screens huffyuv is perfect, a true lossless codec while h264 instead compress something here and I'm unable to understand why if it should be setup in a lossless mode.

(huffyuv is identical to a bitmap screenshot of the desktop, I would achieve the same with h264)

Can someone help me figuring it out?


Edit: Adding some ffmpeg dumps as required in comments ;)

h264 run:

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 run:

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)?  

note: even if I have accepted the answer for general purposes I tend to use the soluction provided by @paul-b-mahol with the libx264rgb encoder.


  • You should show the complete console outputs from each ffmpeg command. – llogan Oct 21 '15 at 22:38
  • @LordNeckbeard done! – user3450548 Oct 21 '15 at 22:53
  • Thanks. What are you using to play back the H.264 video? – llogan Oct 21 '15 at 22:54
  • 1
    The result is probably due to an auto-inserted scaler performing chroma subsampling conversion from yuv444p to yuv420p for playback. I don't know why that needs to occur so you can watch it (I never found the playback side of things very interesting, so therefore I remain ignorant on in this particular case). – llogan Oct 21 '15 at 23:10
  • 2
    libx264 encoder does not support bgr for transcoding so you get different results, try using libx264rgb encoder instead. – Paul B. Mahol Feb 15 '16 at 17:02
5

It's an artifact of the player, not the encoder.

I used the command below to generate a RGB format capture in HuffYUV

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

Then transcoded the file to

x264 lossless 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 lossless YUV 4:2:2

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

Then, using the SSIM metric, compared the HuffYUV and x264 RGB files

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

which resulted in

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

And the HuffYUV and x264 YUV files

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

Result

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

So, x264 does produce lossless output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.