2,543
社区成员
发帖
与我相关
我的任务
分享
[align=left]AVOutputFormat *ofmt = NULL;
AVFormatContext *ofmt_ctx = NULL;
AVStream *out_stream = NULL;
AVPacket pkt = { 0 };
HI_S32 size = 0;
char *filename = "/share/test.avi";
HI_U8 tmp[66666] = {0};
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
//ff_avi_muxer
/* allocate the output media context */
avformat_alloc_output_context2(&ofmt_ctx, NULL, "avi", filename);
if (!ofmt_ctx) {
LOGE("Could not deduce output format from file extension: using AVI.\n");
avformat_alloc_output_context2(&ofmt_ctx, NULL, "avi", filename);
}
if(!ofmt_ctx) {
goto exit;
}
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream) {
LOGE( "Failed allocating output stream\n");
//ret = AVERROR_UNKNOWN;
goto exit;
}
//AVCodecContext *avctx = out_stream->codec;
out_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
//avctx->codec_tag = AV_CODEC_FLAG_GLOBAL_HEADER;
/*此处,需指定编码后的H264数据的分辨率、帧率及码率*/
out_stream->codecpar->codec_id = AV_CODEC_ID_H264;
out_stream->codecpar->bit_rate = 2000000;
out_stream->codecpar->width = 1920;
out_stream->codecpar->height = 1080;
out_stream->time_base.num = 1;
out_stream->time_base.den = 25;
//out_stream->codec->time_base.num = 1;
//out_stream->codec->time_base.den = 25;
/* print output stream information*/
av_dump_format(ofmt_ctx, 0, filename, 1);
if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
{
s32Ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
if (s32Ret < 0) {
LOGE( "Could not open output file '%s'\n", filename);
goto exit;
}
LOGD("Open output file success!\n");
}
s32Ret = avformat_write_header(ofmt_ctx, NULL);
if (s32Ret < 0) {
LOGE( "write avi file header failed\n");
goto exit;
}
LOGD("Write avi header success!\n");
...
while(1)
{
...
av_init_packet(&pkt);
#if 1
// determine whether the I frame
if(0x61 != pVStream.pstPack[i].pu8Addr[4]) {
pkt.flags |= AV_PKT_FLAG_KEY;
} else { /* p frame*/
pkt.flags = 0;
}
pkt.dts = pkt.pts = AV_NOPTS_VALUE;
pkt.size = pVStream.pstPack[i].u32Len;
pkt.data = pVStream.pstPack[i].pu8Addr;
if(!pkt.data) {
printf("no data\n");
}
//Write
s32Ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (s32Ret < 0) {
//LOGE("muxer error");
av_strerror(s32Ret, pkt.data, 1024);
}
#endif
av_packet_unref(&pkt);
}
...
s32Ret = av_write_trailer(ofmt_ctx);