ffpmeg网址:http://ffmpeg.org/

ffmpegapi文档:http://ffmpeg.org/doxygen/trunk/index.html

因为这是JPG转MP4,所以不涉及音频部分,可参考例子解码:http://ffmpeg.org/doxygen/trunk/decode_video_8c-example.html 编码:http://ffmpeg.org/doxygen/trunk/encode_video_8c-example.html

一、读取到的JPG图像内容需要解码:

1、打开一个解码器需要的内容

AVCodec* pDecodec;
AVCodecContext* pDecodecCtx;
AVFrame* pDeFrame;
AVPacket pDePacket;
//读取jpg用的解码器mjpeg
pDecodec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
//获取解码器上下文
pDecodecCtx = avcodec_alloc_context3(pDecodec);
pDecodecCtx->width = 512;
pDecodecCtx->height = 512;
pDecodecCtx->pix_fmt = AV_PIX_FMT_RGB24;
//打开解码器
avcodec_open2(pDecodecCtx,pDecodec,NULL);

2、解码操作

//用一个avstream来接收解码的数据帧
pDeFrame = av_frame_alloc();
//初始化一个ffmpeg的数据包
av_init_packet(&pDePacket);
unsigned char *jpgdata = new unsigned char[2048*2048];
FILE *file;
file = fopen([需要打开的文件全路径], "rw");
int readlen = fread(jpgdata, 1, 2048*2048, file);
//调用解码器解码
avcodec_decode_video2(pDecodecCtx,pDeFrame,&deCFflag,&pDePacket);

二、创建打开编码器

AVCodec* pEncodec;
AVCodecContext* pEncodecCtx;
AVFrame* pEnFrame;
AVPacket pEnPacket; pEncodecCtx = pVideoStream->codec;
pEncodecCtx->codec_id = pOutPutFormatCtx->oformat->video_codec;
pEncodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pEncodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
pEncodecCtx->width = 720;
pEncodecCtx->height = 576;
//pEncodecCtx->bit_rate = 400000; 码流 适用于网络传输
pEncodecCtx->gop_size = 1;
pEncodecCtx->time_base.num = 1;
pEncodecCtx->time_base.den = 25;
pEncodecCtx->qmin = 10;
pEncodecCtx->qmax = 51;
pEncodecCtx->max_b_frames=3;
//质量 文件
pEncodecCtx->bit_rate = 0;
av_opt_set_int(pEncodecCtx->priv_data,"crf",0,0);
//在文件头写入文件信息(默认在每个包写入文件信息,微软默认播放器可能播放不了)
pEncodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
//找一个视频编码器
pEncodec = avcodec_find_encoder(pEncodecCtx->codec_id);
//打开编码器
avcodec_open2(pEncodecCtx,pEncodec,NULL); int picture_size =0;
uint8_t* picture_buf;
//初始换编码用avframe
pEnFrame = av_frame_alloc();
//编码的图像大小
picture_size = avpicture_get_size(pEncodecCtx->pix_fmt,pEncodecCtx->width,pEncodecCtx->height);
//编码图像buf
picture_buf = (uint8_t *)av_malloc(picture_size);
//关联相关属性
avpicture_fill((AVPicture *)pEnFrame,picture_buf, pEncodecCtx->pix_fmt,pEncodecCtx->width,pEncodecCtx->height);
pEnFrame->width = pEncodecCtx->width;
pEnFrame->height = pEncodecCtx->height;
pEnFrame->format = pEncodecCtx->pix_fmt;
//初始话编码的包
av_init_packet(&pEnPacket);

三、输出文件的信息

AVFormatContext* pOutPutFormatCtx;
AVStream* pVideoStream; avformat_alloc_output_context2(&pOutPutFormatCtx, NULL, NULL, [输出的文件全路径].c_str());
pVideoStream = avformat_new_stream(pOutPutFormatCtx, NULL);
avio_open(&pOutPutFormatCtx->pb,[输出的文件全路径],AVIO_FLAG_READ_WRITE);
av_dump_format(pOutPutFormatCtx,0,[输出的文件全路径],1);
//将编码器的信息与输出的信息属性关联起来
avcodec_parameters_from_context(pVideoStream->codecpar,pEncodecCtx);
av_codec_get_tag2(pOutPutFormatCtx->oformat->codec_tag, pEncodecCtx->codec_id, &pVideoStream->codecpar->codec_tag);
//写输出的文件的头
avformat_write_header(pOutPutFormatCtx,NULL);

四、转码,将用解码器打开的jpg文件转码成需要编码的Frame格式

//FFmpeg的一个转换类
SwsContext* pImagectx; pImagectx = sws_getContext(pDecodecCtx->width, pDecodecCtx->height, pDecodecCtx->pix_fmt,pEncodecCtx->width,pEncodecCtx->height, pEncodecCtx->pix_fmt,SWS_BICUBIC, NULL,NULL, NULL); sws_scale(pImagectx,pDeFrame->data,pDeFrame->linesize,0,pDeFrame->height,pEnFrame->data,pEnFrame->linesize);

五、编码写文件

注释信息为测试读取文件的逻辑
//snprintf(imagename, 255, "%d.jpg", i);//X.jpg
//string imagefilePath = dir + imagename;
//printf("---");
//printf(imagefilePath.c_str());
//printf("\n");
//FILE *file;
//file = fopen(imagefilePath.c_str(), "rw");
//int readlen = fread(jpgdata, 1, 2048*2048, file);
//printf("----read file success %d \n",readlen);
//fclose(file);
//pDePacket.data = jpgdata;
//pDePacket.size = readlen;
//int decodecresult = avcodec_decode_video2(pDecodecCtx,pDeFrame,&deCFflag,&pDePacket);
//printf("----decodecresult %d \n",decodecresult);
//sws_scale(pImagectx,pDeFrame->data,pDeFrame->linesize,0,pDeFrame->height,pEnFrame->data,pEnFrame->linesize);
int gotpicture = 0;
pEnFrame->pts = i;
//编码
avcodec_encode_video2(pEncodecCtx,&pEnPacket,pEnFrame,&gotpicture);
//printf("----encodecresult %d \n",gotpicture);
pEnPacket.pts = av_rescale_q(pEnPacket.pts, rational, pVideoStream->time_base);
pEnPacket.stream_index = pVideoStream->index;
//将编码得到的包写入输出文件
av_write_frame(pOutPutFormatCtx,&pEnPacket);
//写文件尾
av_write_trailer(pOutPutFormatCtx);

六、释放资源

av_free_packet(&pDePacket);
if(pDeFrame != NULL)
{
av_free(pDeFrame);
}
if(pDecodecCtx != NULL)
{
avcodec_close(pDecodecCtx);
}
av_free_packet(&pEnPacket);
if(pEnFrame != NULL)
{
av_free(pEnFrame);
}
if(pEncodecCtx != NULL)
{
avcodec_close(pEncodecCtx);
} if(pOutPutFormatCtx!= NULL)
{
avformat_free_context(pOutPutFormatCtx);
}
if(pVideoStream != NULL)
{
av_free(pVideoStream);
}
if(pImagectx != NULL)
{
sws_freeContext(pImagectx);
}

编译ffmpeg(第一次),实现JPG转MP4的更多相关文章

  1. 第一次编译ffmpeg

    今天开始玩ffmpeg了. 从官网下载来的压缩包,不会编译诶,于是我开始研究起来了. 下面就是实时记录的随笔: 首先是从官网下载来的ffmpeg,就是下面这个版本,目前的最新版吧. http://ff ...

  2. 开源项目:windows下使用MinGW+msys编译ffmpeg

    本文参考了网络上的不少文章,但由于版本环境的问题参考文章并不能直接指导编译,本文吸收多方经验,并在自己多次编译实验的基础上写成,欢迎转载,请注名出处.    FFmpeg是在Linux平台下开发的,但 ...

  3. anroid ndk编译ffmpeg 引用librtmp libx264

    Ffmpeg 无处不在,自然android系统少不了它,折腾了不少时间完成 ndk编译ffmpeg,生成so库中引用了外部库librtmp,libx264.条条大路通罗马, 也许还有别的更好的方法去完 ...

  4. windows 下使用 MinGW + msys 编译 ffmpeg

    本文参考了网络上的不少文章,但由于版本环境的问题参考文章并不能直接指导编译,本文吸收多方经验,并在自己多次编译实验的基础上写成,欢迎转载,请注名出处.    FFmpeg是在Linux平台下开发的,但 ...

  5. Windows环境下使用cygwin ndk_r9c编译FFmpeg

     一.废话 最近学习,第一步就是编译.我们需要编译FFmpag,x264,fdk_aac,一步步来.先来讲一下FFmpeg,网上说的很多都是几百年前的,我亲测完美可用 联系我可以直接评论,也可以加我Q ...

  6. MinGW下简单编译FFmpeg

    2009.03.21补充:ffmpeg-0.5正式发布,地址为:[url]http://www.ffmpeg.org/releases/ffmpeg-0.5.tar.bz2[/url].修改了第7步, ...

  7. Mac系统编译FFmpeg

    转载请标明来源:我不是掌柜的博客 前言 维基百科解释:FFmpeg是一个开源软件,可以运行音频和视频多种格式的录影.转换.流功能,包含了libavcodec – 这是一个用于多个项目中音频和视频的解码 ...

  8. 一步步实现windows版ijkplayer系列文章之一——Windows10平台编译ffmpeg 4.0.2,生成ffplay

    一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...

  9. Linux下编译ffmpeg并用GDB调试

    1.在Ubuntu界面上调处命令行界面,最方便的方式是使用快捷键Ctrl+Alt+T. 2.安装SDL SDL是一个开源的多媒体开发库,可以设置图像和视频的绘制等操作.如果不安装SDL,FFMPEG将 ...

  10. ubuntu下编译ffmpeg+SDL+ffplay提取motion vector

    编译ffmpeg: 第一步: 从官网http://ffmpeg.org/下载最新版本. 解压tar -xjvf ffmpeg-3.3.1.tar.bz2 进入目录cd ffmpeg-3.3.1 第二步 ...

随机推荐

  1. 日志采集工具Flume的安装与使用方法

    安装Flume,参考厦门大学林子雨教程:http://dblab.xmu.edu.cn/blog/1102/ 并完成案例1 1.案例1:Avro source Avro可以发送一个给定的文件给Flum ...

  2. js循环修改对象内层元素的值

    问题:存在一个对象,该对象的内部元素也为对象,子对象的元素也为对象,...(即多层对象构成的对象,具体如下),那么应该如何修改最内层元素的值(如 obj.a.a.a = 5)? var obj = { ...

  3. 多项式求值问题(horner规则)——Python实现

    # 多项式求值(Horner规则) # 输入:A[a0,a1,a2...an],x的值 # 输出:给定的x下多项式的值p   # Horner迭代形式实现 1 # 在此修改初值 2 A = [2, 6 ...

  4. Vue项目发布的问题--http://localhost:8088/static/fonts/fontawesome-webfont.af7ae50.woff2

    问题:ngnix将8080转成80对外访问,找不对woff2等文件 一\ 搭建环境 ngnix-->conf中 server { listen 80; server_name 10.9.240. ...

  5. APICloud的真机wifi连接问题

    APICloud的真机wifi连接问题 在APICloud的真机wifi连接时需要注意事项与解决问题. 1.首先将项目拉取到本地,用APICloud Studio 2打开(也可以用webStorm配置 ...

  6. @ControllerAdvice全局异常处理不起作用原因及解决办法

    这段时间使用springboot搭建基础框架,作为springboot新手,各种问题都有. 当把前端框架搭建进来时,针对所有controller层的请求,所发生的异常,需要有一个统一的异常处理,然后返 ...

  7. 大数据学习(18)—— Flume介绍

    老规矩,学习新东西先上官网瞅瞅Apache Flume Flume是什么 Flume是一个分布式.可靠的大规模高效日志收集.汇聚和传输的这么一个服务.它的架构基于流式数据,配置简单灵活.它具备可调节的 ...

  8. 一文带你认识LPWA通信技术

    摘要:为了满足越来越多的远距离物联网设备的连接需求,LPWA应用而生. 本文分享自华为云社区<常见物联网通信技术之LPWA通信技术>,作者:爱吃面包的猫. 如果你比较关注物联网圈的话,想必 ...

  9. CentOS时间日期类语法

    目录 一.date时间日期类 1. date显示当前时间 2. date 显示非当前时间 3. date 设置系统时间 二.cal 查看日历 一.date时间日期类 date [OPTION]... ...

  10. Linux上搭建zookeeper服务注册中心

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...