我的安卓应用程序使用MediaCodec和MediaMuxer创建了一个MP4。我使用MediaPlayer播放视频。在视频播放期间,无法使用安卓的MediaPlayer搜索视频中的任何位置。更具体地说,seekTo函数将不起作用。使用其他应用程序播放视频和搜索有点粗略。一些应用程序似乎可以工作,而其他应用程序则不能。
我已经把我的mp4换成了我在相机上录制的视频,以及我在互联网上找到的各种视频,它们都没有遇到问题。事实上,库存相机应用程序可以生成MP4并让您查找,这清楚地表明编解码器的设置方式存在问题。这让我相信问题很可能出在用于创建视频的格式设置中。我尝试修改了一些设置,但没有成功,包括配置文件(同时使用基线和主配置文件)、配置文件级别、I帧间隔(GOP)以及比特率和视频大小。我还确保每个帧的呈现时间与帧速率完全匹配。这是我从不支持seek的视频和支持seek的视频(摄像头视频)中获得的信息。这些设置中是否有可能导致问题的原因?
可以在此处下载简短的测试文件。如果您在QuickTime或VLC中播放此命令,则seek会起作用:
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
发布于 2021-07-05 21:20:02
MP4播放器需要知道同步样本(I帧或IDR帧)的位置。同步采样位置通常由位于moov->trak->mdia->minf->stbl->stss
中的同步采样盒'stss‘发出信号。
在您的示例文件中缺少'stss‘框。
发布于 2021-07-05 23:58:18
感谢Markus为我指明了正确的方向。当使用安卓的MediaMuxer时,你需要在你想要作为同步帧的那些帧上将BufferInfo.flags
设置为MediaCodec.BUFFER_FLAG_KEY_FRAME
。我只在第一帧设置了这个。
我不清楚MediaCodec中的I帧是如何与多路复用器中的同步帧相关的。它们实际上是一回事。使用MediaCodec对视频进行编码时,可以指示哪些帧将充当I帧。然而,这不是必需的。事实上,在我的应用程序中,不可能设置它,因为编码器从硬件画布接收它的位图图像,并且不可能在MediaCodec中为此设置BufferInfo.flags
。即使您可以设置它,MediaCodec也不会对它做任何事情,只是将它传递给编码后的输出。当您将编码的视频和编码的音频组合在一起时,由您决定是否将其进一步传递给MediaMuxer。因此,如果您希望视频具有seek支持,请确保输入多路复用器的那些帧的BufferInfo.flags
设置为MediaCodec.BUFFER_FLAG_KEY_FRAME
如果您生成的mp4缺少Markus指出的atom对象stss
,则某些视频播放器(如MediaPlayer )将无法提供任何seek功能。虽然更高级的玩家,如ExoPlayer,QuickTime和VLC能够在没有关键帧的情况下进行搜索,但这通常会更慢。
https://stackoverflow.com/questions/68251958
复制相似问题