ffmpeg: 從視頻裏提取視頻幀,並保存爲圖片文件

  1. 用ffmpeg處理視頻時,有時需要從視頻裏提取某個時間的一幀視頻數據,這時需要用到ffmpeg的一個關鍵函數,av_seek_frame。
    av_seek_frame原型如下:
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags){}
參數1: s操作上下文;
參數2: stream_index 流索引,當流索引爲-1時,會選擇一個默認流,時間戳會從以AV_TIME_BASE爲單位
參數3: timestamp將要定位處的時間戳
參數4: flags功能flag 
AVSEEK_FLAG_BACKWARD  seek到請求的時間戳之前最近的關鍵幀
AVSEEK_FLAG_BYTE      基於字節位置的查找 
AVSEEK_FLAG_ANY       是可以seek到任意幀,注意不一定是關鍵幀,因此使用時可能會導致花屏    
AVSEEK_FLAG_FRAME     是基於幀數量快進 
  1. 示例很簡單,工程代碼如下:
    保存文件可以爲調試代碼用,函數如下
int saveAsJPEG(AVFrame* pFrame, int width, int height, int index)
{
    char out_file[256] = {0};
    sprintf_s(out_file, sizeof(out_file), "%s%d.jpg", "", index);
    AVFormatContext* pFormatCtx = avformat_alloc_context();
    pFormatCtx->oformat = av_guess_format("mjpeg", nullptr, nullptr);
    if( avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
    {
        printf("Couldn't open output file.");
        return -1;
    }
    AVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);
    if( pAVStream == nullptr )
    {
        return -1;
    }
    AVCodecContext* pCodecCtx = pAVStream->codec;
    pCodecCtx->codec_id   = pFormatCtx->oformat->video_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
    pCodecCtx->pix_fmt    = AV_PIX_FMT_YUVJ420P;
    pCodecCtx->width      = width;
    pCodecCtx->height     = height;
    pCodecCtx->time_base.num = 1;
    pCodecCtx->time_base.den = 25;
    //打印輸出相關信息
    av_dump_format(pFormatCtx, 0, out_file, 1);
    //================================== 查找編碼器 ==================================//
    AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    if( !pCodec )
    {
        printf("Codec not found.");
        return -1;
    }
    if( avcodec_open2(pCodecCtx, pCodec, nullptr) < 0 )
    {
        printf("Could not open codec.");
        return -1;
    }
    //================================Write Header ===============================//
    avformat_write_header(pFormatCtx, nullptr);
    int y_size = pCodecCtx->width * pCodecCtx->height;
    AVPacket pkt;
    av_new_packet(&pkt, y_size * 3);

    //
    int got_picture = 0;
    int ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);
    if( ret < 0 )
    {
        printf("Encode Error.\n");
        return -1;
    }
    if( got_picture == 1 )
    {
        pkt.stream_index = pAVStream->index;
        ret = av_write_frame(pFormatCtx, &pkt);
    }
    av_free_packet(&pkt);
    av_write_trailer(pFormatCtx);
    if( pAVStream )
    {
        avcodec_close(pAVStream->codec);
    }
    avio_close(pFormatCtx->pb);
    avformat_free_context(pFormatCtx);
    return 0;
}

main函數如下:

int main()
{
    //std::cout << "Hello World!" << std::endl;
    AVFormatContext *pFormatCtx = avformat_alloc_context();
    int res;
    res = avformat_open_input(&pFormatCtx, "test.mp4", nullptr, nullptr);
    if (res) {
        return 0;
    }
    avformat_find_stream_info(pFormatCtx, nullptr);
    int videoStream = -1;
    for(int i=0; i<pFormatCtx->nb_streams; i++) {
        if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoStream=i;
            break;
        }
    }
    if(videoStream == -1) {
        return 0;
    }
    AVCodecContext *pCodecCtxOrig = nullptr;
    // Get a pointer to the codec context for the video stream
    pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
    AVCodec *pCodec = nullptr;
    // Find the decoder for the video stream
    pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id);
    if(pCodec == nullptr) {
        fprintf(stderr, "Unsupported codec!\n");
        return 0; // Codec not found
    }
    AVCodecContext *pCodecCtx = nullptr;
    // Copy context
    pCodecCtx = avcodec_alloc_context3(pCodec);
    if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
        fprintf(stderr, "Couldn't copy codec context");
        return 0; // Error copying codec context
    }
    // Open codec
    if(avcodec_open2(pCodecCtx, pCodec, nullptr)<0) {
        return 0;// Could not open codec
    }
    AVFrame *pFrameRGB = nullptr;
    pFrameRGB = av_frame_alloc();
    res = av_seek_frame(pFormatCtx, -1, 10 * AV_TIME_BASE, AVSEEK_FLAG_BACKWARD);//10(second)
    if (res<0) {
        return 0;
    }
    AVPacket packet;
    while(1) {
        av_read_frame(pFormatCtx, &packet);
        if(packet.stream_index == videoStream) {
            res = avcodec_send_packet(pCodecCtx, &packet);
            int gotPicture = avcodec_receive_frame(pCodecCtx, pFrameRGB); //gotPicture = 0 success, a frame was returned
            if(gotPicture == 0) {
                SwsContext* swsContext = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
                                          SWS_BICUBIC, nullptr, nullptr, nullptr);
                AVFrame* frameRGB = av_frame_alloc();
                avpicture_alloc((AVPicture*)frameRGB, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
                sws_scale(swsContext, pFrameRGB->data, pFrameRGB->linesize, 0, pCodecCtx->height, frameRGB->data, frameRGB->linesize);
                saveAsJPEG(pFrameRGB, pCodecCtx->width, pCodecCtx->height, 10);
                avformat_close_input(&pFormatCtx);
                return 0;
            }
        }
    }
    return 0;
}

如果在Qt工程裏使用,則可以很簡單的用QImage來保存數據,具體講SaveAsJPEG()這一行代碼替換爲:

QImage image(frameRGB->data[0], pCodecCtx->width, pCodecCtx->height, frameRGB->linesize[0], QImage::Format_RGB888);
image.save("10.jpg");

需要包含頭文件

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
#include "libswresample/swresample.h"
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章