我的 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
不支持seek的视频:
**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
支持seek的视频:
**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
MP4 播放器需要知道同步样本(I 帧或 IDR 帧)的位置。同步样本位置通常由位于
moov->trak->mdia->minf->stbl->stss
的同步样本盒“stss”发出信号。
在您的示例文件中,缺少“stss”框。
感谢马库斯为我指明了正确的方向。在 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 能够在没有关键帧的情况下进行搜索,但这通常会更慢。
Markus 已经提供了问题的根本原因。
您可以使用以下 API 将 I 帧间隔设置为您的媒体格式,而不是修改 BufferInfo 中的标志。
KEY_I_FRAME_INTERVAL - 仅编码器,关键帧之间的时间间隔。
更多详细信息:https://developer.android.com/reference/android/media/MediaFormat