ffmpeg学习之-window下编译安装nginx用ffmpeg推抓视频流

感兴趣可以加QQ群85486140,大家一起交流相互学习下!

1.初试牛刀

在网上搜索了nginx可以搭建rtmp server,实现推流拉流功能,就立马从官网下载nginx可执行程序,结果按着博文 没有成功。修改nginx.conf,添加rtmp server后,退出重启时报了下面的错误。

F:\nginx\nginx-1.14.2>nginx -s stop
nginx: [emerg] unknown directive “rtmp” in F:\nginx\nginx-1.14.2/conf/nginx.conf:16

后来才了解到,官网的nginx.exe中时不包含rtmp 功能的。为此需要自己编译

2.下载需要的软件和库

  • 下载安装MinGw

官网:http://www.mingw.org/
下载安装器:https://sourceforge.net/projects/mingw/files/latest/download?source=files
先安装安装器,执行时,本人选择下面几个选项。这个需要花一段时间,耐心等待吧。
在这里插入图片描述
下载完成后,勾选已经变成浅绿色。
在这里插入图片描述
此时再安装目下即可看到msys目录,点击msys目录下的msys.bat,即可看到弹出一个shell终端,应为后面的一些配置,需要用到类linux环境,msys就是为这个来的。至于他们详细介绍看官网介绍吧。
在这里插入图片描述

  • 安装Mercurial client.
    Mercurial是一个版本管理工具,和git属于一类软件。由于nginx是用Mercurial进行版本管理的,所以需要先安装此应用,才能拉代码。请根据系统类型,选择合适的版本。下载地址:https://www.mercurial-scm.org/
    安装时,如果使用win+R命令行打印hg命令如果提示如下放左图,那么就已经安装成功了。但是本人在安装时遇到了点麻烦,没有一次性完成。在安装路径下,时可以发现有一个add_path.exe,可能是杀毒软件保护了吧,没有把环境变量配置进去吧。也可以在安装之前先把杀毒软件推出保户一段时间。环境变量配置如下右侧图片。
    在这里插入图片描述

  • 下载ngnix源码
    首先官网下载源码,执行下面命令,可能你本地还没有hg指令,安装好Mercurial客户端就有了(别忘了加到环境变量里面)
    hg clone http://hg.nginx.org/nginx
    当然也可以直接从官网下载,下载地址:http://nginx.org/download/nginx-1.14.2.zip ,不过使用hg指令下载的能看到其中的修改记录,直接下zip包没有修改提交记录。

  • pcre下载
    所有版本地址:https://ftp.pcre.org/pub/pcre/
    当前使用版本:https://ftp.pcre.org/pub/pcre/pcre-8.43.zip

  • zlib下载
    所有版本地址:http://zlib.net/fossils/
    当前使用版本:http://zlib.net/fossils/zlib-1.2.11.tar.gz

  • OpenSSL libraries
    当前较新版本:https://www.openssl.org/source/
    老版本:https://www.openssl.org/source/old/
    当前可用版本:https://www.openssl.org/source/openssl-1.1.1b.tar.gz

  • nginx-rtmp-modue下载
    当前使用版本:https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/v1.1.11

  • Strawberry Perl安装
    当前使用是最新的5.28.1.1的版本。之前的依赖都是源码包,需要在编译nginx时才会用到,这里一定要安装上
    历史版本:https://www.perl.org/get.html
    当前使用版本:http://strawberryperl.com/download/5.28.1.1/strawberry-perl-5.28.1.1-64bit.msi

3 编译nginx

3.1 nginx切换分支

首先使用hg branches命令查看当前有哪些分支

pc@jtwm /f/nginx/nginx_sources/nginx
$ hg branches
default 7469:bd1bba6c0184
stable-1.14 7425:ebf8c9686b8c
stable-1.12 7150:d00a24dd3dee
stable-1.10 6898:43ba2bb504fb
stable-1.8 6362:28bb9683dd9a
stable-1.6 6095:c94bc8f034ff
stable-1.4 5613:b39df933f0e6
stable-1.2 5208:9c3c460f8a05
stable-1.0 4591:bfc00a6715ac
stable-0.7 3956:f9e9f96e3407
stable-0.8 3951:56e6f3d4561c
stable-0.5 3130:b52b6c3bb2d1
stable-0.6 3128:c4659427a6cc
radix_with_skip 2369:12e8e0045096

由于本人使用的是stable-1.14(一定要选择一个稳定的版本),直接使用hg checkout stable-1.14,切换到该分支。切换后如下所示:
在这里插入图片描述

3.2 解压依赖库源码

  • 新建动态库目录objs/lib
    下面的准备条件是在nginx源码目录下
mkdir objs
mkdir objs/lib
cd objs/lib
  • 拷贝之前下载的几个库
    将下面的几个库拷贝到objs/lib下面进行并进行解压
-rw-r--r-- 1 pc Administrators 8213737 Mar  2 12:41 openssl-1.1.1b.tar.gz
-rw-r--r-- 1 pc Administrators 2320353 Mar  2 12:23 pcre-8.43.zip
-rw-r--r-- 1 pc Administrators  607698 Mar  2 14:24 zlib-1.2.11.tar.gz

将下载的nginx-rtmp-modue的源码拷贝到release下面,最好拷贝到objs/lib下面,一开始自以为可以新建一个目录,专门用于中间文件的的保存,后来配置过程中发现配置的时候一定要在源码根目录下配置

  • 配置ngnix
    这里由于命令后缀比较多,最好新建一个批处理文件,这里本人使用的命令如下所示(注意目录):
auto/configure --with-cc=cl --builddir=objs --prefix= \
--conf-path=conf/nginx.conf --pid-path=logs/nginx.pid \
--http-log-path=logs/access.log --error-log-path=logs/error.log \
--sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp \
--http-proxy-temp-path=temp/proxy_temp \
--http-fastcgi-temp-path=temp/fastcgi_temp \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-select_module \
--with-debug \
--with-http_ssl_module \
--with-pcre=./objs/lib/pcre-8.43 --with-zlib=./objs/lib/zlib-1.2.11 --with-openssl=./objs/lib/openssl-1.1.1b \
--add-module=./release/nginx-rtmp-module-1.1.11

配置之后的结果如下图所示:
在这里插入图片描述
配置过程中提示:找不到mspdb100.dll,并弹出下面这个窗口。
在这里插入图片描述
在C盘搜索了一下mspdb100.dll,没有找到这个文件。在D盘搜索一下发现了2个mspdb100.dll ,这两个应该都可以使用,这里使用第二个吧(在IDE目录下),自己还把如下图所示:

在这里插入图片描述

3.3 启动编译


注意!!! 这里要提前注意:

【1】执行vcvars32.bat,这个在路径在vs安装路径下,本地路径为:D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
【2】配置openssl-1.1.1b,这个一定要先配置,我们来看一下它的README。

 See the appropriate file:
        INSTALL         Linux, Unix, Windows, OpenVMS, ...
        NOTES.*         INSTALL addendums for different platforms

上面发现安装是在INSTALL文件中

  on Windows (only pick one of the targets for configuration):

    $ perl Configure { VC-WIN32 | VC-WIN64A | VC-WIN64I | VC-CE }
    $ nmake
    $ nmake test
    $ nmake install

编译Win64/x64架构的:

perl Configure VC-WIN64A

编译Win64/IA64架构:

perl Configure VC-WIN64I

这里我本地机器X64架构,执行perl Configure VC-WIN64I

注意!!!上面的perl命令一定要先执行,要不然会报很奇怪的问题。

default:        build
clean:
        rm -rf Makefile objs
build:
        $(MAKE) -f objs/Makefile
install:
        $(MAKE) -f objs/Makefile install
modules:
        $(MAKE) -f objs/Makefile modules
upgrade:
        /nginx.exe -t

        kill -USR2 `cat /logs/nginx.pid`
        sleep 1
        test -f /logs/nginx.pid.oldbin

        kill -QUIT `cat /logs/nginx.pid.oldbin`

上面是ngnix源码根目录下的Makefile,直接可以执行nmake build,nmake clean,nmake install.


在执行编译前,可能本地没有nmake命令,这个是vs自带的可以解析Makefile的工具,工具路径在D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin(本人用的vs2010安装在D盘),没有编译VS工具的需要安装一个了。

  • 错误1----找不到pcre.h.generic
    在这里插入图片描述
    在执行make objs/Makefile时发现如下拷贝动作执行失败了,一般情况下不应该出现这种情况,为此打开了Makefile看了一下,发现下面的头文件配置也没问题啊!在这里插入图片描述
    搜索了一下pcre.h.generic所在目录,结果发现头文件竟然在./lib/pcre-8.43/pcre-8.43/pcre.h.generic,而不是./lib/pcre-8.43/pcre.h.generic,注意上面的头文件路径也是./lib/pcre-8.43/pcre.h.generic
pc@jtwm /f/nginx/nginx_sources/nginx/objs
$ find . -name "pcre.h.generic"
./lib/pcre-8.43/pcre-8.43/pcre.h.generic

到这里自然就知道源码目录多套了一层,唉,之前我是用windows的解压工具解压出来的源码,这里要特别注意!现在做的就只能清理工程,重新编译了。源码根目录下执行nmake clean
在这里插入图片描述
到这里重新走一下2.2的配置流程。
下面提示

  • 错误2----perl版本过低
    报错信息如下,之前我已经安装了5.28版本的perl,为何这里还报了版本过低,而且需要的v5.10.0的版本。

Perl v5.10.0 required–this is only v5.8.8, stopped at Configure line 12.
BEGIN failed–compilation aborted at Configure line 12.
NMAKE : fatal error U1077: “perl”: 返回代码“0x2”

在这里插入图片描述
查找了一下perl.exe的位置,发现使用了mingw自带的那个版本
在这里插入图片描述
C:\MinGW\msys\1.0\bin\perl.exe,做一下备份,更改一下名字mv perl.exe perl_v5.8.8.exe。这时msys就找不到自带的perl.exe,转而使用之前安装的perl.exe.
在这里插入图片描述
上面看到msys已经识别到5.28版本的perl,继续编译。

  • 错误3----NASM 没有安装

NASM not found - make sure it’s installed and available on %PATH%
NMAKE : fatal error U1077: “perl”: 返回代码“0x1”

在这里插入图片描述
下载nasm编译器()
历史版本:https://www.nasm.us/
当前使用版本:http://www.nasm.us/pub/nasm/releasebuilds/2.14.02/
解压出来后将下面2个命令拷贝到msys的bin目录下,同时添加到系统变量里面。如下所示。
在这里插入图片描述
拷贝之后,在msys中验证已经可以找到了,继续编译吧。
在这里插入图片描述

  • 错误4----找不到头文件"limits.h"
    这个一开始以为mingw没有配置好,所以在makefile中也试了把mingw的头文件加进去,结果还是编译不过。但是写了一个测试的hello_world.c测试程序包含limits.h,结果是可以编译过去的,以上操作都是在msys中操作的。到后来发现ngnix配置完后,编译操作是要在windows中编译的,是要使用系统中的头文件的,而非mingw中头文件,是用msys中命令编译时使用的时mingw自身的头文件。为此需要执行vcvars32.bat来初始化
    【1】vcvars32.bat(D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin)
    在这里插入图片描述
    【2】错误log
    在这里插入图片描述
    此处卡了4个小时,唉!!!!!!!!!!!!!!!

  • 错误5-asm命令是无效的
    注意:这个命令当时是使用版本openssl-1.0.2l,编译出来的问题,本次使用较新的版本·openssl-1.1.1b不存在这个问题。特此记录下来
    在这里插入图片描述
    在源码根目录下:

        if exist ms\do_ms.bat (                                         \
                ms\do_nasm                                              \
                && $(MAKE) -f ms\nt.mak                                 \
                && $(MAKE) -f ms\nt.mak install                         \

上面把ms\do_ms更该成ms\do_nasm,下面去ms目录下也发现有这两个文件,这里由于本地使用的nasm的汇编器,所以选择do_nasm.

pc@jtwm /f/nginx/nginx_sources/nginx/objs/lib/openssl-1.0.2l/ms
$ ls -l do_ms.bat do_nasm.bat
-rwxr-xr-x 1 pc Administrators 357 May 25  2017 do_ms.bat
-rwxr-xr-x 1 pc Administrators 261 May 25  2017 do_nasm.bat
  • 编译完成
    重新执行在windows shell执行nmake build命令,这里一次性编译成功了。
    在这里插入图片描述

4 nginx的配置和运行

4.1 nginx运行环境配置

下载ngnix官方的程序包,然后替换nginx.exe即可达到目的。这里本人执行前编译的是nginx-1.14.2的版本,所以就下载这个吧,下载地址为 http://nginx.org/download/nginx-1.14.2.zip ,下载之后用本地编译的nginx.exe,替换下载包里面的nginx.exe.如下:

drwxr-xr-x 2 pc Administrators    4096 Feb 28 23:48 conf
drwxr-xr-x 4 pc Administrators       0 Dec  4 22:53 contrib
drwxr-xr-x 2 pc Administrators    4096 Dec  4 22:53 docs
drwxr-xr-x 2 pc Administrators       0 Dec  4 22:53 html
drwxr-xr-x 2 pc Administrators       0 Feb 28 23:34 logs
-rwxr-xr-x 1 pc Administrators 3847680 Mar  2 22:56 nginx.exe
drwxr-xr-x 7 pc Administrators    4096 Feb 28 23:34 temp

注意上面的nginx.exe的时间戳就是下午本地编译时间。

4.2 rtmp server配置

events {
    worker_connections  1024;
}
#添加rtmp server开始处
rtmp {
    server {

    listen 1935;

    application videotest{

    live on;

    }
}
}
# 添加rtmp server结束处。

http {
    include       mime.types;
    default_type  application/octet-stream;

4.3 运行nginx rmp server

2019-3-3凌晨时,尝试过启动5次,但是查看配置后,仍然无所获(可能脑子不够使了吧)。早上起来后看了一下error.log,推敲一下http directive is not allowed here,在查看一下conf/nginx.conf发现配置的rtmp server最外层少了一个大括号。唉!

2019/03/03 00:24:24 [emerg] 5920#1160: “http” directive is not allowed here in F:\nginx\nginx-1.14.2/conf/nginx.conf:28
2019/03/03 00:27:06 [emerg] 7568#17176: “http” directive is not allowed here in F:\nginx\nginx-1.14.2/conf/nginx.conf:28
2019/03/03 00:43:51 [emerg] 5988#13040: “http” directive is not allowed here in F:\nginx\nginx-1.14.2/conf\nginx.conf:28
2019/03/03 00:45:19 [emerg] 16804#16980: “http” directive is not allowed here in F:\nginx\nginx-1.14.2/conf/nginx.conf:28
2019/03/03 00:47:14 [emerg] 16108#17456: “http” directive is not allowed here in F:\nginx\nginx-1.14.2/conf\nginx.conf:28
2019/03/03 10:10:58 [emerg] 8084#14344: bind() to 0.0.0.0:8080 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

大括号补上之后,再一次启动nginx server,这一次error log中又多了上面最后一条bind() ..failed,可以发现8080没权限访问,改成80端口。再一次尝试启动rtmp server,这一次error.log中没有再打印其它错误log。运行下面一条指令:
tasklist /fi "imagename eq nginx.exe"

F:\nginx\nginx-1.14.2>tasklist /fi “imagename eq nginx.exe”

映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
nginx.exe 12188 Console 1 7,608 K
nginx.exe 3896 Console 1 7,972 K

如果上面打印了nginx的进程,则成功运行了。再浏览器中输入localhost,会打印出下面页面。
在这里插入图片描述
一些其它指令:

nginx -s stop //停止nginx
nginx -s quit //关闭nginx
nginx -s reload //当改变配置后,重启启动nginx,可以运行此命令。
nginx -s reopen //重新打开日志文件,不是重启,特别注意

5. ffmpeg 推拉流操作

5.1 ffmpeg推流

ffmpeg -re -i box.mp4 -vcodec copy -acodec copy -b:v 800k -b:a 32k -f flv rtmp://localhost/videotest

在这里插入图片描述
上面ffmpeg的配置参数可以参考官网:http://ffmpeg.org/ffmpeg.html#toc-Generic-options 简单记录一下重要的参数。

  • -re:使用视频原始的帧率进行推流,要不然一股脑都推给服务器了,服务器没有接受好(加入服务器buffer缓冲不够,那么就会有丢失了),官方文档介绍如下:

Read input at native frame rate. Mainly used to simulate a grab device, or live input stream (e.g. when reading from a file). Should not be used with actual grab devices or live input streams (where it can cause packet loss). By default ffmpeg attempts to read the input(s) as fast as possible. This option will slow down the reading of the input(s) to the native frame rate of the input(s). It is useful for real-time output (e.g. live streaming).

  • -i: input,输入文件路径
  • vcodec:设置video的解码器类型,这里设置copy,意思就是要和原视频里面的解码器一致。如果原视频使用h264,那么这里就是h264.官网参看连接:http://ffmpeg.org/ffmpeg.html#toc-Stream-specifiers-1
  • -acodec copy:设置audio的解码器和源文件一致。同样可以参考:http://ffmpeg.org/ffmpeg.html#toc-Stream-specifiers-1
  • -b:v 800k -b:a 32k:设置音视频的码率,这里视频1s发送800k,音频的采样率时32k。
  • -f:flv:设置音视频的输出格式,这里设置flv格式。

5.2 ffmpeg拉流

ffplay rtmp://localhost/videotest

在这里插入图片描述
注意上面的server,确实是rtmp server了。而且从2.706s开始播放的。

5.3 成像效果图:

这里推了一段opencv中拿过来的mp4视频。到这里RTMP基本操作配置好了,花了一天时间编译安装!!在这里插入图片描述
好了,ffmpeg后面会继续学起来,现在算是能手动推流和拉流了。

包含rtmp nginx编译源码以及其它window工具链

windows编译源码和库:https://download.csdn.net/download/armwind/10991002
windows工具链和库:
链接:https://pan.baidu.com/s/1w0gRQrCGJcG70n6V95njlQ
提取码:x3o7

参考文档和PATH配置

  • 参考文档
    参考博客1: https://www.cnblogs.com/qfb620/p/5508468.html windows下安装nginx
    参考博客2: https://www.cnblogs.com/icbc/p/4147138.html
    参考博客3:https://blog.csdn.net/i348018533/article/details/51701865 mingw和nginx安装编译
    参考博客4:http://www.cnblogs.com/lidabo/p/8990348.html 安装mingw
    官方在window上编译方法:http://nginx.org/en/docs/howto_build_on_win32.html
    官方window安装文档:http://nginx.org/en/docs/windows.html

  • path配置:
    在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值