1、简单说下整体流程
用avformat_open_input打开输入文件上下文,再用avformat_find_stream_info找到流信息,为输出分配AVFormatContext空间,然后根据流信息找到视频流,为输出创建流,并打开编解码器,写输出文件头,然后一个循环读帧---解码----压缩----写入,然后flush_encoder,最后写文件尾,释放资源----结束。
2、贴上代码
/*
*最简单的视频转码器(只处理视频)
*缪国凯 Mickel
*821486004@qq.com
*本程序实现从一个视频格式转码到另一个视频格式,只处理视频,音频忽略
*2015-5-7
*/
#include "stdafx.h"
#ifdef __cplusplus
extern"C"
{
#endif
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index);
int _tmain(int argc, _TCHAR* argv[])
{
AVFormatContext *in_fmt_ctx = NULL, *out_fmt_ctx = NULL;
AVStream *out_stream = NULL;
AVFrame *frame;
AVPacket pkt_in, pkt_out;
int video_index = -1;
if (argc < 3)
{
printf("error in input param");
getchar();
return -1;
}
av_register_all();
//input
if (avformat_open_input(&in_fmt_ctx, argv[1], NULL, NULL) < 0)
{
printf("can not open input file context");
goto end;
}
if (avformat_find_stream_info(in_fmt_ctx, NULL) < 0)
{
printf("can not find input stream info!\n");
goto end;
}
//output
avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, argv[2]);
if (!out_fmt_ctx)
{
printf("can not alloc output context!\n");
goto end;
}
//open decoder & new out stream & open encoder
for (int i = 0; i < in_fmt_ctx->nb_streams; i++)
{
if (in_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
//open decoder
if(0 > avcodec_open2(in_fmt_ctx->streams[i]->codec, avcodec_find_decoder(in_fmt_ctx->streams[i]->codec->codec_id), NULL))
{
printf("can not find or open decoder!\n");
goto end;
}
video_index = i;
//new stream
out_stream = avformat_new_stream(out_fmt_ctx, NULL);
if (!out_stream)
{
printf("can not new stream for output!\n");
goto end;
}
//set codec context param
out_stream->codec->codec = avcodec_find_encoder(out_fmt_ctx->oformat->video_codec);
out_stream->codec->height = in_fmt_ctx->streams[i]->codec->height;
out_stream->codec->width = in_fmt_ctx->streams[i]->codec->width;
//I do not know why the input file time_base is not correct
//out_stream->codec->time_base = in_fmt_ctx->streams[i]->codec->time_base;
out_stream->codec->time_base.num = in_fmt_ctx->streams[i]->avg_frame_rate.den;
out_stream->codec->time_base.den = in_fmt_ctx->streams[i]->avg_frame_rate.num;
out_stream->codec->sample_aspect_ratio = in_fmt_ctx->streams[i]->codec->sample_aspect_ratio;
// take first format from list of supported formats
out_stream->codec->pix_fmt = in_fmt_ctx->streams[i]->codec->pix_fmt;
out_stream->codec->pix_fmt = out_stream->codec->codec->pix_fmts[0];
//open encoder
if (!out_stream->codec->codec)
{
printf("can not find the encoder!\n");
goto end;
}
if ((avcodec_open2(out_stream->codec, out_stream->codec->codec, NULL)) < 0)
{
printf("can not open the encoder\n");
goto end;
}
if (out_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
break;
}
}
if (-1 == video_index)
{
printf("found no video stream in input file!\n");
goto end;
}
if (!(out_fmt_ctx->oformat->flags & AVFMT_NOFILE))
{
if(avio_open(&out_fmt_ctx->pb, argv[2], AVIO_FLAG_WRITE) < 0)
{
printf("can not open output file handle!\n");
goto end;
}
}
if(avformat_write_header(out_fmt_ctx, NULL) < 0)
{
printf("can not write the header of the output file!\n");
goto end;
}
av_init_packet(&pkt_in);
av_init_packet(&pkt_out);
frame = av_frame_alloc();
int got_frame, got_picture;
int frame_index = 0;
int i = 0;
while(1)
{
got_frame = -1;
got_picture = -1;
pkt_in.data = NULL;
pkt_in.size = 0;
if (av_read_frame(in_fmt_ctx, &pkt_in) < 0)
{
break;
}
if (avcodec_decode_video2(in_fmt_ctx->streams[video_index]->codec, frame, &got_frame, &pkt_in) < 0)
{
printf("can not decoder a frame");
break;
}
av_free_packet(&pkt_in);
if (got_frame)
{
frame->pts = i++;
pkt_out.data = NULL;
pkt_out.size = 0;
if (avcodec_encode_video2(out_stream->codec, &pkt_out, frame, &got_picture) < 0)
{
printf("can not encode aframe!\n");
break;
}
if (got_picture)
{
printf("Succeed to encode frame: %5d\tsize:%5d\n",frame_index,pkt_out.size);
pkt_out.stream_index = out_stream->index;
// pkt_out.dts = av_rescale_q_rnd(pkt_out.dts,
// out_fmt_ctx->streams[video_index]->codec->time_base,
// out_fmt_ctx->streams[video_index]->time_base,
// (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
// pkt_out.pts = av_rescale_q_rnd(pkt_out.pts,
// out_fmt_ctx->streams[video_index]->codec->time_base,
// out_fmt_ctx->streams[video_index]->time_base,
// (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
// pkt_out.duration = av_rescale_q(pkt_out.duration,
// out_fmt_ctx->streams[video_index]->codec->time_base,
// out_fmt_ctx->streams[video_index]->time_base);
frame_index++;
av_write_frame(out_fmt_ctx, &pkt_out);
av_free_packet(&pkt_out);
}
}
}
av_frame_free(&frame);
int ret = flush_encoder(out_fmt_ctx, 0);
if (ret < 0)
{
printf("Flushing encoder failed");
return -1;
}
//write file trailer
av_write_trailer(out_fmt_ctx);
//clean
avcodec_close(out_stream->codec);
avcodec_close(in_fmt_ctx->streams[video_index]->codec);
end:
avformat_close_input(&in_fmt_ctx);
if (out_fmt_ctx && !(out_fmt_ctx->oformat->flags & AVFMT_NOFILE))
{
avio_close(out_fmt_ctx->pb);
}
avformat_free_context(out_fmt_ctx);
return 0;
}
int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index)
{
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
CODEC_CAP_DELAY))
{
return 0;
}
while(1)
{
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_encode_video2(fmt_ctx->streams[stream_index]->codec, &enc_pkt, NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)
{
break;
}
if (!got_frame)
{
ret = 0;
break;
}
printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
/* mux encoded frame */
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
return ret;
}
3、工程下载地址
http://download.csdn.net/detail/dancing_night/8671679