无损视频编解码器意味着几乎为零信息损失。经过网上搜索,我发现一些传统的开源无损编解码器,如 huffyuv、lagarith、utvideo 和 ffv1,它们产生的文件大小很大,而一些现代无损编解码器,如 h264 无损、hevc 无损、vp9 无损和 av1 无损,它们产生的文件大小要小得多。
我有一个巨大的无损屏幕录制视频,用以下方式编码ffv1 版本 3,虽然只有 2 分钟,但大小却高达 1.5GB。按照 ffmpeg 的无损编码指南操作后(h264,h265,VP9和AV1 的),我将其重新编码为这四个无损编解码器,发现它们的文件大小小了很多!h264无损是64MB,hevc无损是69MB,vp9无损是91MB,av1无损是55MB。它们压缩了原始ffv1编解码器的90%以上。
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
使用 ffmpeg 重新编码为 H264 无损:
$ 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
使用 ffmpeg 重新编码为 H265 无损:
$ 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
在 ffmpeg 中重新编码为 VP9 无损:
$ 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
使用 ffmpeg 重新编码为 AV1 无损:
$ 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
所以我想知道是否可以
- 将使用传统无损编解码器(raw yuv、huffyuv、lagarith、utvideo、ffv1 等)编码的大型视频文件重新编码为现代无损编解码器(h264 无损、hevc 无损、vp9 无损和 av1 无损)以获得新的视频文件。
- 删除原始的巨大视频以节省磁盘空间。
并期望零质量损失?
答案1
有些事情你需要考虑。编解码器真的可以无损吗?是的,一些编解码器,如 h265 和 h264,如果在无损模式下使用,确实是无损的(这些编解码器很灵活,它们允许某种配置来实现真正的无损,例如 -crf 0)。这样,你可以有效地比较像素与像素之间的差异,并获得完全相同的视频帧。
如何才能获得如此小的数据量?答案取决于如何处理数据以及使用何种压缩方式。
每一帧都经过分析和仔细检查,以确定帧与帧之间是否存在一些共同的数据,如果您对此感兴趣,您可以在 h264、h265 和 av1 完整技术文档中阅读许多其他段落。
然而,为了简化每一代编解码器,需要进行更复杂的分析,并允许以更强大的计算能力和更少的存储空间重新构建数据。
现在,您使用的编解码器(如 lagarith、huffyuv)与您想要转换的编解码器之间存在差异。它们的设计在存储方面效率较低,但在计算能力方面效率更高(不讨论 long gop 与 shot gop 配置)。
h264、h265 等可以用作视觉无损,如果从真正无损的 -crf 0 到 -crf 18,我们谈论视觉无损,因为对于正常人来说,几乎不可能察觉到与原始图像的差异。
使用某种编解码器的决定很大程度上取决于您最终的用途,存储文件以供存档?流式传输数据?工作并进一步编辑它们?