gpt4 book ai didi

c - 如何使用 libAV 库(ffmpeg)将 H264 编码数据放入 .mp4 等容器中?

转载 作者:行者123 更新时间:2023-12-04 23:01:24 26 4
gpt4 key购买 nike

我一直在玩 libAV* 系列库,它与 FFMPEG 一起出现,我正在学习如何实现编码、解码、复用等。
我遇到了这个代码:https://libav.org/documentation/doxygen/master/encode_video_8c-example.html ,它使用编码器将 YUV 帧编码到文件中。在我的实现中,我将使用 avcodec_find_encoder(AV_CODEC_ID_H264) 将编码器更改为 H264但我不知道,如何使用 libav 库将此编码数据放入适当的容器(.mp4)中,以便输出文件可以由任何媒体播放器(如 VLC、QuickTime 等)播放...
有人可以帮我吗?每一个帮助将不胜感激!

最佳答案

您应该能够将帧写入您初始化的文件,如下所示:

// Have the library guess the format
AVOutputFormat *fmt = av_guess_format(NULL, "my_video.mp4", NULL);

// Set the AVOutputFormat in your output context (init not shown)
pOutFormatContext->oformat = fmt;

// Open the output, error handling not shown
avio_open2(&pOutFormatContext->pb, "my_video.mp4", AVIO_FLAG_WRITE, NULL, NULL);

// Create output stream
AVStream * outStream;
outStream = avformat_new_stream(pOutFormatContext, NULL);

// Set any output stream options you would want, example of setting the aspect ratio from the input coded 'inCodecContext' (init not shown for this)
outStream->sample_aspect_ratio.num = inCodecContext->sample_aspect_ratio.num;
outStream->sample_aspect_ratio.den = inCodecContext->sample_aspect_ratio.den;

// There may be other things you want to set like the time base, but will depend on what you're doing

// Write the header to the context, error handling not shown
avformat_write_header(pOutFormatContext, NULL);

// Dump out the format and check
av_dump_format(pOutFormatContext, 0, pOutFormatContext->filename, 1);
然后,您必须读取数据包并对其进行编码,这听起来就像您已经在做的那样,然后当您写入输出上下文时,它应该写入您指定的容器中的文件。

关于c - 如何使用 libAV 库(ffmpeg)将 H264 编码数据放入 .mp4 等容器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69070438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com