程序转载其他博主的,自己稍微加了一些修改,视频源可以使用多种格式,目前测试的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();// 分配AVFormatContextpFormatCtx = 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

使用FFmpeg把视频转换成JPG格式的图片相关推荐

  1. 怎么把video文件改成mp4_如何把视频转换成mp4格式?

    要想把视频转换成mp4格式,这就看你原视频是什么格式了.这里推荐一个视频格式转换类型比较齐全的一款. 烁光视频转换器烁光视频转换器​www.ivideotools.com 专业视频格式转换器,支持视频 ...

  2. b站电脑客户端_如何将B站的flv格式的视频转换成mp4格式

    经常看到B站有精彩的视频片段,于是想把这些视频下载保存到电脑,但是发现没有下载按钮,是不是很悲催.有些时候想从优酷.土豆网这些视频网站下载视频,结果却提示要先下载视频客户端才能继续下载视频,运气差的话 ...

  3. 怎么把video文件改成mp4_GiliSoft Video Converter将MP4视频转换成M4V格式教程

    GiliSoft Video Converter是一款好用的视频格式转换软件.使用GiliSoft Video Converter可以轻松对电脑中的视频进行格式转换: 进入下载GiliSoft Vid ...

  4. 电脑视频转换成mp4格式,视频格式转换器转换

    怎么把电脑视频转换成mp4格式?使用视频转换器,可以转换来自各种设备的音视频格式,包括相机.手机.视频播放器.电视.平板电脑等.因此,音视频爱好者都可以使用它在各种设备上播放或在社交平台上分享. 主要 ...

  5. 怎样将wmv格式的视频转换成mp4格式

    WMV视频格式的全称为:Windows Media Video,是微软开发的一系列视频编解码和其相关的视频编码格式的统称,是微软Windows媒体框架的一部分.也是一种有着高压缩率.体积小等优势的视频 ...

  6. flv视频转换成mp4格式怎么转?

    flv视频转换成mp4格式怎么转?相信正在阅读文章的你,一定接触到了flv视频文件.flv是一种新型的小体积视频格式,因为体积小的特点使得flv的加载速度特别快,从而实现了在线观看浏览视频也很顺畅的愿 ...

  7. 怎么把视频转换成mp3格式?

    怎么把视频转换成mp3格式?平时我们在观看视频的时候,总会遇到自己比较中意的曲子,特别是很奇怪,平台播放的歌曲总感觉没有视频里的好听,这个时候我就特别想把视频直接转成音频保存起来,在自己跑步或者坐车的 ...

  8. 利用ffmpeg把一帧原始视频数据转换成jpg格式的图片

    利用ffmpeg对一帧原始的视频数据转换成jpg格式的图片,保存到本地,用于Android显示 #include <jni.h> #include <stdio.h> #inc ...

  9. 手机视频转换成gif格式的方法,手机视频转gif工具推荐

    许多人看到一些有趣的小视频,都想要将视频做成GIF表情包来使用的时候,却不知道应该如何操作时,该怎么办呢?其实我们只需要借助一些工具,就可以完美的完成,我推荐的这款视频转gif工具,在线网站操作,不需 ...

最新文章

  1. 厦门再次加大引才力度!博士补贴8万,硕士5万,本科3万!
  2. 亚马逊首家“无人超市”系统存在bug?!开业当天,记者中途换装成功骗过摄像头...
  3. mysql表创建在哪_mysql创建表命令是哪句
  4. UITextFile
  5. Java二十三设计模式之-----原型模式
  6. java使用迭代器删除元素_使用Java从地图中删除元素
  7. springboot 定时器
  8. 最小环(【CCF】NOI Online能力测试 提高组第三题)
  9. python 中__init__ 与 __call__ 的区别
  10. 设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,
  11. SQL 基础面试题(四)
  12. 每个程序员1小时内必须解决的5个编程问题(转)
  13. Java基础:继承的综合案例 —— 群主发普通红包
  14. 世界杯直播背后的黑科技 腾讯云极速高清技术驱动体育直播发展
  15. CMOS模拟集成电路设计 吴金 学习记录1
  16. html摄氏度转换华氏度,摄氏和华氏转换器-JavaScript
  17. 关于javaweb中的流媒体
  18. 安徽省对口计算机试题答案,安徽省对口高考试卷
  19. iPhone怎么设置自定义铃声?苹果可以设置自定义铃声吗?
  20. linux sqlplus 查询数据,Linux安装sqlplus及shell查询数据库

热门文章

  1. caffe(ubuntu14.04)学习笔记1——运行MNIST数据集模型
  2. dnf鹰犬boss机器人_dnf鹰犬boss怎么打
  3. 华为交换机不同网段互访_FAQ-傻瓜交换机是否可以连接不同的网段的终端实现互访...
  4. 人类动作识别数据集AVA
  5. unity3d 动态合批设置_Unity动态合批(Dynamic Batching)与静态合批(Static Batching)
  6. qqhelp.net 删除 查杀 清除QQ病毒qqhelp变种(6440'qqhelp'net/#sqq5)的方法
  7. PythonQt——yolov5手势识别隔空操纵车载音乐播放器
  8. 白硕:背靠背知识协同——区块链与人工智能结合的新途径
  9. 牛视源码定制,抖音矩阵系统。come here
  10. [30期] 个人职业规划