最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)(转)
原文转自 https://blog.csdn.net/leixiaohua1020/article/details/25346147/
伴随着毕业论文的完成,这两天终于腾出了空闲,又有时间搞搞FFMPEG的研究了。想着之前一直搞的都是FFMPEG解码方面的工作,很少涉及到FFMPEG编码方面的东西,于是打算研究一下FFMPEG的编码。在网上看了一些例子,发现要不然是难度略微有些大,要不然就是类库比较陈旧,于是就决定自己做一个编码方面的例子,方便以后学习。
简介
本文的编码器实现了YUV420P的数据编码为JPEG图片。本着简单的原则,代码基本上精简到了极限。使用了2014年5月6号编译的最新的FFMPEG类库。
程序很简单,打开工程后直接运行即可将YUV数据编码为JPEG。本程序十分灵活,可以根据需要修改成编码各种图像格式的编码器,比如PNG,GIF等等。平台使用VC2010。
源代码
/**
* 最简单的基于FFmpeg的图像编码器
* Simplest FFmpeg Picture Encoder
*
* 雷霄骅 Lei Xiaohua
* leixiaohua1020@126.com
* 中国传媒大学/数字电视技术
* Communication University of China / Digital TV Technology
* http://blog.csdn.net/leixiaohua1020
*
* 本程序实现了YUV420P像素数据编码为JPEG图片。是最简单的FFmpeg编码方面的教程。
* 通过学习本例子可以了解FFmpeg的编码流程。
*/ #include <stdio.h> #define __STDC_CONSTANT_MACROS #ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif int main(int argc, char* argv[])
{
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=;
int size; int ret=; FILE *in_file = NULL; //YUV source
int in_w=,in_h=; //YUV's width and height
const char* out_file = "cuc_view_encode.jpg"; //Output file in_file = fopen("cuc_view_480x272.yuv", "rb"); av_register_all(); //Method 1
pFormatCtx = avformat_alloc_context();
//Guess format
fmt = av_guess_format("mjpeg", NULL, NULL);
pFormatCtx->oformat = fmt;
//Output URL
if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < ){
printf("Couldn't open output file.");
return -;
} //Method 2. More simple
//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
//fmt = pFormatCtx->oformat; video_st = avformat_new_stream(pFormatCtx, );
if (video_st==NULL){
return -;
}
pCodecCtx = video_st->codec;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P; pCodecCtx->width = in_w;
pCodecCtx->height = in_h; pCodecCtx->time_base.num = ;
pCodecCtx->time_base.den = ;
//Output some information
av_dump_format(pFormatCtx, , out_file, ); pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec){
printf("Codec not found.");
return -;
}
if (avcodec_open2(pCodecCtx, pCodec,NULL) < ){
printf("Could not open codec.");
return -;
}
picture = av_frame_alloc();
size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
picture_buf = (uint8_t *)av_malloc(size);
if (!picture_buf)
{
return -;
}
avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); //Write Header
avformat_write_header(pFormatCtx,NULL); y_size = pCodecCtx->width * pCodecCtx->height;
av_new_packet(&pkt,y_size*);
//Read YUV
if (fread(picture_buf, , y_size*/, in_file) <=)
{
printf("Could not read input file.");
return -;
}
picture->data[] = picture_buf; // Y
picture->data[] = picture_buf+ y_size; // U
picture->data[] = picture_buf+ y_size*/; // V //Encode
ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
if(ret < ){
printf("Encode Error.\n");
return -;
}
if (got_picture==){
pkt.stream_index = video_st->index;
ret = av_write_frame(pFormatCtx, &pkt);
} av_free_packet(&pkt);
//Write Trailer
av_write_trailer(pFormatCtx); printf("Encode Successful.\n"); if (video_st){
avcodec_close(video_st->codec);
av_free(picture);
av_free(picture_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx); fclose(in_file); return ;
}
结果
编码前的YUV420P数据:
编码后的JPEG:
下载
simplest ffmpeg picture encoder
项目主页
SourceForge:https://sourceforge.net/projects/simplestffmpegpictureencoder/
Github:https://github.com/leixiaohua1020/simplest_ffmpeg_picture_encoder
开源中国:http://git.oschina.net/leixiaohua1020/simplest_ffmpeg_picture_encoder
CSDN工程下载地址:
http://download.csdn.net/detail/leixiaohua1020/7319265
PUDN工程下载地址:
http://www.pudn.com/downloads644/sourcecode/multimedia/detail2605252.html
本程序实现了YUV420P像素数据编码为JPEG图片。是最简单的FFmpeg编码方面的教程。通过学习本例子可以了解FFmpeg的编码流程。
更新-1.1(2015.2.13)=========================================
这次考虑到了跨平台的要求,调整了源代码。经过这次调整之后,源代码可以在以下平台编译通过:
VC++:打开sln文件即可编译,无需配置。
cl.exe:打开compile_cl.bat即可命令行下使用cl.exe进行编译,注意可能需要按照VC的安装路径调整脚本里面的参数。编译命令如下。
::VS2010 Environment
call "D:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
::include
@set INCLUDE=include;%INCLUDE%
::lib
@set LIB=lib;%LIB%
::compile and link
cl simplest_ffmpeg_picture_encoder.cpp /link avcodec.lib avformat.lib avutil.lib ^
avdevice.lib avfilter.lib postproc.lib swresample.lib swscale.lib /OPT:NOREF
MinGW:MinGW命令行下运行compile_mingw.sh即可使用MinGW的g++进行编译。编译命令如下。
g++ simplest_ffmpeg_picture_encoder.cpp -g -o simplest_ffmpeg_picture_encoder.exe \
-I /usr/local/include -L /usr/local/lib \
-lavformat -lavcodec -lavutil
GCC:Linux或者MacOS命令行下运行compile_gcc.sh即可使用GCC进行编译。编译命令如下。
gcc simplest_ffmpeg_picture_encoder.cpp -g -o simplest_ffmpeg_picture_encoder.out \
-I /usr/local/include -L /usr/local/lib -lavformat -lavcodec -lavutil
PS:相关的编译命令已经保存到了工程文件夹中
CSDN下载地址:http://download.csdn.net/detail/leixiaohua1020/8444893
SourceForge上已经更新。
最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)(转)的更多相关文章
- 最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)
伴随着毕业论文的完成,这两天终于腾出了空闲,又有时间搞搞FFMPEG的研究了.想着之前一直搞的都是FFMPEG解码方面的工作,很少涉及到FFMPEG编码方面的东西,于是打算研究一下FFMPEG的编码. ...
- 最简单的基于FFMPEG的视频编码器(YUV编码为H.264)
本文介绍一个最简单的基于FFMPEG的视频编码器.该编码器实现了YUV420P的像素数据编码为H.264的压缩编码数据.编码器代码十分简单,可是每一行代码都非常重要,适合好好研究一下.弄清楚了本代码也 ...
- 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
http://blog.csdn.net/leixiaohua1020/article/details/25430449 本文介绍一个最简单的基于FFMPEG的音频编码器.该编码器实现了PCM音频采样 ...
- 最简单的基于FFmpeg的编码器-纯净版(不包含libavformat)
===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...
- 最简单的基于FFMPEG的转码程序
本文介绍一个简单的基于FFmpeg的转码器.它可以将一种视频格式(包括封转格式和编码格式)转换为另一种视频格式.转码器在视音频编解码处理的程序中,属于一个比较复杂的东西.因为它结合了视频的解码和编码. ...
- 最简单的基于FFmpeg的视频编码器-更新版(YUV编码为HEVC(H.265))
===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...
- 最简单的基于FFmpeg的内存读写的例子:内存转码器
===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...
- 最简单的基于FFmpeg的内存读写的例子:内存播放器
===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...
- (转)最简单的基于FFmpeg的内存读写的例子:内存播放器
ffmpeg内存播放解码 目录(?)[+] ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章 ...
随机推荐
- 关闭或者开启apache的目录浏览
为了安全或者方便需要关闭或者开启apache的目录浏览 关闭目录浏览 修改http.conf 文件 Options Indexes FollowSymLinks 改为 ...
- Qt快速入门学习笔记(画图篇)
1.Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕和绘图设备上进行绘制,它主要基于QPainter.QPaintDevice和QPaintEngine这三个类.其中QPainter用来执行 ...
- 【题解】Atcoder ARC#94 F-Normalization
再次膜拜此强题!神级性质之不可能发现系列收藏++:首先,对于长度<=3的情况,我们采取爆搜答案(代码当中是打表).对于长度>=4的情况,则有如下几条玄妙的性质: 首先我们将 a, b, c ...
- BZOJ4311:向量——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4311 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 ...
- BZOJ3998:[TJOI2015]弦论——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3998 https://www.luogu.org/problemnew/show/P3975 对于 ...
- 【线段树】【P3740】 [HAOI2014]贴海报
传送门 Description Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委员会为选民准备了一个张贴海报的electoral墙. 张贴规 ...
- The Usage of Pymongo
Install pymongo document install pymongo from the tar package download from website python setup.y i ...
- mysql的时间函数整理
转:这里总结的非常齐全: http://fengbin2005.iteye.com/blog/1999763 Mysql时间函数 对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描 ...
- c++ string写时复制
string写时复制:将字符串str1赋值给str2后,除非str1的内容已经被改变,否则str2和str1共享内存.当str1被修改之后,stl才为str2开辟内存空间,并初始化. #include ...
- bzoj 1132 [POI2008]Tro 几何
[POI2008]Tro Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1796 Solved: 604[Submit][Status][Discu ...