H.264 编码的视频能同时被 mov 和 mp4 进行封装?

2021-09-13 11:45:14 +08:00
 xlsepiphone

我在上一个帖子『Golang 的 http.DetectContentType 有更好的替代实现吗?』求问了 Golang 下面对于不同文件头识别的开源库,有 V 友回复了filetype这个库,但是我最后使用了mimetype这个库的解决方案。

mimetype能够正确把视频的 mimetype 识别成 video/mp4,但是filetype这个库却识别出来的是 video/quicktime 。

然后我在上面的帖子也 append 了一下让我困惑的问题,就是我接着用 ffprobe 查看视频的编码信息,输出的是 H.264 ,封装格式如下:

format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime / MOV

最后我用 Mac 的 Finder 查看,显示的又是 MPEG-4 影片,完全糊涂了,这意味着这个视频是先用用 mov 封装,再用 mp4 进行封装的吗?

本人对视频编解码相关知识的了解非常基础,基本处于懵懵懂懂的状态,Google 了好久都没有找到对于 format_name 这个字段的多个值的具体解释。

求 V 友解惑。

3823 次点击
所在节点    程序员
26 条回复
wy315700
2021-09-13 11:49:09 +08:00
H.264 是视频编码,类似的还有 h265 。

mov 和 mp4 是视频文件格式,类似的还有 mkv,flv 。

一个视频文件里可以包含多个 H.264 视频和多个音频数据。。


mov 和 mp4 其实是一种东西,格式都一样的。。
ysc3839
2021-09-13 11:50:21 +08:00
> The MP4 container is derived from Apple QuickTime (MOV).
https://www.quora.com/What-is-the-main-difference-between-MOV-and-MP4-files
seki
2021-09-13 11:52:12 +08:00
因为 mov 与 mp4 容器的渊源比较深吧
https://en.wikipedia.org/wiki/QuickTime_File_Format

可以试试用 Mediainfo 查看具体是什么容器
JerryCha
2021-09-13 11:56:34 +08:00
你去看 flv 的也是 H.264
ts 和 m2ts 里面也有 H.264
惊不惊喜 意不意外?
xlsepiphone
2021-09-13 11:59:36 +08:00
@wy315700 #1
@ysc3839 #2
@seki #3
@JerryCha #4
所以到底怎么判断容器的格式呢?
xlsepiphone
2021-09-13 12:05:02 +08:00
mediainfo 输出如下
```
General
Complete name : /Users/言叶之庭.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom)
File size : 1.01 GiB
Duration : 46 min 3 s
Overall bit rate mode : Variable
Overall bit rate : 3 130 kb/s
Encoded date : UTC 2018-11-17 15:06:54
Tagged date : UTC 2018-11-17 15:06:54

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4
Format settings : CABAC / 4 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference frames : 4 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 46 min 3 s
Bit rate : 3 000 kb/s
Maximum bit rate : 20.6 Mb/s
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate mode : Variable
Frame rate : 23.976 FPS
Minimum frame rate : 23.810 FPS
Maximum frame rate : 24.390 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.060
Stream size : 989 MiB (96%)
Writing library : x264 core 148 r2597+52 4ed4a81 xiaowan [8-bit@all X86_64]
Encoding settings : cabac=1 / ref=4 / deblock=1:1:1 / analyse=0x3:0x133 / me=umh / subme=10 / psy=1 / fade_compensate=0.00 / psy_rd=0.30:0.00 / mixed_ref=1 / me_range=24 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / fgo=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=240 / keyint_min=1 / scenecut=60 / intra_refresh=0 / rc_lookahead=60 / rc=2pass / mbtree=1 / bitrate=3000 / ratetol=1.0 / qcomp=0.50 / qpmin=0:0:0 / qpmax=69:69:69 / qpstep=4 / cplxblur=20.0 / qblur=0.5 / ip_ratio=1.40 / aq=2:0.80 / aq-sensitivity=10.00 / aq-factor=1.00:1.00:1.00 / aq2=0 / aq3=0
Encoded date : UTC 2018-11-17 14:02:01
Tagged date : UTC 2018-11-17 15:07:04
Color range : Limited
Matrix coefficients : BT.709
Codec configuration box : avcC

Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 46 min 3 s
Bit rate mode : Variable
Bit rate : 125 kb/s
Maximum bit rate : 262 kb/s
Channel(s) : 6 channels
Channel layout : C L R Ls Rs LFE
Sampling rate : 48.0 kHz
Frame rate : 46.875 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 41.0 MiB (4%)
Encoded date : UTC 2018-11-17 13:37:54
Tagged date : UTC 2018-11-17 15:07:04
```
xylophone21
2021-09-13 12:05:31 +08:00
ffmpeg
siguretto
2021-09-13 12:07:13 +08:00
最简单的方法是判断头部字节 ftyp,mov 是 ftypqt,mp4 是 ftypmp4 。
xlsepiphone
2021-09-13 12:08:37 +08:00
@siguretto #8

xxd /Users/言叶之庭.mp4 | head
00000000: 0000 0014 6674 7970 6973 6f6d 0000 0001 ....ftypisom....
00000010: 6973 6f6d 001b 04be 6d6f 6f76 0000 006c isom....moov...l
00000020: 6d76 6864 0000 0000 d815 df0e d815 df0e mvhd............
00000030: 0000 0258 0019 4dc0 0001 0000 0100 0000 ...X..M.........
00000040: 0000 0000 0000 0000 0001 0000 0000 0000 ................
00000050: 0000 0000 0000 0000 0001 0000 0000 0000 ................
00000060: 0000 0000 0000 0000 4000 0000 0000 0000 ........@.......
00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000080: 0000 0000 0000 0003 0000 0015 696f 6473 ............iods
00000090: 0000 0000 1007 004f ffff 2a15 ff00 11d3 .......O..*.....
xlsepiphone
2021-09-13 12:09:10 +08:00
@siguretto #8 fty pisom 是什么鬼。。。
siguretto
2021-09-13 12:09:20 +08:00
在我理解,mov 是兼容 mp4 的,所里两个库得到的 mimetype 不一样也很正常。video/mp4 是从兼容性考虑,video/quicktime 则是更加准确。
siguretto
2021-09-13 12:14:44 +08:00
关于 iso,可以看看 wiki: https://en.wikipedia.org/wiki/ISO/IEC_base_media_file_format

可以这么想,iso 把 mov 标准化了,mp4 又采纳了 iso 标准,所以就是 mov -> iso -> mp4
mikewang
2021-09-13 12:17:44 +08:00
mov 是 Apple 的定标准,mp4 来自于 mov,用是 ISO 的标准。它们的格式类似。就像 webm 来自于 mkv,格式类似但有着不同的标准。

大多数情况播放器 mov 和 mp4 可以互相兼容,视频编码一样的话。mimetype 不一致关系也不大。

使用 ffprobe 来看,mov 格式的 major_brand 是 qt ( QuickTime )
lostvincent
2021-09-13 14:01:41 +08:00
首先,你要区分视频流和封装
h.264 是视频流的格式
mp4 是封装格式
这两个是可以同时存在的,简单来说就是一个是外包装,一个是内容

其次,你要明确下你需要的是视频流还是封装的格式。
看帖子内容,我假设你需要的是:封装格式
在没有具体文件的情况下,你描述的情况是可能存在的

类比现实情况,就是你拿到了一个水果礼物盒( mp4 ),里面装着一个柑橘( h.264 )
mimetype 检测之后,告诉你这是一个水果礼物盒( video/mp4 )
filetype 检测后,告诉你这是一个橘子礼物盒( video/quicktime )
ffprobe 告诉你这是一个柑橘( h.264 )

一些关于 isom 的东西
Some references:
https://forum.doom9.org/showthread.php?p=1664561#post1664561
https://forum.doom9.org/showthread.php?p=1827483#post1827483

相关行业标准文档:
Document:
ISO/IEC 14496-12

太长不看版:
都说的没错,只不过对象和详细程度不同
expy
2021-09-13 14:15:47 +08:00
容器跟编码的区别,H264/MPEG4/AVC 是同一个编码。

MP4 是容器,可以封装各种编码的视频流和音频流(H.264/H.265/AV1/AAC/FLAC/MP3/Opus)。
tsanie
2021-09-13 14:50:41 +08:00
抛开标题,我觉得这个文件比较奇怪。'ftypisom'为什么会被 ffprobe 和 filetype 库识别成 QuickTime File,果子的 QuickTime 格式规范上写的 major_brand 不是一定要'qt '么……
huangmingyou
2021-09-13 14:55:17 +08:00
封装和内容编码是两个独立概念。 内容编码可以比喻成英语书或者中文书,封装可以理解成顺风或者邮政快递。可以随便组合啊。
CEBBCAT
2021-09-13 16:00:50 +08:00
<amp-youtube data-videoid="P-k4nqCL7tw" layout="responsive" width="480" height="270"></amp-youtube>
mikewang
2021-09-13 16:59:24 +08:00
#14 @lostvincent
水果礼物盒和橘子礼物盒的比喻可能不太准确,mov 和 mp4 应该是平级的,他们之间还存在一些微小的差别。
例如:
   video/quicktime 支持封装 ALAC 无损音频,video/mp4 不能。
   video/mp4 支持封装 AV1 编码的视频,video/quicktime 不能。

上面都在讨论视频编码的问题,我觉得这个问题重点不在这里。
macOS 文件简介显示的「 MPEG-4 影片」指的是「 MPEG-4 Part 14 」定义的容器格式,不是指「 MPEG-4 Part 2 」定义的 MPEG-4 编码。
可以查看一个封装 HEVC 的 mp4 文件,简介依然为「 MPEG-4 影片」。

严格意义上讲,只有 major_brand 为 qt 时,文件才是 video/quicktime 。
filetype 库,将 #9 的文件识别为 video/quicktime 是有问题的,这显然是个 video/mp4 文件。

这个问题已经有人提过了,同一个文件多次执行,得到结果不一样 XD
https://github.com/h2non/filetype/issues/64

![]( https://ae01.alicdn.com/kf/Hdd619c78118648b1a0d5550c16c80aa3x.png)
xlsepiphone
2021-09-13 17:10:53 +08:00
@mikewang #19

```
Metadata:
major_brand : isom
minor_version : 1
compatible_brands: isom
creation_time : 2018-11-17T15:06:54.000000Z
```
major_brand 是 isom,看完你们的回复,懵了,感觉视频编码与封装这块,太不直白了,这么多历史渊源。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/801517

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX