Dear all,
I am facing an issue when trying to encode an H264 video to MPEG4. The output
file is playing great in VLC player but with Quicktime I only see black frame.
Here below my code:
//----------------------START OF CODE------------------------------//
- (void) videoOutFromFile:(NSString *)filenameString toFile:(NSString
*)outfilenameString
{
const char *inFileName = [filenameString UTF8String];
const char *outFileName = [outfilenameString UTF8String];
av_register_all();
AVFormatContext* inContainer = NULL;
if(avformat_open_input(&inContainer, inFileName, NULL, NULL) < 0)
exit(1);
if(avformat_find_stream_info(inContainer, NULL) < 0)
exit(1);
int videoStreamIndex = -1;
for (unsigned int i = 0; i < inContainer->nb_streams; ++i)
{
if (inContainer->streams[i] && inContainer->streams[i]->codec &&
inContainer->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1)
exit(1);
AVFormatContext* outContainer = NULL;
if(avformat_alloc_output_context2(&outContainer, NULL, NULL, outFileName) <
0)
exit(1);
AVStream const *const inStream = inContainer->streams[videoStreamIndex];
AVCodec *const decoder = avcodec_find_decoder(inStream->codec->codec_id);
if(!decoder)
exit(1);
if(avcodec_open2(inStream->codec, decoder, NULL) < 0)
exit(1);
AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
AVStream *outStream = avformat_new_stream(outContainer, encoder);
if(!outStream)
exit(1);
avcodec_get_context_defaults3(outStream->codec, encoder);
outStream->codec->sample_aspect_ratio = outStream->sample_aspect_ratio =
inStream->sample_aspect_ratio;
if(outContainer->oformat->flags & AVFMT_GLOBALHEADER)
outStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
outStream->codec->coder_type = AVMEDIA_TYPE_VIDEO;
outStream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
outStream->codec->width = inStream->codec->width;
outStream->codec->height = inStream->codec->height;
outStream->codec->codec_id = encoder->id;
outStream->codec->bit_rate = 40000000;
outStream->codec->time_base= (AVRational){1,25};
outStream->codec->max_b_frames = FF_MAX_B_FRAMES;
if(avcodec_open2(outStream->codec, encoder, NULL) < 0)
exit(1);
if(avio_open(&outContainer->pb, outFileName, AVIO_FLAG_WRITE) < 0)
exit(1);
AVFrame *decodedFrame = avcodec_alloc_frame();
if(!decodedFrame)
exit(1);
av_dump_format(inContainer, 0, inFileName,0);
avformat_write_header(outContainer, NULL);
AVPacket decodePacket, encodedPacket;
av_init_packet(&decodePacket);
decodePacket.data = NULL;
decodePacket.size = 0;
double video_pts, video_dts;
int curFrameNum = 1;
int frame_count = 0;
int got_frame, len;
while(av_read_frame(inContainer, &decodePacket)>=0)
{
if (decodePacket.stream_index == videoStreamIndex)
{
len = avcodec_decode_video2(inStream->codec, decodedFrame,
&got_frame, &decodePacket);
if(len < 0)
exit(1);
if(got_frame)
{
av_init_packet(&encodedPacket);
encodedPacket.data = NULL;
encodedPacket.size = 0;
if(avcodec_encode_video2(outStream->codec, &encodedPacket,
decodedFrame, &got_frame) < 0)
exit(1);
if(got_frame )
{
if (outStream->codec->coded_frame->key_frame)
{
encodedPacket.flags |= AV_PKT_FLAG_KEY;
}
encodedPacket.stream_index = outStream->index;
encodedPacket.pts = av_rescale_q(curFrameNum,
outStream->codec->time_base, outStream->time_base);
encodedPacket.dts = av_rescale_q(curFrameNum,
outStream->codec->time_base, outStream->time_base);
if(av_interleaved_write_frame(outContainer, &encodedPacket)
< 0)
exit(1);
else
curFrameNum +=1;
}
frame_count+=1;
av_free_packet(&encodedPacket);
}
}
}
av_free_packet(&decodePacket);
av_write_trailer(outContainer);
avio_close(outContainer->pb);
avcodec_free_frame(&decodedFrame);
avformat_free_context(outContainer);
avformat_close_input(&inContainer);
}
//----------------------END OF CODE------------------------------//
Thanks a lot for any help as I am struggling on this since one week.
Regards,
Francois
_______________________________________________
libav-api mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-api