ios - h.264 视频无法在 iOS 上播放

标签 ios html video encoding h.264

我有一个用 h.264 编码的视频文件不能在 iOS 上播放,我想知道为什么。这是 HTML:

<video style="width: 480px; height: 360px;" autoplay controls>
    <source src="sample.mp4">
</video>

该视频在桌面浏览器中播放良好,但在 iOS 设备上我看到一个不可点击的播放图标,上面有一条斜线。我读到这意味着视频的编码有问题,但我不确定是什么问题。

我通过 MediaInfo 运行视频:

General
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 706 KiB
Duration                                 : 15s 256ms
Overall bit rate mode                    : Variable
Overall bit rate                         : 379 Kbps

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Main@L5.1
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 6 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 15s 250ms
Bit rate                                 : 311 Kbps
Width                                    : 480 pixels
Height                                   : 360 pixels
Display aspect ratio                     : 4:3
Frame rate mode                          : Constant
Frame rate                               : 24.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.075
Stream size                              : 578 KiB (82%)

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40
Duration                                 : 15s 256ms
Bit rate mode                            : Variable
Bit rate                                 : 62.8 Kbps
Channel(s)                               : 2 channels
Channel positions                        : Front: L R
Sampling rate                            : 44.1 KHz
Compression mode                         : Lossy
Stream size                              : 117 KiB (17%)

另外,我通过 GSpot 运行它并获得了一些额外的信息:

Container
isom: MP4 Base Media v1 [IS0 14496-12:2003]
- mp41: MP4 v1 [ISO 14496-1:ch13]

谁能告诉我为什么这个视频不能播放?我知道我可以简单地重新编码视频,如果我这样做,它在 iOS 上播放得很好。但我想知道原始文件的问题是什么。

为了比较,这是我制作的重新编码版本的 MediaInfo 分析(这个有效):

General
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 990 KiB
Duration                                 : 15s 256ms
Overall bit rate mode                    : Variable
Overall bit rate                         : 532 Kbps
Writing application                      : Lavf53.19.0

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L2.1
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 15s 250ms
Bit rate                                 : 414 Kbps
Width                                    : 480 pixels
Height                                   : 360 pixels
Display aspect ratio                     : 4:3
Frame rate mode                          : Constant
Frame rate                               : 24.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.100
Stream size                              : 771 KiB (78%)
Writing library                          : x264 core 118 r2085 8a62835
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x111 / me=hex / subme=6 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=24 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=22.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40
Duration                                 : 15s 256ms
Bit rate mode                            : Variable
Bit rate                                 : 111 Kbps
Maximum bit rate                         : 128 Kbps
Channel(s)                               : 2 channels
Channel positions                        : Front: L R
Sampling rate                            : 44.1 KHz
Compression mode                         : Lossy
Stream size                              : 207 KiB (21%)

GSpot 关于这个容器的信息不同(这可能是这个容器起作用的原因吗?):

Container
isom: MP4 Base Media v1 [IS0 14496-12:2003]
- iso2: MP4 Base Media v2 [ISO 14496-12:2005]
- avc1: MP4 Base w/ AVC ext [ISO 14496-12:2005]
- mp41: MP4 v1 [ISO 14496-1:ch13]

我唯一能想到的是引用帧的数量(我读到的最大值是 3),但重新编码的版本有 4,所以我认为可能不是。

最佳答案

找到了我自己问题的解决方案:

问题是该视频使用了 h.264 Main Profile 级别 5.1,而 iOS 仅支持 AVC 级别 3.1(更多信息 here)。

我通过关注 these instructions 确认了这一点使用十六进制编辑器更改配置文件和级别字节。在文件中搜索 ASCII 格式的 avcC 或十六进制格式的 61 76 63 43。第一个字节通常是 01,对此目的并不重要,但之后应该是以下内容之一:

42 E0 – for Baseline Profile
4D 40 – for Main Profile
58 A0 – for Extended Profile
64 00 – for High Profile

后面的数字是十六进制的级别(不带小数点),例如:

1F (31 in dec) is level 3.1
29 (41 in dec) is level 4.1
33 (51 in dec) is level 5.1

如果您查找并替换配置文件/级别字节的所有实例(例如,搜索 4D 40 33 并替换为您想要的字节——在我的例子中是 4D 40 1F),视频现在应该可以在 iOS 上播放。请注意,许多网络帮助页面都说 iOS 需要 Baseline 配置文件,但事实并非如此。只要 AVC 级别为 3.1,Main 就可以正常工作。

关于ios - h.264 视频无法在 iOS 上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21120717/

相关文章:

video - 如何使用 ffmpeg 将字幕嵌入到 .mp4 电影中

c# - 在c#中使用ffmpeg在特定帧数中分割视频

ios - 收缩高度约束动画问题

iphone - 为什么我的 UITableView 无法滚动,除非我告诉它为滚动设置动画?

ios - 使用预编译静态库比直接包含它的源代码有什么优势?

javascript - Coldfusion 中数据表的无限滚动

android - 带有 GPS 位置的 mp4 视频文件

ios - 如何在 Swift 中制作日历 View

javascript - 单击按钮启用/禁用滚动

javascript - 获取所有数据后如何加载div包含