如何通过libavformat将H264流复制到MP4文件中

问题描述 投票:3回答:1

我想实现一个首先解码多媒体文件的应用程序(例如test.mp4文件,视频编解码器ID是H264),获取视频流和音频流,然后在音频流中做一些不同的,最后编码视频流(使用libx264)和音频流进入结果文件(result.mp4)。为了提高效率,我省略了视频流的解码和编码,我通过函数“av_read_frame”获取视频包,然后通过函数“av_write_frame”将其直接输出到结果文件中。但是输出文件中没有图片,输出文件的大小相当小。

我跟踪了ffmpeg代码并发现在函数“av_write_frame-> mov_write_packet-> ff_mov_write_packet”中,它将调用函数“ff_avc_parse_nal_units”来获取nal单元的大小,但返回值非常小(例如208字节)。

我发现MP4文件中的H264流没有以Annex-B格式存储,因此无法找到起始码(0x000001),现在我的问题是如何将H264流改为Annex-B格式,并使它工作吗?我手动在每一帧的开头添加了起始码,但它仍然不起作用。

任何人都可以给我任何暗示吗?非常感谢。以下是与我类似的代码:

    // write the stream header, if any
    av_write_header(pFormatCtxEnc);

    .........
    /**
    * Init of Encoder and Decoder
    */

    bool KeyFlag = false;
    bool KeyFlagEx = false;
    // Read frames and save frames to disk
    int iPts = 1;
    av_init_packet(&packet);
    while(av_read_frame(pFormatCtxDec, &packet)>=0)
    {
            if (packet.flags == 1)
                    KeyFlag = true;

            if (!KeyFlag)
                    continue;

            if (m_bStop)
            {
                    break;
            }

            // Is this a packet from the video stream?
            if(packet.stream_index == videoStream)
            {
                    currentframeNum ++;
                    if (progressCB != NULL && currentframeNum%20 == 0)
                    {
                            float fpercent = (float)currentframeNum/frameNum;
                            progressCB(fpercent,m_pUser);
                    }

                    if (currentframeNum >= beginFrame && currentframeNum <= endFrane)
                    {
                            if (packet.flags == 1)
                                    KeyFlagEx = true;

                            if (!KeyFlagEx)
                                    continue;
                            packet.dts = iPts ++;
                            av_write_frame(pFormatCtxEnc, &packet);

                    }
            }
            // Free the packet that was allocated by av_read_frame
    }

    // write the trailer, if any
    av_write_trailer(pFormatCtxEnc);

    /**
    * Release of encoder and decoder
    */

    return true;
ffmpeg h.264 mp4 libavformat
1个回答
0
投票

你可以试试这个:libavcodec/h264_mp4toannexb_bsf.c。它将没有起始码的比特流转换为带有起始码的比特流。

使用你的源文件,ffmpeg -i src.mp4 -vcodec copy -an dst.mp4有效吗?如果你添加-bsf h264_mp4toannexb它是否有效? (所有使用与您尝试以编程方式使用的ffmpeg相同的版本/版本)

© www.soinside.com 2019 - 2024. All rights reserved.