编码 mp4 视频以支持搜索

时间:2021-07-05 07:07:57

标签: android video mp4 h.264 android-mediacodec

我的 Android 应用程序使用 MediaCodec 和 MediaMuxer 创建 MP4。我使用 MediaPlayer 播放视频。在播放视频时,无法使用 Android 的 MediaPlayer 寻找视频中的任何位置。更具体地说,seekTo 函数将不起作用。使用其他应用程序播放视频和搜索有点粗略。一些应用似乎可以运行,而另一些则不起作用。

我已经用我在相机上录制的视频以及我在互联网上找到的各种视频替换了我的 mp4,它们都没有问题。库存相机应用程序可以生成 MP4 并让您搜索的事实清楚地表明编解码器的设置方式存在问题。这让我相信问题很可能出在用于创建视频的格式设置中。我尝试修改了许多设置,但没有成功,包括配置文件(同时使用基线和主要)、配置文件级别、I 帧间隔 (GOP) 以及比特率和视频大小。我还确保每帧的呈现时间与帧速率完全匹配。这是我为不支持搜索的视频和支持搜索的视频(相机视频)获得的信息。这些设置中是否有任何可能导致问题的原因?

一个简短的测试文件可以在这里下载。如果您在 QuickTime 或 VLC 中播放,则搜索有效:

https://drive.google.com/file/d/15QiDPYdPd_tVQTkqXuP0v2L7eKoMbWQo/view?usp=sharing

不支持搜索的视频:

**General**
Complete name : test.mp4
Format :    MPEG-4
Format profile :    Base Media
File size : 9.46 MiB
Duration :  19 s 488 ms
Overall bit rate :  4 071 kb/s
Encoded date :  UTC 2021-07-05 12:32:22
Tagged date :   UTC 2021-07-05 12:32:22
com_android_version :   11
    
**Video**
ID :    2
Format :    AVC
Format/Info :   Advanced Video Codec
Format profile :    Baseline
Format level :  5
Format settings, CABAC :    No
Format settings, Reference frames : 1 frame
Format settings, GOP :  M=1, N=30
Codec ID :  avc1
Duration :  17 s 900 ms
Source duration :   17 s 900 ms
Bit rate :  4 289 kb/s
Width : 1 440 pixels
Height :    2 614 pixels
Display aspect ratio :  0.551
Frame rate mode :   Constant
Frame rate :    30.000 FPS
Standard :  NTSC
Color space :   YUV
Chroma subsampling :    4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) :    0.038
Stream size :   9.15 MiB (97%)
Source stream size :    9.15 MiB (97%)
Title : VideoHandle
Language :  English
Encoded date :  UTC 2021-07-05 12:32:22
Tagged date :   UTC 2021-07-05 12:32:22
Color range :   Limited
Color primaries :   BT.601 PAL
Transfer characteristics :  BT.709
transfer_characteristics_Original : BT.601
Matrix coefficients :   BT.601
mdhd_Duration : 17900
Codec configuration box :   avcC
    
**Audio**
ID :    1
Format :    AAC LC
Format/Info :   Advanced Audio Codec
Codec ID :  mp4a-40-2
Duration :  19 s 488 ms
Bit rate mode : Constant
Bit rate :  128 kb/s
Channel(s) :    1 channel
Channel layout :    C
Sampling rate : 32.0 kHz
Frame rate :    31.250 FPS (1024 SPF)
Compression mode :  Lossy
Stream size :   305 KiB (3%)
Title : SoundHandle
Language :  English
Encoded date :  UTC 2021-07-05 12:32:22
Tagged date :   UTC 2021-07-05 12:32:22

支持搜索的视频:

**General**
Complete name : camera-1.mp4
Format :    MPEG-4
Format profile :    Base Media
File size : 44.4 MiB
Duration :  21 s 823 ms
Overall bit rate :  17.1 Mb/s
Encoded date :  UTC 2021-07-05 06:19:27
Tagged date :   UTC 2021-07-05 06:19:27
com_android_version :   11

**Video**
ID :    2
Format :    AVC
Format/Info :   Advanced Video Codec
Format profile :    High
Format level :  4
Format settings, CABAC :    Yes
Format settings, Reference frames : 1 frame
Format settings, GOP :  M=1, N=30
Codec ID :  avc1
Duration :  21 s 823 ms
Bit rate :  17.0 Mb/s
Width : 1 920 pixels
Height :    1 080 pixels
Display aspect ratio :  16:9
Frame rate mode :   Variable
Frame rate :    30.000 FPS
Minimum frame rate :    29.890 FPS
Maximum frame rate :    30.120 FPS
Standard :  NTSC
Color space :   YUV
Chroma subsampling :    4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) :    0.273
Stream size :   44.2 MiB (99%)
Title : VideoHandle
Language :  English
Encoded date :  UTC 2021-07-05 06:19:27
Tagged date :   UTC 2021-07-05 06:19:27
Color range :   Full
Color primaries :   BT.601 PAL
colour_primaries_Original : BT.601 NTSC
Transfer characteristics :  BT.709
transfer_characteristics_Original : BT.601
Matrix coefficients :   BT.601
mdhd_Duration : 21823
Codec configuration box :   avcC

**Audio**
ID :    1
Format :    AAC LC
Format/Info :   Advanced Audio Codec
Codec ID :  mp4a-40-2
Duration :  21 s 819 ms
Source duration :   21 s 717 ms
Bit rate mode : Constant
Bit rate :  96.0 kb/s
Channel(s) :    1 channel
Channel layout :    C
Sampling rate : 48.0 kHz
Frame rate :    46.875 FPS (1024 SPF)
Compression mode :  Lossy
Stream size :   255 KiB (1%)
Source stream size :    255 KiB (1%)
Title : SoundHandle
Language :  English
Encoded date :  UTC 2021-07-05 06:19:27
Tagged date :   UTC 2021-07-05 06:19:27
mdhd_Duration : 21819

2 个答案:

答案 0 :(得分:1)

MP4 播放器需要知道同步样本(I 帧或 IDR 帧)的位置。同步样本位置通常由位于 moov->trak->mdia->minf->stbl->stss 中的同步样本框“stss”发出信号。

在您的示例文件中缺少“stss”框。

答案 1 :(得分:0)

感谢 Markus 为我指明了正确的方向。在 Android 中使用 MediaMuxer 时,您需要在要作为同步帧的那些帧上将 BufferInfo.flags 设置为 MediaCodec.BUFFER_FLAG_KEY_FRAME。我设置了这个,但只在第一帧。

我不清楚 MediaCodec 中的 I 帧如何与复用器中的同步帧相关联。它们实际上是同一回事。当您使用 MediaCodec 对视频进行编码时,您可以指明哪些帧将用作 I 帧。然而,这不是必需的。事实上,在我的应用程序中,无法设置它,因为编码器从硬件画布接收它的位图图像,并且不可能为此在 MediaCodec 中设置 BufferInfo.flags。即使您可以设置它,MediaCodec 除了将它传递给编码输出之外,也不会对它做任何事情。当您将编码的视频与编码的音频组合在一起时,您可以将其进一步传递给 MediaMuxer。因此,如果您希望您的视频获得支持,请确保您提供给多路复用器的那些帧将 BufferInfo.flags 设置为 MediaCodec.BUFFER_FLAG_KEY_FRAME

如果您生成的 mp4 缺少 Markus 指出的原子对象 stss,则某些视频播放器(如 MediaPlayer)将无法提供任何搜索功能。更高级的播放器,如 ExoPlayer、QuickTime 和 VLC 能够在没有关键帧的情况下进行搜索,但这通常会更慢。

相关问题