首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >avformat_write_header在ffmepg中不能正常工作。

avformat_write_header在ffmepg中不能正常工作。
EN

Stack Overflow用户
提问于 2017-03-23 10:49:22
回答 1查看 1.1K关注 0票数 0

我正在使用FFMPEG进行mp4文件创建项目,我尝试基于FFMPEG转换视频数据包的流信息,重新编译代码,但是在转换成这样的文件后头部会损坏。

代码语言:javascript
复制
/* this code used to set the stream information */

   AVFormatContext *input_context,*output_context;
   AVDictionary *opt;
   AVStream *out_stream;
   AVCodecContext *newcontext = NULL;
   out_stream= avformat_new_stream(output_context,NULL);
                newcontext = avcodec_alloc_context3(codec);
                newcontext->codec_id=Output_fmt->video_codec;
                newcontext->bit_rate =in_stream->codec->bit_rate;
                newcontext->width    = in_stream->codec->width;
                newcontext->height   = in_stream->codec->height;
                newcontext->timecode_frame_start = in_stream->codec->timecode_frame_start;
                newcontext->gop_size      = in_stream->codec->gop_size;
                newcontext->profile       =  in_stream->codec->profile;
                newcontext->level         = in_stream->codec->level;
                newcontext->pix_fmt       = PIX_FMT_YUV420P;
                newcontext->frame_size = in_stream->codec->frame_size;
                newcontext->sample_fmt = in_stream->codec->sample_fmt;
                newcontext->sample_rate = in_stream->codec->sample_rate;
                 time_base =  (double)in_stream->time_base.num / (double)in_stream->time_base.den;
                 duration = (double)in_stream->duration * time_base * 1000.0;
                if (!out_stream) {
                    fprintf(stderr, "Failed allocating output stream\n");
                    ret = AVERROR_UNKNOWN;
                    return;
                }
               ret = avcodec_copy_context(out_stream->codec,newcontext);
                if (ret < 0) {
                    fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
                    goto end;
                }
                out_stream->codec->codec_tag = 0;
                if (output_context->oformat->flags & AVFMT_GLOBALHEADER)
                    out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

使用以下方法更改标题信息:

代码语言:javascript
复制
/* this code used to set the metadata */

    av_dict_set(&opt, "major_brand", "mp42", 0);
    av_dict_set(&opt, "minor_version","512" , 0);
    av_dict_set(&opt, "compatible_brands","isomiso2avc1mp41",0);
    av_dict_set(&opt, "comment","Hash=855738390",0);
    output_context->metadata = opt;
    ret = avformat_write_header(output_context,NULL);

创建mp4文件后,在终端中使用ffmpeg检查文件。出现这样的错误:

/this错误消息/

mpeg4 @ 0x7ff2b9811c00报头重复了39次mov、mp4、m4a、3GP、3G2、mj2 @ 0x7ff2ba800000对流0失败的mov解码,mp4、m4a、3GP、3G2、mj2 @ 0x7ff2ba800000无法为流0找到编解码器参数(视频: mpeg4 (mp4v / 0x7634706D)、none、376 kb/s):未指定的大小考虑增加“分析”和“预配置”选项的值。

EN

回答 1

Stack Overflow用户

发布于 2017-03-26 17:55:19

最简单的方法就是下载一个免费的十六进制编辑器(针对特定的O.S)。接下来是使用FFmpeg的桌面(命令行)版本(下载静态构建)。

  • 使用命令行FFmpeg将源代码转换为MP4 (即: as mp4_ffmpeg.mp4)
  • 使用代码将源代码转换为MP4 (即: as mp4_code.mp4)
  • 同时打开mp4_ffmpeg.mp4mp4_code.mp4并比较字节。工作的应该是mp4_ffmpeg.mp4,所以与代码生成的字节有什么不同?

需要寻找的东西:

  • 都是从ftyp开始
  • moov是头,应该在开始时(有时是在mdat后面,它将所有a/v数据放在一个块中)。要将任何mp4的头移动到前面或开始字节,然后在命令行中使用-movflags +faststart,例如在命令行中: ffmpeg -i myfile.avi -movflags +faststart newfile.mp4)
  • 在每个单词moovmdat之前,前4个字节是跳过单词的4个字母之后的大小(以字节为单位).这些尺码对吗?
  • 是否定义了所有MP4原子(元数据部分)?它们与FFmpeg为其版本的MP4转换生成的产品相匹配?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42973869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档