使用FFmpeg把视频转换成JPG格式的图片

程序转载其他博主的,自己稍微加了一些修改,视频源可以使用多种格式,目前测试的MP4,H264视频都可以

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endif

#define MAX_PATH 128
#include "stdio.h"
/**
 * 将AVFrame(YUV420格式)保存为JPEG格式的图片
 *
 * @param width YUV420的宽
 * @param height YUV42的高
 *
 */
int MyWriteJPEG(AVFrame* pFrame, int width, int height, int iIndex)
{
// 输出文件路径
char out_file[MAX_PATH] = {0};


//   sprintf_s(out_file, sizeof(out_file), "./%d.jpg",  iIndex);

sprintf(out_file,"./tupian/%d.jpg",iIndex);

// 分配AVFormatContext对象
AVFormatContext* pFormatCtx = avformat_alloc_context();

// 设置输出文件格式
pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);

// 创建并初始化一个和该url相关的AVIOContext
if( avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
{
printf("Couldn't open output file.");
return -1;
}


// 构建一个新stream
AVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);
if( pAVStream == NULL )
{
return -1;
}


// 设置该stream的信息
AVCodecContext* pCodecCtx = pAVStream->codec;

pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = PIX_FMT_YUVJ420P;
pCodecCtx->width = width;
pCodecCtx->height = height;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;

// Begin Output some information
av_dump_format(pFormatCtx, 0, out_file, 1);
// End Output some information


// 查找解码器
AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if( !pCodec )
{
printf("Codec not found.");
return -1;
}
// 设置pCodecCtx的解码器为pCodec
if( avcodec_open2(pCodecCtx, pCodec, NULL) < 0 ) 
{
printf("Could not open codec.");
return -1;
}


//Write Header
avformat_write_header(pFormatCtx, NULL);


int y_size = pCodecCtx->width * pCodecCtx->height;


//Encode
// 给AVPacket分配足够大的空间
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;
}
printf("got_picture %d \n",got_picture);
if( got_picture == 1 )
{
//pkt.stream_index = pAVStream->index;
ret = av_write_frame(pFormatCtx, &pkt);
}


av_free_packet(&pkt);


//Write Trailer
av_write_trailer(pFormatCtx);


printf("Encode Successful.\n");


if( pAVStream )
{
avcodec_close(pAVStream->codec);
}

avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);


return 0;
}


//int Work_Save2JPG()


int main(int argc,char* argv[])
{
    int videoStream = -1;
    AVCodecContext *pCodecCtx;
    AVFormatContext *pFormatCtx;
    AVCodec *pCodec;
    AVFrame *pFrame, *pFrameRGB;
    struct SwsContext *pSwsCtx;
    const char *filename = "2518.mp4";
    AVPacket packet;
    int frameFinished;
    int PictureSize;
    uint8_t *outBuff;


    //注册编解码器
    av_register_all();

    // 分配AVFormatContext
    pFormatCtx = avformat_alloc_context();


    //打开视频文件
if( avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0 )
{
printf ("av open input file failed!\n");
exit (1);
}


//获取流信息
if( avformat_find_stream_info(pFormatCtx, NULL) < 0 )
{
printf ("av find stream info failed!\n");
exit (1);
}

printf(" pFormatCtx->nb_streams %d \n", pFormatCtx->nb_streams);


    //获取视频流
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 )
{
printf ("find video stream failed!\n");
exit (1);
}


    // 寻找解码器
pCodecCtx = pFormatCtx->streams[videoStream]->codec;

pCodec = avcodec_find_decoder(pCodecCtx->codec_id);

if( pCodec == NULL ) 
{
printf ("avcode find decoder failed!\n");
exit (1);
}


    //打开解码器
if( avcodec_open2(pCodecCtx, pCodec, NULL) < 0 ) 
{
printf ("avcode open failed!\n");
exit (1);
}


    //为每帧图像分配内存
pFrame = avcodec_alloc_frame();


int i = 0;

while( av_read_frame(pFormatCtx, &packet) >= 0 )
{
if( packet.stream_index == videoStream ) 
{
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);


if( frameFinished ) 
{
MyWriteJPEG(pFrame, pCodecCtx->width, pCodecCtx->height, i ++);
}
}
else 
{
int a=2;
int b=a;
}


av_free_packet(&packet);
}


sws_freeContext(pSwsCtx);


av_free(pFrame);
av_free(pFrameRGB);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);


    return 0;
}



编译时使用的命令:

 g++ tupian.cpp -lavcodec  -lavformat -lavutil -lswscale




展开阅读全文

没有更多推荐了,返回首页