首页 > 解决方案 > 编码 mp4 视频以支持搜索

问题描述

我的 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

标签: androidvideomp4h.264android-mediacodec

解决方案


MP4 播放器需要知道同步样本(I 帧或 IDR 帧)的位置。同步样本位置通常使用位于moov->trak->mdia->minf->stbl->stss.

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


推荐阅读