FFMPEG SDK 快速截取I帧

本文介绍了使用FFMPEG SDK来快速截取I帧的流程,包括如何根据输入时间寻求I帧,解码I帧并将其保存为JPEG。关键步骤包括av_seek_frame()、av_read_frame()和avcodec_decode_video2()的使用。同时,文章提醒注意时间戳单位转换、解码异步处理以及色彩空间转换带来的对比度问题。
摘要由CSDN通过智能技术生成

主要流程 : 

               根据输入的时间,向前seek到最近的I帧,读取I帧,解码,将得到的I帧写为JPEG保存。

主要接口:

          av_seek_frame();

          av_read_frame();

          avcodec_decode_video2();

注意问题:

       1.输入的时间戳单位是秒,需要转换成ffmpeg的时间戳,输入到av_seek_frame(),主要使用av_rescale()实现转换。

       2.ffmpeg解码另开线程,所以decode不是同步返回结果,代码中使用while(){sleep()}的逻辑获取解码结果。

       3.解码得到的frame需要先转换成YUVJ420p,利用sws_scale()实现,转换成YUV420p存在color_range的问题,导致输出的jpeg图像与原图像存在对比度差异,详见: 点击打开链接

//
//  main.cpp
//  test_keyframe
//
//  Created by shiyao.xsy on 16/1/26.
//  Copyright © 2016年 rq. All rights reserved.
//

#include <iostream>
#include <stdio.h>
extern "C" {
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavcodec/avcodec.h"
#include "libavutil/time.h"
}
int SavetoJPEG(AVFrame *pFrameYUV,AVStream *pVStream,char *filepath,int width, int height){
    AVFormatContext* pFormatCtx;
    AVOutputFormat* fmt;
    AVStream* video_st;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;
    
    uint8_t* picture_buf;
    AVFrame* picture;
    AVPacket pkt;
    int y_size;
    int got_picture=0;
    int size;
    
    int ret=0;
    
    int in_w=width,in_h=height;    //YUV's width and height
    char* out_file = filepath;    //Output file
    
    //Method 1
    pFormatCtx = avformat_alloc_context();
    //Guess format
    fmt = av_guess_format("mjpeg", NULL, NULL);
    pFormatCtx->oformat = fmt;
    //Output URL
    if (avio_open(&pFormatCt
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值