2

Lossless video codec means almost zero information loss. After some internet search I found that there are some traditional opensource lossless codec like huffyuv, lagarith, utvideo and ffv1, which produce huge file size and some modern lossless codec like h264 lossless, hevc lossless, vp9 lossless and av1 lossless which produce much smaller file size.

I have a huge lossless screen recording video encoded with ffv1 version 3, which is only 2 minutes but at the size of 1.5GB. After following the lossless encoding guide from ffmpeg(h264, h265, vp9 and av1), I re-encode it to these four lossless codecs and find that their file size is much smaller! h264 lossless is 64MB, hevc lossless is 69MB, vp9 lossless is 91MB, av1 lossless is 55MB. They have compressed more than 90% of original ffv1 codec.

Original Video in ffmpeg:

$ ffmpeg -hide_banner -i record.mkv 
Input #0, matroska,webm, from 'record.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.27, start: 0.000000, bitrate: 95922 kb/s
  Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv420p(tv, bt709), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
At least one output file must be specified

$ ls -sh record.mkv 
1.5G record.mkv

Re-encode to H264 Lossless with ffmpeg:

$ ffmpeg -hide_banner -i record.mkv -c:v libx264 -preset veryslow -qp 0 -threads 16 -c:a copy lossless_h264.mkv
Input #0, matroska,webm, from 'record.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.27, start: 0.000000, bitrate: 95922 kb/s
  Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv420p(tv, bt709), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
File 'lossless_h264.mkv' already exists. Overwrite? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (ffv1 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libx264 @ 0x555d761e3840] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x555d761e3840] profile High 4:4:4 Predictive, level 5.1, 4:2:0, 8-bit
[libx264 @ 0x555d761e3840] 264 - core 164 r3095 baee400 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=16 deblock=1:0:0 analyse=0x3:0x133 me=umh subme=9 psy=0 mixed_ref=1 me_range=24 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=0 chroma_qp_offset=0 threads=16 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc=cqp mbtree=0 qp=0
Output #0, matroska, to 'lossless_h264.mkv':
  Metadata:
    encoder         : Lavf60.3.100
  Stream #0:0: Video: h264 (H264 / 0x34363248), yuv420p(tv, bt709, progressive), 1920x1080, q=2-31, 60 fps, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
      encoder         : Lavc60.3.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: flac ([172][241][0][0] / 0xF1AC), 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
frame= 7757 fps= 80 q=-1.0 Lsize=   65004kB time=00:02:09.26 bitrate=4119.5kbits/s speed=1.34x    
video:60220kB audio:4722kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.094194%
[libx264 @ 0x555d761e3840] frame I:37    Avg QP: 0.00  size:193560
[libx264 @ 0x555d761e3840] frame P:7720  Avg QP: 0.00  size:  7060
[libx264 @ 0x555d761e3840] mb I  I16..4: 80.5%  1.3% 18.1%
[libx264 @ 0x555d761e3840] mb P  I16..4:  1.5%  0.0%  0.2%  P16..4:  1.2%  0.1%  0.1%  0.0%  0.0%    skip:96.7%
[libx264 @ 0x555d761e3840] 8x8 transform intra:2.4% inter:48.6%
[libx264 @ 0x555d761e3840] coded y,uvDC,uvAC intra: 29.2% 30.8% 30.6% inter: 0.8% 1.2% 1.2%
[libx264 @ 0x555d761e3840] i16 v,h,dc,p: 76% 23%  1%  0%
[libx264 @ 0x555d761e3840] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 39% 15%  2%  2%  0%  4%  0%  3%
[libx264 @ 0x555d761e3840] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 46% 36% 10%  2%  1%  1%  2%  1%  2%
[libx264 @ 0x555d761e3840] i8c dc,h,v,p: 59% 22% 19%  0%
[libx264 @ 0x555d761e3840] Weighted P-Frames: Y:0.8% UV:0.8%
[libx264 @ 0x555d761e3840] ref P L0: 82.1%  3.9%  6.2%  5.2%  0.7%  0.5%  0.4%  0.2%  0.1%  0.1%  0.1%  0.1%  0.1%  0.1%  0.1%  0.1%
[libx264 @ 0x555d761e3840] kb/s:3815.81

$ ffmpeg -hide_banner -i lossless_h264.mkv 
Input #0, matroska,webm, from 'lossless_h264.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.28, start: 0.000000, bitrate: 4118 kb/s
  Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv420p(tv, bt709, progressive), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      ENCODER         : Lavc60.3.100 libx264
      DURATION        : 00:02:09.284000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
At least one output file must be specified

$ ls -sh lossless_h264.mkv 
64M lossless_h264.mkv

Re-encode to H265 Lossless with ffmpeg:

$ ffmpeg -hide_banner -i record.mkv -c:v libx265 -preset veryslow -x265-params lossless=1 -threads 16 -c:a copy lossless_h265.mkv
Input #0, matroska,webm, from 'record.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.27, start: 0.000000, bitrate: 95922 kb/s
  Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv420p(tv, bt709), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
Stream mapping:
  Stream #0:0 -> #0:0 (ffv1 (native) -> hevc (libx265))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
x265 [info]: HEVC encoder version 3.5
x265 [info]: build info [Linux][GCC 11.2.0][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x265 [info]: Main profile, Level-8.5 (Main tier)
x265 [info]: Thread pool created using 16 threads
x265 [info]: Slices                              : 1
x265 [info]: frame threads / pool features       : 16 / wpp(17 rows)
x265 [info]: Coding QT: max CU size, min CU size : 64 / 8
x265 [info]: Residual QT: max TU size, max depth : 32 / 3 inter / 3 intra
x265 [info]: ME / range / subpel / merge         : star / 57 / 4 / 5
x265 [info]: Keyframe min / max / scenecut / bias  : 25 / 250 / 40 / 5.00 
x265 [info]: Lookahead / bframes / badapt        : 40 / 8 / 2
x265 [info]: b-pyramid / weightp / weightb       : 1 / 1 / 1
x265 [info]: References / ref-limit  cu / depth  : 5 / off / off
x265 [info]: Rate Control                        : Lossless
x265 [info]: tools: rect amp rd=6 psy-rd=2.00 rdoq=2 psy-rdoq=1.00 rskip mode=1
x265 [info]: tools: signhide tmvp b-intra strong-intra-smoothing deblock sao
Output #0, matroska, to 'lossless_h265.mkv':
  Metadata:
    encoder         : Lavf60.3.100
  Stream #0:0: Video: hevc, yuv420p(tv, bt709, progressive), 1920x1080, q=2-31, 60 fps, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
      encoder         : Lavc60.3.100 libx265
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: flac ([172][241][0][0] / 0xF1AC), 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
frame= 7757 fps= 16 q=4.0 Lsize=   69891kB time=00:02:09.23 bitrate=4430.4kbits/s speed=0.274x    
video:65103kB audio:4722kB subtitle:0kB other streams:0kB global headers:2kB muxing overhead: 0.095454%
x265 [info]: frame I:     32, Avg QP:4.00  kb/s: 114005.70
x265 [info]: frame P:   1488, Avg QP:4.00  kb/s: 10058.43
x265 [info]: frame B:   6237, Avg QP:4.00  kb/s: 2143.53 
x265 [info]: Weighted P-Frames: Y:3.9% UV:3.5%
x265 [info]: Weighted B-Frames: Y:1.5% UV:1.1%
x265 [info]: consecutive B-frames: 9.1% 12.0% 14.8% 10.5% 8.1% 11.4% 9.5% 5.3% 19.2% 
x265 [info]: lossless compression ratio 241.39::1

encoded 7757 frames in 472.40s (16.42 fps), 4123.29 kb/s, Avg QP:4.00

$ ffmpeg -hide_banner -i lossless_h265.mkv 
Input #0, matroska,webm, from 'lossless_h265.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.28, start: 0.000000, bitrate: 4428 kb/s
  Stream #0:0: Video: hevc (Main), yuv420p(tv, bt709, progressive), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      ENCODER         : Lavc60.3.100 libx265
      DURATION        : 00:02:09.284000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
At least one output file must be specified

$ ls -sh lossless_h265.mkv 
69M lossless_h265.mkv

Re-encode to VP9 Lossless in ffmpeg:

$ ffmpeg -hide_banner -i record.mkv -c:v libvpx-vp9 -lossless 1 -threads 16 -c:a copy lossless_vp9.mkv
Input #0, matroska,webm, from 'record.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.27, start: 0.000000, bitrate: 95922 kb/s
  Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv420p(tv, bt709), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
File 'lossless_vp9.mkv' already exists. Overwrite? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (ffv1 (native) -> vp9 (libvpx-vp9))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libvpx-vp9 @ 0x55f6e1c3a780] v1.13.0
Output #0, matroska, to 'lossless_vp9.mkv':
  Metadata:
    encoder         : Lavf60.3.100
  Stream #0:0: Video: vp9 (VP90 / 0x30395056), yuv420p(tv, bt709, progressive), 1920x1080, q=2-31, 60 fps, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
      encoder         : Lavc60.3.100 libvpx-vp9
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: flac ([172][241][0][0] / 0xF1AC), 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
frame= 7757 fps= 30 q=0.0 Lsize=   92578kB time=00:02:09.26 bitrate=5866.9kbits/s speed=0.494x    
video:87793kB audio:4722kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.067256%

$ ffmpeg -hide_banner -i lossless_vp9.mkv 
Input #0, matroska,webm, from 'lossless_vp9.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.28, start: 0.000000, bitrate: 5866 kb/s
  Stream #0:0: Video: vp9 (Profile 0), yuv420p(tv, bt709, progressive), 1920x1080, lossless, 60 fps, 60 tbr, 1k tbn
    Metadata:
      ENCODER         : Lavc60.3.100 libvpx-vp9
      DURATION        : 00:02:09.284000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
At least one output file must be specified

$ ls -sh lossless_vp9.mkv 
91M lossless_vp9.mkv

Re-encode to AV1 Lossless with ffmpeg:

$ ffmpeg -hide_banner -i record.mkv -c:v libaom-av1 -crf 0 -threads 16 -c:a copy lossless_av1.mkv
Input #0, matroska,webm, from 'record.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.27, start: 0.000000, bitrate: 95922 kb/s
  Stream #0:0: Video: ffv1 (FFV1 / 0x31564646), yuv420p(tv, bt709), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
Stream mapping:
  Stream #0:0 -> #0:0 (ffv1 (native) -> av1 (libaom-av1))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libaom-av1 @ 0x55f58812a780] v3.6.1
Output #0, matroska, to 'lossless_av1.mkv':
  Metadata:
    encoder         : Lavf60.3.100
  Stream #0:0: Video: av1 (AV01 / 0x31305641), yuv420p(tv, bt709, progressive), 1920x1080, q=2-31, 60 fps, 1k tbn
    Metadata:
      DURATION        : 00:02:09.267000000
      encoder         : Lavc60.3.100 libaom-av1
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: flac ([172][241][0][0] / 0xF1AC), 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
frame= 7757 fps=1.5 q=0.0 Lsize=   55457kB time=00:02:09.26 bitrate=3514.5kbits/s speed=0.0242x    
video:50692kB audio:4722kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.078322%

$ ffmpeg -hide_banner -i lossless_av1.mkv 
Input #0, matroska,webm, from 'lossless_av1.mkv':
  Metadata:
    ENCODER         : Lavf60.3.100
  Duration: 00:02:09.28, start: 0.000000, bitrate: 3514 kb/s
  Stream #0:0: Video: av1 (Main), yuv420p(tv, bt709, progressive), 1920x1080, 60 fps, 60 tbr, 1k tbn
    Metadata:
      ENCODER         : Lavc60.3.100 libaom-av1
      DURATION        : 00:02:09.284000000
  Stream #0:1: Audio: flac, 48000 Hz, stereo, s16
    Metadata:
      DURATION        : 00:02:09.216000000
At least one output file must be specified

$ ls -sh lossless_av1.mkv 
55M lossless_av1.mkv

So I wonder if it is okay to

  1. Re-encode huge video files which is encoded with traditional lossless codec(raw yuv, huffyuv, lagarith, utvideo, ffv1, etc) to modern lossless codec(h264 lossless, hevc lossless, vp9 lossless and av1 lossless) to a new video file.
  2. Delete the original huge video to save disk space.

And expect ZERO QUALITY LOSS?

4
  • 2
    Is it OK? Do you have a theoretical question, or a real question? If the encoding you've chosen and used is truly lossless, then yes you can, and so I'm wondering: what is your actual question? Are you just looking for some affirmation you've made the right choice? I don't think we can give that. Commented Jul 17, 2023 at 4:27
  • 2
    I'm not an expert of this field. These codec claimed themselves lossless, but I'm not sure if they are really lossless at such small file size.
    – ArchBug
    Commented Jul 17, 2023 at 4:33
  • I did some encodes with the settings from the h264 link in your post. I chose the "very slow" option. A 27secs, hd1080, 50fps video (just my dog walking) came out at over 1.4GB (original 85MB). I then created a 2mins screencast of me using a vector drawing app. Encoded with the same settings produced just over 8MB (original 12MB). You make no mention of the content, resolution, framerate etc of your video, so it is impossible to suggest whether it is likely to be too small, or even too large.
    – Sark
    Commented Jul 18, 2023 at 19:35
  • The original recording is not accessible so I updated the detailed log with another similar recording. Pretty much the same situation.
    – ArchBug
    Commented Jul 22, 2023 at 12:58

1 Answer 1

1

There are some things you have to account. Can really the codecs be lossless? Yes some codecs like h265 and h264 are really lossless if used in lossless mode (this codecs are flexible, they allow a certain configuration that allows true lossless eg. -crf 0). And in this way you can effectively compare pixel to pixel and obtain the same exact video frames.

How is possibile obtain such small level of data amount? The reply relies in how the data is treated and the compression used.

Every frame is analyzed and carefully inspected in order to determine if some data in common is present frame to frame and lot of other passages that you can read in h264, h265 and av1 full technical documentation if you are interested into this.

However to simplify each generation of codec do lot more complex analysis and allow to re construct data with more computational power and lesser storage.

Now there's a difference between the codec you used like lagarith, huffyuv and the ones you want convert to. They are designed to be less efficient in terms of storage but more in terms of computational power (without entering in the discussion of long gop vs shot gop configurations).

h264, h265 and so on can be used as visual lossless if you go from -crf 0 that is really lossless to -crf 18 for example, we talk about visual lossless because for a normal human is nearly to impossible to perceive differences versus the original.

Lot of the decision in using a certain codecs depends the final usage you have to do with it, store the files for archive ? stream the data ? working and further edit them?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .