Android FFmpeg - 来自 MediaCodec 的 Mux 编码的 h264 帧

Bob*_*oss 5 android ffmpeg h.264 android-ffmpeg android-mediacodec

我正在尝试从 Android 相机(由 MediaCodec 编码)捕获 h264 帧并将它们传递给在同一设备上运行的 FFmpeg 进程。

我目前通过将我从 MediaCodec 接收到的编码字节数组写入名为 out.h264 的文件来实现。

像这样:

    FileOutputStream fosVideo = new ...

    ...

    // encoder callback
    @Override
    public void onVideoData(ByteBuffer h264Buffer, MediaCodec.BufferInfo info) {
        fosVideo.write(h264Buffer);
    }
Run Code Online (Sandbox Code Playgroud)

在写入 h264 文件时,我启动 FFmpeg 进程并提供 h264 文件作为输入。

ffmpeg -re -i out.h264 -c:v copy -r 30 -loglevel 48 a.mp4
Run Code Online (Sandbox Code Playgroud)

我也试过

ffmpeg -re -framerate 25 -i out.h264 -c:v copy -r 30 -loglevel 48 a.mp4
Run Code Online (Sandbox Code Playgroud)

FFmpeg 进程在 10 秒到几分钟之间运行,然后突然停止:

frame=  330 fps= 29 q=31.0 size=     512kB time=00:00:10.98 bitrate= 381.8kbits/s dup=55 drop=0 speed=0.972x    
[h264 @ 0xf1863800] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
[h264 @ 0xf1863b80] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
[h264 @ 0xf1863f00] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
*** 1 dup!
[h264 @ 0xf1864280] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
Clipping frame in rate conversion by 0.199989
[h264 @ 0xf1864600] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
[h264 @ 0xf1862a00] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
[h264 @ 0xf1862d80] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
[h264 @ 0xf1863100] nal_unit_type: 1(Coded slice of a non-IDR picture), nal_ref_idc: 1
*** 1 dup!
Clipping frame in rate conversion by 0.199989
*** 1 dup!
frame=  347 fps= 29 q=31.0 size=     768kB time=00:00:11.53 bitrate= 545.5kbits/s dup=58 drop=0 speed=0.974x    
Clipping frame in rate conversion by 0.199989
[out_0_0 @ 0xf182e1e0] EOF on sink link out_0_0:default.
No more output streams to write to, finishing.
frame=  349 fps= 29 q=24.8 Lsize=     920kB time=00:00:17.68 bitrate= 426.1kbits/s dup=58 drop=0 speed=1.48x    
video:631kB audio:282kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.732886%
Input file #0 (/storage/emulated/0/MOVIES/out.h264):
Input stream #0:0 (video): 291 packets read (6065016 bytes); 291 frames decoded; 
Total: 291 packets (6065016 bytes) demuxed
Input file #1 (/storage/emulated/0/MOVIES/out.aac):
Input stream #1:0 (audio): 830 packets read (289119 bytes); 
Total: 830 packets (289119 bytes) demuxed
Output file #0 (/storage/emulated/0/hls/a.mp4):
Output stream #0:0 (video): 349 frames encoded; 349 packets muxed (645952 bytes); 
Output stream #0:1 (audio): 830 packets muxed (289119 bytes); 
Total: 1179 packets (935071 bytes) muxed
291 frames successfully decoded, 0 decoding errors
Run Code Online (Sandbox Code Playgroud)

即使 out.h264 文件仍在记录中。就好像 ffmpeg 进程认为文件已经结束。

知道它是什么吗?