将 MPEG-1 转码为 MPEG-H (H.265/HEVC)

问题描述

使用 FFMPEG 文档中介绍的 2-pass 方法,将 MPEG-4 视频转码为 MPEG-H 以将大小减少一半而不损失可感知的质量相对容易。我一直在使用的方案是使用 bit_rate 从原始 MPEG-4 视频中提取 ffprobe 值,然后将该值乘以 4 / 10。因此,新的较低值将由 -b:v 指定并成为结果 MPEG-H 视频的 bit_rate 值。这对于大多数视频都非常有效,不仅适用于 MPEG-4,而且适用于 QuickTime 和音频视频交错 (AVI)。

但是,现在我偶然发现了以这种方式转码 MPEG-1 视频的问题。不知何故,生成的 MPEG-H 视频比原始视频大得令人无法接受。 200MB 的原始视频很容易产生 1.5GB 的转码视频。当我寻找这种荒谬行为的原因时,我发现 MPEG-1 视频中宣传的 bit_rate 值不知何故也高得离谱。以下面两个视频为例(ffprobe的输出):

164MB                           15MB
[STREAM]                        [STREAM]
index=0                         index=1
codec_name=mpeg4                codec_name=mpeg1video
codec_long_name=MPEG-4 part 2   codec_long_name=MPEG-1 video
profile=Simple Profile          profile=unknown
codec_type=video                codec_type=video
codec_time_base=1/24            codec_time_base=1/25
codec_tag_string=mp4v           codec_tag_string=[0][0][0][0]
codec_tag=0x7634706d            codec_tag=0x0000
width=960                       width=640
height=540                      height=480
coded_width=960                 coded_width=0
coded_height=540                coded_height=0
closed_captions=0               closed_captions=0
has_b_frames=0                  has_b_frames=1
sample_aspect_ratio=1:1         sample_aspect_ratio=1:1
display_aspect_ratio=16:9       display_aspect_ratio=4:3
pix_fmt=yuv420p                 pix_fmt=yuv420p
level=1                         level=-99
color_range=unknown             color_range=tv
color_space=unknown             color_space=unknown
color_transfer=unknown          color_transfer=unknown
color_primaries=unknown         color_primaries=unknown
chroma_location=left            chroma_location=center
field_order=unknown             field_order=unknown
timecode=N/A                    timecode=N/A
refs=1                          refs=1
quarter_sample=false            
divx_packed=false               
id=N/A                          id=0x1e0
r_frame_rate=24/1               r_frame_rate=25/1
avg_frame_rate=24/1             avg_frame_rate=25/1
time_base=1/24                  time_base=1/90000
start_pts=0                     start_pts=9900
start_time=0.000000             start_time=0.110000
duration_ts=7451                duration_ts=3690000
duration=310.458333             duration=41.000000
bit_rate=4096346                bit_rate=104857200
max_bit_rate=9000000            max_bit_rate=N/A
bits_per_raw_sample=N/A         bits_per_raw_sample=N/A
nb_frames=7451                  nb_frames=N/A
nb_read_frames=N/A              nb_read_frames=N/A
nb_read_packets=N/A             nb_read_packets=N/A
DISPOSITION:default=1           DISPOSITION:default=0
DISPOSITION:dub=0               DISPOSITION:dub=0
DISPOSITION:original=0          DISPOSITION:original=0
DISPOSITION:comment=0           DISPOSITION:comment=0
DISPOSITION:lyrics=0            DISPOSITION:lyrics=0
DISPOSITION:karaoke=0           DISPOSITION:karaoke=0
DISPOSITION:forced=0            DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0  DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0   DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0     DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0      DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0  DISPOSITION:timed_thumbnails=0
[/STREAM]                       [/STREAM]

显然,MPEG-4 视频比 MPEG-1 大 11 倍,但同时 MPEG-4 视频比 MPEG-1 宣传的比特率低 100000000。而且由于我的算法没有考虑其他任何东西来计算最终比特率,这就是问题本身的原因。现在我试图查看其他一些字段值以寻找可能还应该将其他内容包含在等式中的提示,我的怀疑在于以下几点:

time_base=1/24                  time_base=1/90000
start_pts=0                     start_pts=9900
start_time=0.000000             start_time=0.110000
duration_ts=7451                duration_ts=3690000
duration=310.458333             duration=41.000000

我不是 MPEG 方面的专家,而且文档在这些方面的信息并不多,我可以从中推导出一些数学知识,或者以其他方式将它们与以有意义的方式解决我的问题联系起来。有经验的可以帮忙吗?

解决方法

通过搜索网络,我发现 104857200 的比特率值是某种问题的迹象,可能与元数据有关。可以搜索ffmpeg104857 kb/s 关键字。因此,正如我的问题中已经提出的那样,这个值根本不能被认真对待。幸运的是,FORMAT 部分还包含文件的 bit_rate,如下所示:

ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 <video-file>

在 MPEG-1 的情况下,此 bit_rate 值仅与视频流所需的比特率完全对应(是的,其中不包含音频流很重要)。例如,主题 MPEG-1 的值为 2914159

因此,将视频转码为 MPEG-H 的算法现在只是测试

的返回值是否
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 <video-file>

等于mpeg1video,如果是,则从bit_rate部分而不是视频FORMAT部分进行分支检索STREAM

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...