前一篇介绍过用x264编码文件,ffmpeg内部也是用x264编码,这里记录下简单的用ffmpeg的编码
直接上代码:
-
onst char* SRC_FILE = "1.mp4";
-
const char* OUT_FILE = "outfile.h264";
-
-
typedef struct X264Context {
-
AVClass *pclass;
-
x264_param_t params;
-
x264_t *enc;
-
x264_picture_t pic;
-
uint8_t *sei;
-
int sei_size;
-
char *preset;
-
char *tune;
-
char *profile;
-
char *level;
-
int fastfirstpass;
-
char *wpredp;
-
char *x264opts;
-
float crf;
-
float crf_max;
-
int cqp;
-
int aq_mode;
-
float aq_strength;
-
char *psy_rd;
-
int psy;
-
int rc_lookahead;
-
int weightp;
-
int weightb;
-
int ssim;
-
int intra_refresh;
-
int bluray_compat;
-
int b_bias;
-
int b_pyramid;
-
int mixed_refs;
-
int dct8x8;
-
int fast_pskip;
-
int aud;
-
int mbtree;
-
char *deblock;
-
float cplxblur;
-
char *partitions;
-
int direct_pred;
-
int slice_max_size;
-
char *stats;
-
int nal_hrd;
-
char *x264_params;
-
} X264Context;
-
-
-
int main()
-
{
-
FILE *yuv_file = fopen("yuv_file", "ab");
-
if (!yuv_file)
-
return 0;
-
av_register_all();
-
AVFormatContext* pFormat = NULL;
-
if (avformat_open_input(&pFormat, SRC_FILE, NULL, NULL) < 0)
-
{
-
return 0;
-
}
-
AVCodecContext* video_dec_ctx = NULL;
-
AVCodec* video_dec = NULL;
-
if (avformat_find_stream_info(pFormat, NULL) < 0)
-
{
-
return 0;
-
}
-
av_dump_format(pFormat, 0, SRC_FILE, 0);
-
video_dec_ctx = pFormat->streams[0]->codec;
-
video_dec = avcodec_find_decoder(video_dec_ctx->codec_id);
-
if (avcodec_open2(video_dec_ctx, video_dec, NULL) < 0)
-
{
-
return 0;
-
}
-
-
AVFormatContext* pOFormat = NULL;
-
AVOutputFormat* ofmt = NULL;
-
if (avformat_alloc_output_context2(&pOFormat, NULL, NULL, OUT_FILE) < 0)
-
{
-
return 0;
-
}
-
ofmt = pOFormat->oformat;
-
if (avio_open(&(pOFormat->pb), OUT_FILE, AVIO_FLAG_READ_WRITE) < 0)
-
{
-
return 0;
-
}
-
AVCodecContext *video_enc_ctx = NULL;
-
AVCodec *video_enc = NULL;
-
video_enc = avcodec_find_encoder(AV_CODEC_ID_H264);
-
AVStream *video_st = avformat_new_stream(pOFormat, video_enc);
-
if (!video_st)
-
return 0;
-
video_enc_ctx = video_st->codec;
-
video_enc_ctx->profile = FF_PROFILE_H264_HIGH;
-
video_enc_ctx->width = video_dec_ctx->width;
-
video_enc_ctx->height = video_dec_ctx->height;
-
video_enc_ctx->pix_fmt = PIX_FMT_YUV420P;
-
video_enc_ctx->time_base.num = 1;
-
video_enc_ctx->time_base.den = 25;
-
video_enc_ctx->bit_rate = 400000;
-
video_enc_ctx->gop_size = 250;
-
video_enc_ctx->max_b_frames = 10;
-
video_enc_ctx->qmin = 10;
-
video_enc_ctx->qmax = 51;
-
((X264Context*)(video_enc_ctx->priv_data))->preset = "superfast";
-
((X264Context*)(video_enc_ctx->priv_data))->tune = "zerolatency";
-
if (avcodec_open2(video_enc_ctx, video_enc, NULL) < 0)
-
{
-
printf("编码器打开失败!\n");
-
return 0;
-
}
-
-
-
printf("Output264video Information====================\n");
-
av_dump_format(pOFormat, 0, OUT_FILE, 1);
-
printf("Output264video Information====================\n");
-
avformat_write_header(pOFormat, NULL);
-
AVPacket *pkt = new AVPacket();
-
av_init_packet(pkt);
-
AVFrame *pFrame = avcodec_alloc_frame();
-
int ts = 0;
-
while (1)
-
{
-
if (av_read_frame(pFormat, pkt) < 0)
-
{
-
avio_close(pOFormat->pb);
-
av_write_trailer(pOFormat);
-
delete pkt;
-
return 0;
-
}
-
if (pkt->stream_index == 0)
-
{
-
-
int got_picture = 0, ret = 0;
-
ret = avcodec_decode_video2(video_dec_ctx, pFrame, &got_picture, pkt);
-
if (ret < 0)
-
{
-
delete pkt;
-
return 0;
-
}
-
pFrame->pts = ts++;
-
if (got_picture)
-
{
-
AVPacket *tmppkt = new AVPacket;
-
av_init_packet(tmppkt);
-
int size = video_enc_ctx->width*video_enc_ctx->height * 3 / 2;
-
char* buf = new char[size];
-
memset(buf, 0, size);
-
tmppkt->data = (uint8_t*)buf;
-
tmppkt->size = size;
-
ret = avcodec_encode_video2(video_enc_ctx, tmppkt, pFrame, &got_picture);
-
if (ret < 0)
-
{
-
avio_close(pOFormat->pb);
-
delete buf;
-
return 0;
-
}
-
if (got_picture)
-
{
-
printf(".");
-
ret = av_interleaved_write_frame(pOFormat, tmppkt);
-
delete tmppkt;
-
delete buf;
-
}
-
}
-
}
-
-
}
-
avcodec_free_frame(&pFrame);
-
return 0;
-
}
阅读(642) | 评论(0) | 转发(0) |