一,ubuntu源码编译安装
1,如何安装
参考:感谢原作者
基本操作就是:config,make,make install
需要注意的是,如何证明自己安装成功了呢?大约是由于版本升级的原因,并不能按照原作者的方式去证实
2,安装完成以后让pkg-config帮我们管理各种头文件和lib
配置方法:https://www.cnblogs.com/0-lingdu/p/10655176.html第15条
如何证明配置好了呢?
pkg-config --cflags libavformat
#能看到返回值啦
3,那么so库怎么办呢?
添加到环境变量:
sudo gedit /etc/ld.so.conf打开文本后,加入如下路径:/usr/local/ffmpeg/lib/
最后面的斜杠一定要带上,别忘了!
4,配置完了以后,如何证明自己ffmpeg安装成功了呢?
如下代码是抄的(无法添加到代码专用框里,将就一下,注意自己找个mp4文件,路径要写对):
include
#include
extern "C"
{
#include #include #include }
#pragma comment(lib, "avformat.a")
#pragma comment(lib, "avutil.a")
#pragma comment(lib, "avcodec.a")
using namespace std;
int main()
{
av_register_all(); //ffmpeg程序的第一句,注册库
AVFormatContext afc = NULL;
//打开视频文件
int nRet = avformat_open_input(&afc, "test1.mp4", 0, 0);
if (nRet < 0)
{
cout << "找不到视频文件" << endl;
}
else
{
cout << "视频打开成功" << endl;
}
int durTime = afc->duration / AV_TIME_BASE; //视频时间 4分20秒
unsigned int numberOfStream = afc->nb_streams; //包含流的个数2:一个视频流一个音频流
for (int i = 0; i < afc->nb_streams; i++)
{
AVCodecContext acc = afc->streams[i]->codec;
if (acc->codec_type == AVMEDIA_TYPE_VIDEO) //如果是视频类型
{
AVCodec *codec = avcodec_find_decoder(acc->codec_id);
if (!codec)
{
cout << "没有该类型解码器" << endl;
}
int ret = avcodec_open2(acc, codec, NULL);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf));
}
cout << "解码器打开成功" << endl;
}
}
if (afc)
{
avformat_close_input(&afc); //关闭视频流
}
//system("pause");
pause();
return 0;
}
#include
extern "C"
{
#include
#pragma comment(lib, "avformat.a")
#pragma comment(lib, "avutil.a")
#pragma comment(lib, "avcodec.a")
using namespace std;
int main()
{
av_register_all(); //ffmpeg程序的第一句,注册库
AVFormatContext afc = NULL;
//打开视频文件
int nRet = avformat_open_input(&afc, "test1.mp4", 0, 0);
if (nRet < 0)
{
cout << "找不到视频文件" << endl;
}
else
{
cout << "视频打开成功" << endl;
}
int durTime = afc->duration / AV_TIME_BASE; //视频时间 4分20秒
unsigned int numberOfStream = afc->nb_streams; //包含流的个数2:一个视频流一个音频流
for (int i = 0; i < afc->nb_streams; i++)
{
AVCodecContext acc = afc->streams[i]->codec;
if (acc->codec_type == AVMEDIA_TYPE_VIDEO) //如果是视频类型
{
AVCodec *codec = avcodec_find_decoder(acc->codec_id);
if (!codec)
{
cout << "没有该类型解码器" << endl;
}
int ret = avcodec_open2(acc, codec, NULL);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf));
}
cout << "解码器打开成功" << endl;
}
}
if (afc)
{
avformat_close_input(&afc); //关闭视频流
}
//system("pause");
pause();
return 0;
}
如何编译:
g++ main.cpp -o main.out `pkg-config --cflags --libs libavcodec libavdevice libavfilter libavformat libavutil libswresample libswscale `
执行结果返回:
视频打开成功,解码器打开成功,就可以了.
临时添加环境变量:
echo $PATH
#看看环境变量里都有啥
PATH="$PATH:/usr/local/ffmpeg/bin"
#往环境变量里加点东西
永久添加环境变量:
打开~/.bashrc文件最末添加命令:
PATH=$PATH:/usr/local/your_path
二,win10+vs2015配置ffmpeg开发环境
首先声明一点哈,windows系统上配置ffmpeg开发环境和源码编译安装ffmpeg不一样。一开始本人各种查找win10源码编译ffmpeg各种教程各种复杂,后来才知道,官网提供现成的头文件和库文件,直接下下来用就可以了。囧。。
1,下载
本人下载的时候是这样的
算了,无法上传图片,本人下的是4.2.2 win64选项下的 Shared和Dev版本(后期两个版本都用得到)
本地新建D:/ffmpeg文件夹,将下载的zip包放进去,解压就会出现两个文件夹:ffmpeg-4.2.2-win64-shared(内含bin)和ffmpeg-4.2.2-win64-dev(内含include和lib)
2,配置开发环境
打开vs2015,新建空项目,个人喜欢使用“release”和x64作为平台解决方案
1),添加ffmpeg的头文件:项目--属性--c++--附加包含目录 ,新增一项“D:\ffmpeg\ffmpeg-4.2.2-win64-dev\include”。
2),添加lib:项目--属性--链接器--常规--附加库目录,新增一项“D:\ffmpeg\ffmpeg-4.2.2-win64-dev\lib”。如果是其他项目,还需要在项目--属性--链接器--输入--附加依赖项 中输入具体的lib名称(比如opencv),但是,我们的ffmpeg这样做好像并不管用哦,那咋整?
建议如下操作,具体原因不详,但是能跑。
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
//在代码中明确地关联lib也是可以的
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/pixfmt.h>
}
//顺带一提,ffmpeg的头文件需要按照c语言的规则引进来
3),关联dll动态库:上述一通操作以后,编译应该是问题不大,可以生成exe文件,但是我们还是无法运行,提示缺少好几个动态库,那么我们就只能,把D:\ffmpeg\ffmpeg-4.2.2-win64-shared\bin里面所有的dll文件复制到运行环境下(和生成的exe同一个目录下),我非常纳闷ffmpeg为什么不把include,lib和bin放到同一个zip包中供大家下载,我也不确定把shared版本的dll粗暴地复制到dev版本的代码环境中是不是一点问题都没有,只不过在我自己的例子中,能跑。嗯嗯,估计是问题不大。不过话说回来,ffmpeg是我非常喜欢的一个包,我能感觉到它的开发者肯定是非常爱干净爱整洁的人,也很细致贴心,,比心,,,不管是ubuntu还是windows上,编译起来都没有什么糟心的问题,点赞。。
3,编译运行的代码实例
此处感谢原作者
但是原作者的代码我跑不起来,修改了一处代码:
if (avformat_open_input(&pFormatCtx, input, NULL, 0) != 0)
下面贴的是本人能跑起来的版本:
//main.cpp
//#include <stdio.h>
//#include <stdlib.h>
#include <iostream>
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
extern "C"
{
//编码
#include "libavcodec/avcodec.h"
//封装格式处理
#include "libavformat/avformat.h"
//像素处理
#include "libswscale/swscale.h"
};
int main(int argc, char* argv[])
{
//获取输入输出文件名
const char *input = "1.mp4";
const char *output = "test.yuv";
//1.注册所有组件
av_register_all();
//封装格式上下文,统领全局的结构体,保存了视频文件封装格式的相关信息
AVFormatContext *pFormatCtx = avformat_alloc_context();
//2.打开输入视频文件
if (avformat_open_input(&pFormatCtx, input, NULL, 0) != 0)
{
printf("%s", "无法打开输入视频文件");
return -1;
}
//3.获取视频文件信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
printf("%s", "无法获取视频文件信息");
return -1;
}
//获取视频流的索引位置
//遍历所有类型的流(音频流、视频流、字幕流),找到视频流
int v_stream_idx = -1;
int i = 0;
//number of streams
for (; i < pFormatCtx->nb_streams; i++)
{
//流的类型
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
v_stream_idx = i;
break;
}
}
if (v_stream_idx == -1)
{
printf("%s", "找不到视频流\n");
return -1;
}
//只有知道视频的编码方式,才能够根据编码方式去找到解码器
//获取视频流中的编解码上下文
AVCodecContext *pCodecCtx = pFormatCtx->streams[v_stream_idx]->codec;
//4.根据编解码上下文中的编码id查找对应的解码
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
{
printf("%s", "找不到解码器\n");
return -1;
}
//5.打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("%s", "解码器无法打开\n");
return -1;
}
//输出视频信息
printf("视频的文件格式:%s", pFormatCtx->iformat->name);
printf("视频时长:%d", (pFormatCtx->duration) / 1000000);
printf("视频的宽高:%d,%d", pCodecCtx->width, pCodecCtx->height);
printf("解码器的名称:%s", pCodec->name);
//准备读取
//AVPacket用于存储一帧一帧的压缩数据(H264)
//缓冲区,开辟空间
AVPacket *packet = (AVPacket*)av_malloc(sizeof(AVPacket));
//AVFrame用于存储解码后的像素数据(YUV)
//内存分配
AVFrame *pFrame = av_frame_alloc();
//YUV420
AVFrame *pFrameYUV = av_frame_alloc();
//只有指定了AVFrame的像素格式、画面大小才能真正分配内存
//缓冲区分配内存
uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
//初始化缓冲区
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
//用于转码(缩放)的参数,转之前的宽高,转之后的宽高,格式等
struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P,
SWS_BICUBIC, NULL, NULL, NULL);
int got_picture, ret;
FILE *fp_yuv = fopen(output, "wb+");
int frame_count = 0;
//6.一帧一帧的读取压缩数据
while (av_read_frame(pFormatCtx, packet) >= 0)
{
//只要视频压缩数据(根据流的索引位置判断)
if (packet->stream_index == v_stream_idx)
{
//7.解码一帧视频压缩数据,得到视频像素数据
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (ret < 0)
{
printf("%s", "解码错误");
return -1;
}
//为0说明解码完成,非0正在解码
if (got_picture)
{
//AVFrame转为像素格式YUV420,宽高
//2 6输入、输出数据
//3 7输入、输出画面一行的数据的大小 AVFrame 转换是一行一行转换的
//4 输入数据第一列要转码的位置 从0开始
//5 输入画面的高度
sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
pFrameYUV->data, pFrameYUV->linesize);
//输出到YUV文件
//AVFrame像素帧写入文件
//data解码后的图像像素数据(音频采样数据)
//Y 亮度 UV 色度(压缩了) 人对亮度更加敏感
//U V 个数是Y的1/4
int y_size = pCodecCtx->width * pCodecCtx->height;
fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);
fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);
fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);
frame_count++;
printf("解码第%d帧\n", frame_count);
}
}
//释放资源
av_free_packet(packet);
}
fclose(fp_yuv);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_free_context(pFormatCtx);
return 0;
}
原文链接: https://www.cnblogs.com/0-lingdu/p/12852021.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/197058
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!