"faststart" causes invalid MP4 file to write I set the format format for the video as follows: AVOutputFormat* outputFormat = ...">

FFmpeg "movflags"> "faststart" causes invalid MP4 file to write

I set the format format for the video as follows:

AVOutputFormat* outputFormat = ffmpeg.av_guess_format(null, "output.mp4", null); AVCodec* videoCodec = ffmpeg.avcodec_find_encoder(outputFormat->video_codec); AVFormatContext* formatContext = ffmpeg.avformat_alloc_context(); formatContext->oformat = outputFormat; formatContext->video_codec_id = videoCodec->id; ffmpeg.avformat_new_stream(formatContext, videoCodec); 

Here's how I set up the codec context:

 AVCodecContext* codecContext = ffmpeg.avcodec_alloc_context3(videoCodec); codecContext->bit_rate = 400000; codecContext->width = 1280; codecContext->height = 720; codecContext->gop_size = 12; codecContext->max_b_frames = 1; codecContext->pix_fmt = videoCodec->pix_fmts[0]; codecContext->codec_id = videoCodec->id; codecContext->codec_type = videoCodec->type; codecContext->time_base = new AVRational { num = 1, den = 30 }; 

I use the following code to configure the option "movflags"> "faststart" for the video title:

 AVDictionary* options = null; int result = ffmpeg.av_dict_set(&options, "movflags", "faststart", 0); int writeHeaderResult = ffmpeg.avformat_write_header(formatContext, &options); 

The file opens and the header is written as follows:

 if ((formatContext->oformat->flags & ffmpeg.AVFMT_NOFILE) == 0) { int ioOptionResult = ffmpeg.avio_open(&formatContext->pb, "output.mp4", ffmpeg.AVIO_FLAG_WRITE); } int writeHeaderResult = ffmpeg.avformat_write_header(formatContext, &options); 

After that, I record each video frame as follows:

 outputFrame->pts = frameIndex; packet.flags |= ffmpeg.AV_PKT_FLAG_KEY; packet.pts = frameIndex; packet.dts = frameIndex; int encodedFrame = 0; int encodeVideoResult = ffmpeg.avcodec_encode_video2(codecContext, &packet, outputFrame, &encodedFrame); if (encodedFrame != 0) { packet.pts = ffmpeg.av_rescale_q(packet.pts, codecContext->time_base, m_videoStream->time_base); packet.dts = ffmpeg.av_rescale_q(packet.dts, codecContext->time_base, m_videoStream->time_base); packet.stream_index = m_videoStream->index; if (codecContext->coded_frame->key_frame > 0) { packet.flags |= ffmpeg.AV_PKT_FLAG_KEY; } int writeFrameResult = ffmpeg.av_interleaved_write_frame(formatContext, &packet); } 

After that I write a trailer:

 int writeTrailerResult = ffmpeg.av_write_trailer(formatContext); 

The file ends the recording, and everything closes and is released correctly. However, the MP4 file cannot be played (even VLC cannot play it). AtomicParsley.exe will also not display file information.

DLLs used for AutoGen library:

 avcodec-56.dll avdevice-56.dll avfilter-5.dll avformat-56.dll avutil-54.dll postproc-53.dll swresample-1.dll swscale-3.dll 
+5
source share
1 answer

Using faststart seems to cause a problem in itself. As long as you properly size your segment using "movflags" ("frag_duration" in my case), this should be fine.

In addition, it would be better to make sure that both streams in the video are correctly synchronized. See this question for answers regarding this.

+1
source

All Articles