文章目录

  • 01FFmpeg-VS213开发环境搭建(windows)
    • 官网资料:
    • **1、下载ffmpeg包(dll、include、lib) **
    • **2、环境配置**
      • **2.1 新建工程**
      • 2.2 将1中准备的dll、include、lib拷贝到2.1建立的FFmpegEnv工程目录下**
      • **2.3 右击FFmpegEnv工程“属性”**
      • **2.4 “VC++目录”---->"包含目录"---->编辑添加:
      • **2.5 “链接器”---->"常规"---->“附加库目录”---->添加
      • **2.6 “连接器”---->"输入"---->“附加依赖项“---->**
    • **3、测试**
      • **测试代码(FFmpegEnv.cpp):**
    • 4、出现的错误
      • **4.1 编译出现---error LNK2019: 无法解析的外部符号**
      • 4.2、头文件找不到
      • **4.3 运行程序,弹如下错误**
    • 5、参考资料

01FFmpeg-VS213开发环境搭建(windows)

官网资料:

​ 官网是最原始的和最好的学习资料
​ 官网 http://ffmpeg.org/

]

**1、下载ffmpeg包(dll、include、lib) **

下载地址:https://ffmpeg.zeranoe.com/builds/win64/ https://ffmpeg.zeranoe.com/builds/

包含三个版本:Static、Shared以及Dev 一般我们用到的是Shared(.dll)和Dev(.lib;*.h)。

Static — 包含3个应用程序:ffmpeg.exe , ffplay.exe , ffprobe.exe,体积都很大,相关的DLL已经被编译到exe里面去了。
Shared — 除了ffmpeg.exe , ffplay.exe , ffprobe.exe之外还有一些DLL,exe体积很小,在运行时到相应的DLL中调用功能。
Dev — 开发者(developer)版本,里面包含了库文件xxx.lib以及头文件xxx.h,这个版本不含exe文件。

开发者下载Shared(include、lib)和Dev(dll),依据自己电脑选择相应的平台下载,本文下载的是:

​ [外链图片转存失败(img-BHDvG5XM-1567264621768)(…/images/FFmpeg-dev-share.png)]

2、环境配置

2.1 新建工程

新建工程—>Visual C++ --> Win32控制台应用程序–>指定名称(FFmpegEnv)、位置、解决方案路径–>下一步–>空项目

2.2 将1中准备的dll、include、lib拷贝到2.1建立的FFmpegEnv工程目录下**

ffmpeg-3.4.1-win64-dev目录下的文件lib include文件夹拷贝到FFmpegEnv目录下。(注:lib 可以改名为ffmpeglib)

ffmpeg-3.4.1-win64-shared目录下的文件bin文件夹拷贝到FFmpegEnv目录下。(注:bin 可以改名为ffmpegbin-动态库)

2.3 右击FFmpegEnv工程“属性”

选择配置属性-->常规--->输出目录--->拷贝 **$(SolutionDir) ** 这个就是我们当前解决方案的路径,一个相对路径,以后移植起来比较方便。

**2.4 “VC++目录”---->“包含目录”---->编辑添加:

$(SolutionDir)include—>确定 **

**2.5 “链接器”---->“常规”---->“附加库目录”---->添加

$(SolutionDir)ffmpeglib**

2.6 “连接器”---->“输入”---->“附加依赖项“---->

添加avdevice.lib avfilter.lib avformat.lib avutil.lib postproc.lib swresample.lib swscale.lib

3、测试

在ffmpeg-3.4.2-win64-dev\examples目录下复制metagata.c源码到新建的FFmpegEnv.cpp文件中,这是一个独立的demo,作用是打印音视频媒体文件基本信息。(注:我直接用的FFmpegEnv.cpp的代码,王纲老师的代码)

**注意!!!**有些人下载的metadata.c里面的头文件需要修改:

#ifndef  _FFMPEGENV_H_
#define  _FFMPEGENV_H_extern "C"
{
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
#include "libavutil/time.h"
#include "libavutil/fifo.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavfilter/avfiltergraph.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
}
#endif

(why? C++工程直接调用ffmpeg的c函数库会导致c函数无法解析,需要用extern "C"进行声明即可)

测试代码(FFmpegEnv.cpp):

// FFmpegEnv.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"
#include "ffmpegenv.h"
#include <string>
#include <memory>
#include <thread>
#include <iostream>
using namespace std;AVFormatContext *inputContext = nullptr;
AVFormatContext * outputContext;
int64_t lastReadPacktTime;static int interrupt_cb(void *ctx)
{int  timeout = 3;if (av_gettime() - lastReadPacktTime > timeout * 1000 * 1000){return -1;}return 0;
}
int OpenInput(string inputUrl)
{inputContext = avformat_alloc_context();lastReadPacktTime = av_gettime();inputContext->interrupt_callback.callback = interrupt_cb;int ret = avformat_open_input(&inputContext, inputUrl.c_str(), nullptr, nullptr);if (ret < 0){av_log(NULL, AV_LOG_ERROR, "Input file open input failed\n");return  ret;}ret = avformat_find_stream_info(inputContext, nullptr);if (ret < 0){av_log(NULL, AV_LOG_ERROR, "Find input file stream inform failed\n");}else{av_log(NULL, AV_LOG_FATAL, "Open input file  %s success\n", inputUrl.c_str());}return ret;
}shared_ptr<AVPacket> ReadPacketFromSource()
{shared_ptr<AVPacket> packet(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_packet_free(&p); av_freep(&p); });av_init_packet(packet.get());lastReadPacktTime = av_gettime();int ret = av_read_frame(inputContext, packet.get());if (ret >= 0){return packet;}else{return nullptr;}
}
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
{if (pkt->pts != AV_NOPTS_VALUE)pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);if (pkt->dts != AV_NOPTS_VALUE)pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);if (pkt->duration > 0)pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
}
int WritePacket(shared_ptr<AVPacket> packet)
{auto inputStream = inputContext->streams[packet->stream_index];auto outputStream = outputContext->streams[packet->stream_index];av_packet_rescale_ts(packet.get(), inputStream->time_base, outputStream->time_base);return av_interleaved_write_frame(outputContext, packet.get());
}int OpenOutput(string outUrl)
{int ret = avformat_alloc_output_context2(&outputContext, nullptr, "mpegts", outUrl.c_str());if (ret < 0){av_log(NULL, AV_LOG_ERROR, "open output context failed\n");goto Error;}ret = avio_open2(&outputContext->pb, outUrl.c_str(), AVIO_FLAG_WRITE, nullptr, nullptr);if (ret < 0){av_log(NULL, AV_LOG_ERROR, "open avio failed");goto Error;}for (int i = 0; i < inputContext->nb_streams; i++){AVStream * stream = avformat_new_stream(outputContext, inputContext->streams[i]->codec->codec);ret = avcodec_copy_context(stream->codec, inputContext->streams[i]->codec);if (ret < 0){av_log(NULL, AV_LOG_ERROR, "copy coddec context failed");goto Error;}}ret = avformat_write_header(outputContext, nullptr);if (ret < 0){av_log(NULL, AV_LOG_ERROR, "format write header failed");goto Error;}av_log(NULL, AV_LOG_FATAL, " Open output file success %s\n", outUrl.c_str());return ret;
Error:if (outputContext){for (int i = 0; i < outputContext->nb_streams; i++){avcodec_close(outputContext->streams[i]->codec);}avformat_close_input(&outputContext);}return ret;
}void CloseInput()
{if (inputContext != nullptr){avformat_close_input(&inputContext);}
}void CloseOutput()
{if (outputContext != nullptr){for (int i = 0; i < outputContext->nb_streams; i++){AVCodecContext *codecContext = outputContext->streams[i]->codec;avcodec_close(codecContext);}avformat_close_input(&outputContext);}
}
void Init()
{av_register_all();avfilter_register_all();avformat_network_init();av_log_set_level(AV_LOG_ERROR);
}
int _tmain(int argc, _TCHAR* argv[])
{Init();int ret = OpenInput("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"); //大熊兔(点播)if (ret >= 0){ret = OpenOutput("G:\\FFmpeg\\FFmpegEnv\\videos\\test.ts");}if (ret <0) goto Error;while (true){auto packet = ReadPacketFromSource();if (packet){ret = WritePacket(packet);if (ret >= 0){cout << "WritePacket Success!" << endl;}else{cout << "WritePacket failed!" << endl;}}else{break;}}
Error:CloseInput();CloseOutput();while (true){this_thread::sleep_for(chrono::seconds(100));}return 0;
}

运行结果:

​ 打开一个联网的视频流(//大熊兔(点播))作为输入流,然后将播放的内容保存到本地 (需要自己指定路径)。

ret = OpenInput("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"); //大熊兔(点播)
ret = OpenOutput("G:\\FFmpeg\\FFmpegEnv\\videos\\test.ts");

4、出现的错误

4.1 编译出现—error LNK2019: 无法解析的外部符号

[外链图片转存失败(img-6leW8CzT-1567264621770)(…/images/errorLINK2019.png)]
解决方法:

     1) ffmpeg的环境已经配置好(第2部分)2)配置管理器--平台由Win32修改为x64(原因未知)
参考 https://blog.csdn.net/ljh0302/article/details/50011587

[外链图片转存失败(img-yRViQP8W-1567264621770)(…/images/配置管理器-x64.png)]

4.2、头文件找不到

 解决方案: 对头文件的使用比较特殊  扩展C
extern "C"
{
#include <stdio.h>
#include <libavcodec\avcodec.h>
#include <libavformat\avformat.h>
#include <libswscale\swscale.h>
#include <libavutil\pixfmt.h>
#include <libavutil\imgutils.h>
};
参考链接;
Visual C++ 无法解析的外部符号问题小结(调用FFmpeg遇到)

4.3 运行程序,弹如下错误

    ![img](../images/wps1.jpg)
**解决方法:**将文件夹内的dll文件拷贝到Debug文件夹内

[外链图片转存失败(img-fSU3Nzxp-1567264621771)(…/images/wps2.jpg)]

5、参考资料

1 https://ffmpeg.zeranoe.com/builds/
2/3 https://blog.csdn.net/spaceyqy/article/details/43115391
https://blog.csdn.net/qq_28425595/article/details/51488869
https://www.cnblogs.com/bhlsheji/p/5359390.html
4.1 https://blog.csdn.net/ljh0302/article/details/50011587

01FFmpeg-VS213开发环境搭建(windows)相关推荐

  1. Java开发环境搭建:Windows

    Java开发环境搭建:Windows JDK下载安装 JDK下载 设置环境变量 测试是否安装成功 开发工具下载安装:Idea Idea下载 Idea安装 Idea开发JAVA 推荐一波 JDK下载安装 ...

  2. docker开发环境搭建(windows)

    目录 Docker开发环境搭建 1.1mysql 1.1.1拉取镜像 1.1.2本地新建目录(windows为例) 1.1.3创建容器并添加本地映射 1.1.4连接容器数据库验证成功 2.1nacos ...

  3. 【PHP】PHP开发环境搭建——windows篇(apache2.2.22+php5.3.29+mysql5.7)

    一直有学习php的想法,也稍微弄过一点点php,以前是用xmpp或者wamp来实现这会让很多初学者盲目,不知道是啥.现在来搭建一遍开发环境,并且说明下每一个的用途. 1.下载apache的http s ...

  4. <python开发> python开发 环境搭建(windows)

    以下提供软件的官方地址链接,如果不想去官方下载,可使用作者整理好的软件包,链接:python环境搭建软件包链接地址 一.python 安装 1.安装包的官网:下载地址 打开后界面如下: 2.点击&qu ...

  5. PHP开发环境搭建--Windows

    本文目录 准备 开始 PHP Apache 此处参考1 参考2 MySQL 错误锦集 附httpd命令 搭建php开发环境有两种方式,一种是自定义搭建:另一种是使用php服务组件,PHP服务器组件非常 ...

  6. 本地开发环境搭建(windows)

    一.虚拟器安装 1.概念 ・为什么要搭建搭建模拟环境 在租借服务器前用手中的PC模拟一个服务器的环境,可以打包与团队人员分享 ・什么是Vagrant https://segmentfault.com/ ...

  7. PHP开发环境搭建(windows)

    文章目录 Apache 下载 配置 安装服务 异常信息 测试Apache PHP 下载 配置Apache支持PHP 配置PHP 测试PHP 其他问题 开启mod_rewrite 开启curl模块 终于 ...

  8. python搭建qt开发环境_QT开发环境搭建(Windows)

    正式启航踏上Qt开发之路 遇到的第一个难题,搭环境 我本来是准备装个虚拟机在Ubuntu上开发,搞了一天好像生成不了可执行文件,就决定从Windows先装个试试 下面是我的步骤和安装过程中遇到的一些小 ...

  9. Cordova/Ionic Android 开发环境搭建 - Windows系统

    电脑操作系统 - windows 10 IDE - WebStorm 2019 Node v10.15.3 npm v6.4.1 Ionic v3 Angula v5 Cordova 移动设备 - 机 ...

  10. a9g 开发环境搭建 windows

    a9g 支持以单片机的开发方式进行开发 a9g有两种方式,一种以AT指令,这种方式有外接MCU进行通信,调试的可以直接用电脑串口 调试. 别外一种方式:以单片机方式写固件, 单片机方式: 总共有两个文 ...

最新文章

  1. 协议学习:TCP/IP协议之物理层 上
  2. lsof查看占用高_查看端口占用情况lsof,并关闭对应进程kill
  3. LiveVideoStack线上交流分享 (十五) —— 熟悉技术的边界,实现1+1+13
  4. python中的命名空间_深入理解Python中的命名空间和范围
  5. php 多线程处理redis,redis的多线程
  6. 【模板/经典题型】并查集维护生成树
  7. 百度地图隐藏LOGO显示
  8. 中国坚果脱壳机行业市场供需与战略研究报告
  9. EJBCA 在windows上的安装
  10. 电脑投屏电视怎么设置_夏普电视怎么投屏?投屏功能在哪?
  11. swagger2常用注解
  12. Final类型数据的初始化
  13. journalctl命令详解
  14. CardView覆盖问题
  15. html td 跨两个,【单选题】在HTML中,td标签的( )属性用于创建跨多个行的单元格。...
  16. mysql存特殊符号失败_mysql存储符号表情失败
  17. final 和effectively final区别
  18. Thingworx 调用外部接口
  19. 关于网易2018实习生招聘的“道路布灯”问题
  20. 修复IE默认主页——注册表篇

热门文章

  1. Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations学习与理解
  2. js节点都有哪些类型?怎么判断是哪种节点类型?
  3. js中节点关系及相关操作
  4. MapboxMap之Expression(一)
  5. 大数据挖掘建模案例分享:利用BP神经网络算法进行用户行为分析(三)
  6. linux安装rides
  7. oracle 存储过程误删,oracle恢复误删的procedure存储过程
  8. NGUI发布后看不见UI层解决
  9. dreamweaver网页设计作业制作 学生个人网页猫眼电影 WEB静态网页作业模板 大学生个人主页博客网页代码 dw个人网页作业成品
  10. 身体是革命的本钱(运动与健身)