本教程只针对windows64/32+vs2013环境配置
第一步 :配环境
1.打开ffmpeg官网中编译好的windows版本http://ffmpeg.zeranoe.com/builds/
64位windows系统和32位系统各有三个版本分别为Static版本,Share版本,Dev版本;
在这里建议无论是32位还是64位系统都直接配置32位的ffmpeg版本,除非你vs2013选择的是x64编译器。

将32位版本Share版本和Dev下载,解压。
在Dev里面主要是一些头文件和lib,在share版本里主要是dll文件。

2.打开vs2013,新建一个win32控制台程序。
选择项目-》属性-》c++目录,分别在包含目录和库目录中打开dev版本中的include和lib(指的是你解压dev保存的位置,在里面找到include和lib)

3项目-》属性-》链接器-》输入-》依赖项中填写

avcodec.lib
avformat.lib
avutil.lib
avdevice.lib
avfilter.lib
postproc.lib
swresample.lib
swscale.lib
保存即可。

第二步:视频解码,此处是一个完整的视频解码工程,从解码到存储,在vs2013上可以运行,如果编译不成功,可考虑是否是环境配错了。注意关闭防火墙。

// ConsoleApplication9.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma once
#pragma warning(disable:4996) extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include<libavfilter/avfilter.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
}; //定义BMP文件头
#ifndef _WINGDI_
#define _WINGDI_
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER; #endif //保存BMP文件的函数
void SaveAsBMP(AVFrame *pFrameRGB, int width, int height, int index, int bpp)
{
char buf[] = { };
//bmp头
BITMAPFILEHEADER bmpheader;
BITMAPINFOHEADER bmpinfo;
FILE *fp;
char *filename = new char[];
//文件存放路径,根据自己的修改
sprintf_s(filename, , "%s_%d.bmp", "D:\\", index);
if ((fp = fopen(filename, "wb+")) == NULL)
{
printf("open file failed!\n");
return;
} bmpheader.bfType = 0x4d42;
bmpheader.bfReserved1 = ;
bmpheader.bfReserved2 = ;
bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp / ; bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.biWidth = width;
bmpinfo.biHeight = height;
bmpinfo.biPlanes = ;
bmpinfo.biBitCount = bpp;
bmpinfo.biCompression = BI_RGB;
bmpinfo.biSizeImage = (width*bpp + ) / * * height;
bmpinfo.biXPelsPerMeter = ;
bmpinfo.biYPelsPerMeter = ;
bmpinfo.biClrUsed = ;
bmpinfo.biClrImportant = ; fwrite(&bmpheader, sizeof(bmpheader), , fp);
fwrite(&bmpinfo, sizeof(bmpinfo), , fp);
fwrite(pFrameRGB->data[], width*height*bpp / , , fp); fclose(fp);
} //主函数
int main(void)
{
unsigned int i = , videoStream = -;
AVCodecContext *pCodecCtx;
AVFormatContext *pFormatCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameRGB;
struct SwsContext *pSwsCtx;
const char *filename = "E:\\123.avi";//rtsp://192.168.2.214:554/bs0
AVPacket packet;
int frameFinished;
int PictureSize;
uint8_t *buf;
//注册编解码器
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
//打开视频文件
if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != )
{
printf("av open input file failed!\n");
exit();
}
//获取流信息
if (avformat_find_stream_info(pFormatCtx, NULL) < )
{
printf("av find stream info failed!\n");
exit();
}
//获取视频流
for (i = ; i<pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
} if (videoStream == -)
{
printf("find video stream failed!\n");
exit();
} pCodecCtx = pFormatCtx->streams[videoStream]->codec; pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL)
{
printf("avcode find decoder failed!\n");
exit();
}
//打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL)<)
{
printf("avcode open failed!\n");
exit();
} //为每帧图像分配内存
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc(); if ((pFrame == NULL) || (pFrameRGB == NULL))
{
printf("avcodec alloc frame failed!\n");
exit();
}
//获得帧图大小
PictureSize = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);
buf = (uint8_t*)av_malloc(PictureSize); if (buf == NULL)
{
printf("av malloc failed!\n");
exit();
}
avpicture_fill((AVPicture *)pFrameRGB, buf, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); //设置图像转换上下文
pSwsCtx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_BGR24,
SWS_BICUBIC,
NULL, NULL, NULL);
i = ;
while (av_read_frame(pFormatCtx, &packet) >= )
{
if (packet.stream_index == videoStream)
{
//真正解码
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished)
{
//反转图像 ,否则生成的图像是上下调到的
pFrame->data[] += pFrame->linesize[] * (pCodecCtx->height - );
pFrame->linesize[] *= -;
pFrame->data[] += pFrame->linesize[] * (pCodecCtx->height / - );
pFrame->linesize[] *= -;
pFrame->data[] += pFrame->linesize[] * (pCodecCtx->height / - );
pFrame->linesize[] *= -;
//转换图像格式,将解压出来的YUV420P的图像转换为BRG24的图像
sws_scale(pSwsCtx, pFrame->data, pFrame->linesize, , pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
//保存为bmp图
SaveAsBMP(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i, );
i++;
}
av_free_packet(&packet);
}
}
sws_freeContext(pSwsCtx);
av_free(pFrame);
av_free(pFrameRGB);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx); return ;
}

ffmpeg这样连接了后会花屏,解决方式如下(换成TCP连接方式):

AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "tcp", );
if(avformat_open_input(&pFormatCtx,"rtsp://192.168.2.214:554/bs0",NULL,&options)!=){
printf("Couldn't open input stream.\n");
return -;
}
这样连接了不会花屏,但是延时会越来越长

c++ 配置ffmpeg的更多相关文章

  1. Visual Studio 开发(二):VS 2017配置FFmpeg开发环境

    在上篇文章Visual Studio 开发(一):安装配置Visual Studio Code 中,我们讲了一下如何配置VS CODE,来编写和调试C的代码.如果你已经使用VS Code回顾和复习好C ...

  2. Windows配置ffmpeg

    一.ffmpeg简介 ffmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案. 支持操作系统: ...

  3. [转载]Windows x64下配置ffmpeg的方法

    ffmpeg简介 FFmpeg 是一款跨平台的,对视频.音频进行录制.转换.播放的命令行形式软件,它使用的是 libavcodec 编解码器.FFmpeg 官方网站是 http://ffmpeg.or ...

  4. FFmpeg + SoundTouch实现音频的变调变速

    本文使用FFmpeg + SoundTouch实现将音频解码后,进行变调变速处理,并将处理后的结果保存为WAV文件. 主要有以下内容: 实现一个FFmpeg的工具类,保存多媒体文件所需的解码信息 将解 ...

  5. iOS编译FFmpeg、kxmovie实现视频播放 (转载)

    由于FFmpeg开源框架的功能非常强大,可以播放的视频种类很多,同时添加第三方库kxmovie,实现视频播放,真的是爽爆了,因此今天来说一下关于FFmpeg在iOS手机上的一些配置过程,配置工具,还有 ...

  6. FFMPEG在嵌入式硬件上应用之 —— 基本环境搭建及编译

    前段时间在翻看电脑里面资料时,发现了以前做的在嵌入式硬件上面运行以ffmepg为基础,以嵌入式硬件解码的多媒体播放工作,发现都快忘记完了.今日得闲整理温习了一下ffmpeg在嵌入式上的运用,这里给大家 ...

  7. 【FFmpeg】Windows下FFmpeg调试

    为了深入了解ffmpeg的工作原理,需要阅读源代码,调试源代码.在Windows下调试ffmpeg源码,一种方法是在MinGW+Msys环境下,利用GDB进行调试:另一种是借助Eclipse进调试,其 ...

  8. 在Windows下利用Eclipse调试FFmpeg

    目录 [隐藏]  1 环境与软件 2 第一步:安装MinGW 3 第二步:配置编译环境 4 第三步:配置SDL 5 第四步:编译 5.1 编译faac 5.2 编译fdk-aac 5.3 编译x264 ...

  9. 在CentOS下利用Eclipse调试FFmpeg

    所需软件 64位软件打包下载链接:http://pan.baidu.com/s/1i3B08Up 密码:o50u https://yunpan.cn/cBKDSbrGDgBvz  访问密码 1f55 ...

随机推荐

  1. 小程序本地存储之wx.getStorageSync

    这个主要可以解决微信小程序的记录缓存,入输入框的搜索历史记录 直接上代码 setsearchMsg:function(){ var that=this if (this.data.inputValue ...

  2. re模块补充 configparse模块

    import rere.findall("(?:abc)+","abcabcabc")--->['abcabcabc'] import configpar ...

  3. 「USACO09FEB」改造路Revamping Trails

    传送门 Luogu 解题思路 有点像这题,但是现在这道不能跑k遍SPFA了,会TLE. 那么我们就跑分层图最短路,然后就变成模板题了. 细节注意事项 别跑SPFA就好了. 参考代码 #include ...

  4. python笔记12

    day12 今日内容 函数中高级(闭包/高阶函数) 内置函数 内置模块(.py文件) 内容回顾 函数基础概念 函数基本结构 def func(arg): return arg; v1 = func(1 ...

  5. C# FTp 上传,下载

    public class FtpHelper { string ftpServerIP; string ftpRemotePath; string ftpUserID; string ftpPassw ...

  6. 【LOJ2542】「PKUWC2018」随机游走

    题意 给定一棵 \(n\) 个结点的树,你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(Q\) 次询问,每次询问给定一个集合 \(S\),求如果从 \(x\) 出发一 ...

  7. Python3 中 的 绝对导入 与 相对导入

    背景: 在学习tf的时候,看到了from __future__ import absolute_import,所以登记学习一下. 概览: 一般模块导入规则: import xxx时搜索文件的优先级如下 ...

  8. ubuntu18.04窗口截图和选区截图快捷键

    解决方法: 1.点击左下角的系统设置. 2.点击设备. 3.点击键盘,可查看各种截图操作的快捷键.  PS:双击图中的快捷键可以设置新的快捷键.

  9. CodeForces - 876C Classroom Watch (枚举)

    题意:已知n,问满足条件"x的各个数字之和+x=n"的x有几个并按升序输出. 分析: 1.n最大1e9,10位数,假设每一位都为9的话,可知x的各个数字之和最大可以贡献90. 2. ...

  10. 题目:给定一数组 例如:a = [1,2,3,5,2,1] 现用户提供一个数字 请返回用户所提供的数字的所有下标

    def test(ary): ds = {} for i in range(len(ary)): if ds.get(ary[i]): ds[ary[i]].append(i) else: ds[ar ...