ffmpeg记录

这玩意资料实在太少

av_register_all();
avcodec_register_all();
AVCodecContext            *pOCtx= NULL;
AVCodec                    *pOCodex = NULL;
pOCodex = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
pOCtx= avcodec_alloc_context3(pOCodex);
pOCtx->bit_rate = 1500000;//400000
pOCtx->width = 640;
//640
pOCtx->height = 480;//480
AVRational rational = {1,25};
pOCtx->time_base= rational;
//pOCtx->time_base.den=10;
//pOCtx->time_base.num=1;
//pOCtx->codec_tag=0;
pOCtx->gop_size = 10; /* emit one intra frame every ten frames */
pOCtx->max_b_frames=1;
pOCtx->pix_fmt = PIX_FMT_YUV420P;
int ret = avcodec_open2(pOCtx,pOCodex,NULL);
if(ret < 0)
{
return;
}
AVFormatContext            *pIFormatCtx = NULL;
ret = avformat_open_input(&pIFormatCtx, imagefile, NULL, NULL);
if(ret < 0)
{
//Cant't open jpg file
return;
}
av_dump_format(pIFormatCtx, 0, imagefile, false);
AVCodecContext            *pICodecCtx;  //output codec context
pICodecCtx = pIFormatCtx->streams[0]->codec;
/*pICodecCtx->width = 640;
pICodecCtx->height = 480;
pICodecCtx->pix_fmt = PIX_FMT_YUV420P;*/
AVCodec *pICodec = avcodec_find_decoder(pICodecCtx->codec_id); //output codec
// Open codec
ret = avcodec_open2(pICodecCtx, pICodec,NULL);
if(ret < 0)
{
//Can't find the decoder
return;
}
AVFrame *pIFrame = avcodec_alloc_frame();
if (!pIFrame)
{
//Can't alloc the input frame
return ;
}
int bufSize = avpicture_get_size(PIX_FMT_YUVJ420P, pICodecCtx->width, pICodecCtx->height);
uint8_t *buffer = (uint8_t *) av_malloc(bufSize * sizeof(uint8_t));
avpicture_fill((AVPicture *) pIFrame, buffer, PIX_FMT_YUVJ420P, pICodecCtx->width, pICodecCtx->height);
FILE *outputFile;
outputFile=fopen(videoFile,"wb");
if(outputFile==NULL)
{
//TODO open file error
}
int outbuf_size = 100000;
uint8_t* outbuf = (uint8_t*)malloc(outbuf_size);
AVPacket packet;
int frameFinished;
int framesNumber = 0;
int out_size;
while (av_read_frame(pIFormatCtx, &packet) >= 0)
//if (av_read_frame(pIFormatCtx, &packet) >= 0)
{
if(packet.stream_index != 0)
continue;
ret = avcodec_decode_video2(pICodecCtx, pIFrame, &frameFinished, &packet);
if (ret > 0)
{
pIFrame->quality = 4;
for(int i=0;i<25;i++) {
fflush(stdout);
/* encode the image */
out_size = avcodec_encode_video(pOCtx, outbuf, outbuf_size, pIFrame);
fwrite(outbuf, 1, out_size, outputFile);
}
}
}
/* get the delayed frames */
for(int i=0; i<out_size; i++) {
fflush(stdout);
int out_size = avcodec_encode_video(pOCtx, outbuf, outbuf_size, NULL);
fwrite(outbuf, 1, out_size, outputFile);
}
/* add sequence end code to have a real mpeg file */
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, outputFile);
fclose(outputFile);
free(outbuf);

avcodec_close(pOCtx);
av_free(pOCtx);
av_free(pIFrame);

上面这代码记录:
一,当将宽和高设定为稍大一点的时候,会进入一种十分随机的状态,一个程序几次执行的结果竟然能不同,生成的文件通常会很大,不是根据宽高变大后得到的大,是超大。之所以执行结果不同,我估计就是和文件超大有关系面色因为进入了写入状态,程序退出的时间不同导致了结果不同
二,要将宽高设定为原图片的宽高

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值