FFmpeg实时封装海思H264裸流

weixin_42018622 2019-01-14 08:51:29
这边第一次用ffmpeg封装海思3531a的裸流成AVI时,文件能生成,VLC播放提示文件索引损坏,但是能播放。还有右键文件属性看不到任何媒体信息,比如分辨率,码率,帧率以及编码器等等信息。代码已经手动填充了AVCodecContext!下面贴出代码。。

[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);
...全文
1310 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
詆調 2019-09-20
  • 打赏
  • 举报
回复
想问下pts时间信息怎么填写的
weixin_42018622 2019-03-15
  • 打赏
  • 举报
回复
终于抽时间弄好了,原来是一个很低级的错误,u盘没有卸载就拔出来了,导致avi文件异常。汗颜~~~~~~~~
smwhotjay 2019-01-16
  • 打赏
  • 举报
回复

avi格式也有很多吧,不太清楚。。
rightorwrong 2019-01-15
  • 打赏
  • 举报
回复
不清楚x-msvideo是标准的
weixin_42018622 2019-01-14
  • 打赏
  • 举报
回复
引用 2 楼 rightorwrong 的回复:
写avi文件时,没有写入标准avi信息

版主您好,您是指下面截图的吗?
rightorwrong 2019-01-14
  • 打赏
  • 举报
回复
写avi文件时,没有写入标准avi信息
weixin_42018622 2019-01-14
  • 打赏
  • 举报
回复
上面写的一个代码用例,请前辈帮忙看看问题在哪,由于对ffmpeg比较陌生,有没有人遇到同样的问题?

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧