I am writing code to mux h264 file into mp4 by using latest FFMPEG 3.0. Muxing is working but the resultant mp4 file is not showing vedio, only the time is running when i play the resultant mp4 file. Kindly try to help to solve this. Please tell me what i am missing. Here is my code:
int main()
{
avcodec_register_all();
av_register_all();
// define AVOutputFormat
AVOutputFormat *avoutputFormat = NULL;
avoutputFormat = av_guess_format("mp4", NULL, NULL);
if (!avoutputFormat) {
fprintf(stderr, "Could not find suitable output format\n");
return 1;
}
AVFormatContext *avoutFmtCtx = NULL;
/*Below initialize AVFormatContext.*/
avformat_alloc_output_context2(&avoutFmtCtx, avoutputFormat, NULL, NULL);
avoutFmtCtx->oformat = avoutputFormat;
sprintf_s(avoutFmtCtx->filename, "%s", "Out.mp4"); /*Filling output file name..*/
if (avoutputFormat->video_codec == AV_CODEC_ID_NONE)
printf("\n Unsupported format...");
AVStream * avoutStrm = avformat_new_stream(avoutFmtCtx, 0);
if (!avoutStrm) {
_tcprintf(_T("FFMPEG: Could not alloc video stream\n"));
}
AVCodecContext *c = avoutStrm->codec;
c->codec_id = avoutputFormat->video_codec;
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->bit_rate = 2000*1000;
c->width = 1920;
c->height = 1080;
AVRational avr;
avr.den = 30;
avr.num = 1;
avoutStrm->time_base = c->time_base = av_add_q(avoutStrm->codec->time_base, avr);
// Some formats want stream headers to be separate
if(avoutFmtCtx->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
// Open the output container file
if (avio_open(&avoutFmtCtx->pb, avoutFmtCtx->filename, AVIO_FLAG_WRITE) < 0)
{
_tcprintf(_T("FFMPEG: Could not open '%s'\n"), avoutFmtCtx->filename);
}
m_pExtDataBuffer = (uint8_t*)av_malloc(1000 + 1000);
if(!m_pExtDataBuffer) {
_tcprintf(_T("FFMPEG: could not allocate required buffer\n"));
}
uint8_t SPSbuf[1000];
uint8_t PPSbuf[1000];
memcpy(m_pExtDataBuffer, SPSbuf, 1000);
memcpy(m_pExtDataBuffer + 1000, PPSbuf, 1000);
/* Codec "extradata" conveys the H.264 stream SPS and PPS info (MPEG2: sequence header is housed in SPS buffer, PPS buffer is empty)*/
c->extradata = m_pExtDataBuffer;
c->extradata_size = 1000 + 1000;
if(avformat_write_header(avoutFmtCtx,NULL)) {
_tcprintf(_T("FFMPEG: avformat_write_header error!\n"));
}
/* Here do writing data in loop...*/
int m_nProcessedFramesNum = 0;
while(1)
{
++m_nProcessedFramesNum;
AVPacket pkt;
av_init_packet(&pkt);
AVCodecContext *c = avoutStrm->codec;
avoutStrm->pts.val = m_nProcessedFramesNum;
pkt.stream_index = avoutStrm->index;
pkt.data = /*Filling h.264 data from here... This is valid h264 data*/
pkt.size = /*Filling valid h264 data size here...*/
av_new_packet(&pkt,pkt.size);
pkt.pts = m_nProcessedFramesNum*512;
pkt.dts = m_nProcessedFramesNum*512;
pkt.duration = 512;
// Write the compressed frame in the media file
if (av_interleaved_write_frame(avoutFmtCtx, &pkt)) {
_tcprintf(_T("FFMPEG: Error while writing video frame\n"));
}/*End of loop.*/
av_free_packet(&pkt);
}
av_write_trailer(avoutFmtCtx);
avio_close(avoutFmtCtx->pb);
avformat_free_context(avoutFmtCtx);
}