本教程只针对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. VirtualBox安装Debian

    1.下载Debian的dvd1,按照http://www.jb51.net/os/85858.html网上教程安装Debian 1.1.我创建了20G的虚拟磁盘,分区的时候我分了3个区,2G交换空间, ...

  2. java#tostring

    通常使用apache-commons 来生成tostring方法,但是对于类型为java.util.Date的字段打印效果并不是我们想要的. @Override public String toStr ...

  3. spyder崩溃修复

    实验室突然断电,重启电脑后spyder崩溃 在anaconda命令行中输入命令失败 StackOverflow上找的解决方案,适合win10系统,简单粗暴 在win10搜索里面找,点一下就自动修复了

  4. 前端学习笔记系列一:15vscode汉化、快速复制行、网页背景图有效设置、 dl~dt~dd标签使用

    ctrl+shift+p,调出configure display language,选择en或zh,若没有则选择安装使用其它语言,则直接呼出扩展程序搜索界面,选择,然后安装,重启即可. shift+a ...

  5. 数据库同步和使用JSONObject让Java Bean“原地满状态复活”

    分类: [java]2013-11-28 21:04 729人阅读 评论(0) 收藏 举报 简介我为什么写这样一个简单的问题呢?首先介绍一下项目背景.最近需要做一个数据库同步的工作,也就是一个Web程 ...

  6. [LeetCode] 326. Power of Three + 342. Power of Four

    这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...

  7. postgres 删除外部表

    drop external table if exists tableName;

  8. 12541:TNS无监听状态

    上次在项目上遇见数据库报这个问题,然后网上几乎都是让重新进行配置数据库.配置多次之后还是无效,最后找到了问题的根源. 使用的是Oracle数据库,用PLSQL登录报的这个错误. 在计算机全局搜索:li ...

  9. P2312 解方程(随机化)

    P2312 解方程 随机化的通俗解释:当无法得出100%正确的答案时,考虑随机化一波,于是这份代码很大可能会对(几乎不可能出错). 比如这题:把系数都模一个大质数(也可以随机一个质数),然后O(m)跑 ...

  10. PHP开发环境(Apache+mysql+PHPstorm+php)的搭建

    一.搭建思路 从浏览器到web服务器(Apache)到PHP环境到mysql数据库 二.环境搭建 1.浏览器(略) 2.Apache的安装与配置 1)官方下载地址:https://httpd.apac ...