[FFmpeg-devel] writing h.264 raw frames to mp4
Nisar Ahmed
nisar.med at gmail.com
Wed Aug 7 20:35:25 CEST 2013
Dear list,
I am writing my first libav program and facing problem in muxing h.264 raw
frames to mp4.
I referred to muxing.c sample but that sample encodes frames to h.264
before writing to file however in my case I dont have to encoding, the
frames are already compressed.
I need to know the what steps do I need to follow in order to create
correct mp4 movie
currently I am doing the following
AVOutputFormat* outputFormat
AVFormatContext* formatContext
char* filename = "c:\\export.mp4";
outputFormat = av_guess_format(NULL, filename, NULL);
formatContext = avformat_alloc_context();
AVCodec* codec = avcodec_find_encoder(outputFormat->video_codec);
AVStream* st = avformat_new_stream(formatContext , codec);
c = st->codec;
c->bit_rate = 400000;
c->width = 720;
c->height = 480;
c->time_base.den = 2997;
c->time_base.num = 100;
c->gop_size = 12; /* emit one intra frame every twelve frames at most
*/
c->pix_fmt = AV_PIX_FMT_YUV420P;
av_dump_format(formatContext, 0, filename, 1);
if (!(outputFormat->flags & AVFMT_NOFILE)) {
if (avio_open(&formatContext->pb, filename, AVIO_FLAG_WRITE) < 0) {
return 1;
}
}
/* Write the stream header, if any. */
avformat_write_header(formatContext, NULL);
The following code writes the data in another callback function having data
and dataLen as parameters.. data is the pointer to h.264 frame data and
dataLen is the length in bytes
AVPacket pkt = { 0 };
av_init_packet(&pkt);
pkt.stream_index = 0;
pkt.flags = 0;
pkt.pts = videoFrameNumber;
pkt.dts = videoFrameNumber;
pkt.size = dataLen;
pkt.data = data;
av_interleaved_write_frame(formatContext, &pkt);
videoFrameNumber += 100;
and finally
av_write_trailer(formatContext);
int i = 0;
for (i = 0; i < formatContext->nb_streams; i++)
{
av_freep(&formatContext->streams[i]->codec);
av_freep(&formatContext->streams[i]);
}
if (!(formatContext->oformat->flags & AVFMT_NOFILE))
{
avio_close(formatContext->pb);
}
av_free(formatContext);
The movie is created but when I run
ffmpeg -i c:\output.mp4 the following error
Could not find codec parameters for stream 0 (Video: h264 (avc1 /
0x31637661), 720x480, 318244 kb/s): unspecified pixel format
Please help!
More information about the ffmpeg-devel
mailing list