FFMPEG SDK 快速截取I帧

主要流程 : 

               根据输入的时间,向前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
    评论
要使用FFmpeg截取I,可以通过以下步骤实现: 1. 首先,将输入的时间戳转换为FFmpeg的时间戳格式。在FFmpeg中,时间戳的单位是基于AV_TIME_BASE的,而输入的时间戳单位是秒。可以使用av_rescale()函数将其进行转换。 2. 接下来,需要使用FFmpeg的av_seek_frame()函数进行关键的定位。关键通常是I,因此可以通过定位关键来实现截取I的目的。 3. 在av_seek_frame()函数中,需要指定相关参数,包括输入的AVFormatContext、流索引、目标时间戳,以及seek_flag参数。 4. 设置seek_flag参数为AVSEEK_FLAG_BACKWARD,表示向后查找关键。这样可以确保定位到离目标时间戳最近的关键。 5. 调用av_seek_frame()函数后,可以获取到定位到的关键的时间戳。 请注意,以上步骤仅提供了一种实现截取I的方法,具体的实现方式可能会因应用场景和需求的不同而有所差异。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [FFMPEG SDK 快速截取I](https://blog.csdn.net/xiongsy1990/article/details/50605780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [QT+FFMPEG 从摄像机拉取rtsp裸h264流,播放并保存到本地](https://download.csdn.net/download/asdasfdgdhh/13451538)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值