Ganesh kumar
Ganesh kumar

Reputation: 29

avformat_write_header is not working properly in ffmepg

I was working on mp4 file creation project using FFMPEG, i tried to convert the stream information of video packet based on FFMPEG muxing,remuxing code, but header get damaged after convert into so file is corrupted.

/* 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;

Changed the Header Information using:

/* 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);

after create the mp4 file check file using ffmpeg in terminal. getting Error like this:

/this error message/

[mpeg4 @ 0x7ff2b9811c00] header damaged Last message repeated 39 times [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7ff2ba800000] decoding for stream 0 failed [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7ff2ba800000] Could not find codec parameters for stream 0 (Video: mpeg4 (mp4v / 0x7634706D), none, 376 kb/s): unspecified size Consider increasing the value for the 'analyzeduration' and 'probesize' options.

Upvotes: 0

Views: 1443

Answers (1)

VC.One
VC.One

Reputation: 15936

Easiest thing is to just download a freeware hex editor (for your specific O.S). Next is use desktop (commandline) version of FFmpeg (download a static build)

  • Use the commandline FFmpeg to convert Source to MP4 (ie: as mp4_ffmpeg.mp4)
  • Use your code to convert Source to MP4 (ie: as mp4_code.mp4)
  • Open both mp4_ffmpeg.mp4 & mp4_code.mp4 and compare bytes. The working one should be mp4_ffmpeg.mp4 so what's different from bytes produced with your code?

Things to look for :

  • All begin with ftyp?
  • moov is header and should be at start (sometimes at back after mdat which holds all a/v data in one chunk. To move the header of any mp4 to front or beginning bytes then use -movflags +faststart for example in commandline use :
    ffmpeg -i myfile.avi -movflags +faststart newfile.mp4)

  • Before each of the words moov or mdat, the previous 4 bytes are the size (in bytes) after you skip the 4 letters of word... are these sizes correct?

  • Do you have all the MP4 atoms (metadata sections) defined? They match what FFmpeg produced for its version of the MP4 converting?

Upvotes: 0

Related Questions