如何强制ffmpeg编码时输出一个关键帧
展开全部
如何强制ffmpeg编码时输出一个关键帧
AVCodecContext *c //编码器环境句柄AVFrame* f //需要编码的一帧视频 在编码前设置
f->pict_type=FF_I_TYPE;
f->key_frame=1;
然后编码
*outsize = avcodec_encode_video(c, temp, outbuf_size, f);
则编码之后通过如下参数判断是否为关键帧:
key_frame=c->coded_frame->key_frame;
pict_type=c->coded_frame->pict_type;
AVCodecContext *c //编码器环境句柄AVFrame* f //需要编码的一帧视频 在编码前设置
f->pict_type=FF_I_TYPE;
f->key_frame=1;
然后编码
*outsize = avcodec_encode_video(c, temp, outbuf_size, f);
则编码之后通过如下参数判断是否为关键帧:
key_frame=c->coded_frame->key_frame;
pict_type=c->coded_frame->pict_type;
2017-06-20
展开全部
int main()
{
SwsContext *pSWSCtx;
AVFormatContext *pFormatCtx;
const char *filename="sample.mpg";
int i,videoStream,y_size;
AVCodecContext *pCodecCtx;
AVFrame *pFrame;
AVFrame *pFrameRGB;
int numBytes,frameFinished;
uint8_t *buffer;
static AVPacket packet;
av_register_all();
if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
printf("error!\n");
if(av_find_stream_info(pFormatCtx)<0)
printf("error!\n");
dump_format(pFormatCtx, 0, filename, false);
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
printf("error!\n");// Didn't find a video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
AVCodec *pCodec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
printf("error!\n");
if(avcodec_open(pCodecCtx, pCodec)<0)
printf("error!\n");
pFrame=avcodec_alloc_frame();
pFrameRGB = avcodec_alloc_frame();
numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);
buffer=new uint8_t[numBytes];
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);
pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
i=0;
while(av_read_frame(pFormatCtx,&packet)>=0)
{
if(packet.stream_index==videoStream)
{
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);
if(frameFinished)
{
if(pFrame->key_frame==1)//这里取到关键帧数据
{
sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
i++;
}
}
}
av_free_packet(&packet);
}
av_free(pFrameRGB);
av_free(pFrame);
sws_freeContext(pSWSCtx);
avcodec_close(pCodecCtx);
av_close_input_file(pFormatCtx);
return 0;
}
{
SwsContext *pSWSCtx;
AVFormatContext *pFormatCtx;
const char *filename="sample.mpg";
int i,videoStream,y_size;
AVCodecContext *pCodecCtx;
AVFrame *pFrame;
AVFrame *pFrameRGB;
int numBytes,frameFinished;
uint8_t *buffer;
static AVPacket packet;
av_register_all();
if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
printf("error!\n");
if(av_find_stream_info(pFormatCtx)<0)
printf("error!\n");
dump_format(pFormatCtx, 0, filename, false);
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
printf("error!\n");// Didn't find a video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
AVCodec *pCodec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
printf("error!\n");
if(avcodec_open(pCodecCtx, pCodec)<0)
printf("error!\n");
pFrame=avcodec_alloc_frame();
pFrameRGB = avcodec_alloc_frame();
numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);
buffer=new uint8_t[numBytes];
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);
pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
i=0;
while(av_read_frame(pFormatCtx,&packet)>=0)
{
if(packet.stream_index==videoStream)
{
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);
if(frameFinished)
{
if(pFrame->key_frame==1)//这里取到关键帧数据
{
sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
i++;
}
}
}
av_free_packet(&packet);
}
av_free(pFrameRGB);
av_free(pFrame);
sws_freeContext(pSWSCtx);
avcodec_close(pCodecCtx);
av_close_input_file(pFormatCtx);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询