阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Nginx+FFmpeg搭建HLS直播转码服务器

500次阅读
没有评论

共计 11083 个字符,预计需要花费 28 分钟才能阅读完成。

目的: 使 Nginx 支持 Rtmp 协议推流,并支持 HLS 分发功能及 FFmpeg 转码多码率功能。

一、准备工作
模块:nginx-rtmp-module-master(支持 rtmp 协议)
下载地址:
http://nginx.org
https://github.com/arut/nginx-rtmp-module

1、安装依赖包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++

2、安装 git 工具:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tar xzvf git-latest.tar.gz
#cd git-2013-02-04
#autoconf
#./configure
#make && make install
# git –version
git version 1.8.1.GIT
#cd ..

【错误处理】

如果 git-latest.tar.gz 大小为 0,请下载 git-latest-tar.xz

然后 xz -d git-latest.tar.xz 解压为.tar

再 tar xvf git-latest.tar

3、安装 ffmpeg 及其依赖包:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure –enable-shared
#make
#make install
#cd ..

++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure –enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis-1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++++
#git clone http://git.chromium.org/webm/libvpx.git
#cd libvpx
#./configure  –enable-shared
#make
#make install
#cd ..
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make install
#cd ..

【错误处理】

编译 FAAC-1.28 时遇到错误:

mpeg4ip.h:126: error: new declaration‘char* strcasestr(const char*, const char*)’

解决方法:

从 123 行开始修改此文件 mpeg4ip.h,到 129 行结束。
修改前:
#ifdef __cplusplus
extern “C” {
#endif
char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

修改后:
#ifdef __cplusplus
extern “C++” {
#endif
const char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
cd ../../../

++++++++ffmpeg+++++++++++
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure  –prefix=/opt/ffmpeg/ –enable-version3  –enable-libvpx –enable-libfaac –enable-libmp3lame  –enable-libvorbis –enable-libx264 –enable-libxvid –enable-shared –enable-gpl –enable-postproc –enable-nonfree  –enable-avfilter –enable-pthreads
#make && make install
#cd ..
【错误处理】

如果提示 libvpx decoder version must be >=0.91,请从 Baidu 搜索一下 libvpx-v1.1.0.tar.bz 下载。

bzip2 -d  libvpx-v1.1.0.tar.bz2

tar xvf  libvpx-v1.1.0.tar.bz2

cd libvpx-v1.1.0

./configure –enable-shared –enable-vp8

make

make install

修改 /etc/ld.so.conf 如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig

【说明】

动态装入器找到共享库要依靠两个文件 — /etc/ld.so.conf 和 /etc/ld.so.cache。

安装完成后,ffmpeg 位于 /opt/ffmpeg/bin 目录下。

Linux 下编译 FFmpeg 之下载源文件并编译 http://www.linuxidc.com/Linux/2012-02/54565.htm

Linux 编译升级 FFmpeg 步骤 http://www.linuxidc.com/Linux/2013-08/88190.htm

CentOS 5.6 上安装 FFMPEG http://www.linuxidc.com/Linux/2011-09/42793.htm

在 Ubuntu 下安装 FFmpeg http://www.linuxidc.com/Linux/2012-12/75408.htm

Ubuntu 12.04 下编译 ffmpeg  http://www.linuxidc.com/Linux/2013-02/78857.htm

Ubuntu 14.04 下 PPA 安装 FFmpeg 2.2.2  http://www.linuxidc.com/Linux/2014-05/101322.htm

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2015-01/111182p2.htm

二、安装 Nginx 相关模块

1. 环境准备

yum install –y pcre pcre-devel

yum install –y zlib zlib-devel

2. 下载 nginx 及 rtmp 模块

wget http://nginx.org/download/nginx-1.6.2.tar.gz

tar xzvf nginx_1.6.2.tar.gz

git clone git://github.com/arut/nginx-rtmp-module.git

3. 编译 nginx-rtmp

./configure –prefix=/usr/local/nginx –add-module=../nginx-rtmp-module –with-http_stub_status_module

make

make install

安装完成后,nginx 位于 /usr/local/nginx/sbin 目录下,配置文件 nginx.conf 在 /usr/local/nginx/conf 目录下

++++++++ 测试 nginx 是否安装正确 +++++++++++

#cd /usr/local/nginx

#./sbin/nginx -c ./conf/nginx.conf

打开网页 http://localhost,如果显示 Welcome 表示安装下正确,如果没有显示,请查看一下 nginx 的日志。

++++++++ 测试 RTMP+++++++++++

修改 /usr/local/nginx/conf/nginx.conf 的内容如下:

#debug
daemon off;
master_process off;

error_log ./error.log debug;
events{
    worker_connections 1024;
}

rtmp{
    server {
        listen 1935;
        chunk_size 4000;

        #live
        application myapp {
            live on;
      }

}

从网上下载一款 RTMP 推流工具,我使用的 OBS(Open Broadcaster Software),开始推流 rtmp://your_ip/myapp/test,使用播放器(http://www.cutv.com/demo/live_test.swf)查看是否正常。

++++++++ 测试 HLS 切片功能 +++++++++++

修改 /usr/local/nginx/conf/nginx.conf 的内容如下:

#debug
daemon off;
master_process off;

error_log ./error.log debug;
events{
    worker_connections 1024;
}

rtmp{
    server {
        listen 1935;
        chunk_size 4000;

        #live
        application myapp {
            live on;
 
            hls on;
            hls_path /tmp/hls;
            hls_fragment 2s;
            hls_playlist_length 6s;

      }
    }
}

#HTTP
http{
    server {
        listen 80;

        #welcome
        location / {
            root  html;
            index  index.html index.htm;
        }

        #hls
        location /hls {
            types {
                application/vnd.apple.mpegusr m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        } 
    }
}

使用 VLC 或 iPAD 上的播放器进行查看 http://yourip/hls/test.m3u8。

 

++++++++ 测试 FFMPEG 转码功能 +++++++++++

修改 /usr/local/nginx/conf/nginx.conf 的内容如下:

#debug
daemon off;
master_process off;

error_log ./error.log debug;
events{
    worker_connections 1024;
}

rtmp{
    server {
        listen 1935;
        chunk_size 4000;

        #live
        application myapp {
            live on;
 
          exec /opt/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name
            -c:a copy  -c:v libx264 -b:v 512K -g 30 -f flv rtmp://localhost/hls/$name_low;
        }
     
        application hls {

            live on;

            hls on;
            hls_path /tmp/hls;
            hls_nested on;
            hls_fragment 2s;
            hls_playlist_length 6s;

          hls_variant _hi  BANDWIDTH=640000;

        }
    }
}

#HTTP
http{
    server {
        listen 80;

        #welcome
        location / {
            root  html;
            index  index.html index.htm;
        }

        #hls
        location /hls {
            types {
                application/vnd.apple.mpegusr m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        } 
    }
}

使用 ffmpeg 转码时,

exec /opt/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name
            -c:a copy  -c:v libx264 -b:v 512K -g 30 -f flv rtmp://localhost/hls/$name_low;

仅对视频进行转码,音频不做处理,同时向流从 myapp 转推到 hls,hls_variant 会生成一个多码率的 m3u8 文件,同时把切片文件存放到 test_low 目录下,访问多码率时,直接访问 http://yourip/hls/test.m3u8,根据这个 m3u8 中的实现的内容访问相应的码流,在本例中,实际码流 URL 为 http://yourip/hls/test_low/index.m3u8

————————————– 分割线 ————————————–

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

FFmpeg 的详细介绍:请点这里
FFmpeg 的下载地址:请点这里

目的: 使 Nginx 支持 Rtmp 协议推流,并支持 HLS 分发功能及 FFmpeg 转码多码率功能。

一、准备工作
模块:nginx-rtmp-module-master(支持 rtmp 协议)
下载地址:
http://nginx.org
https://github.com/arut/nginx-rtmp-module

1、安装依赖包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++

2、安装 git 工具:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tar xzvf git-latest.tar.gz
#cd git-2013-02-04
#autoconf
#./configure
#make && make install
# git –version
git version 1.8.1.GIT
#cd ..

【错误处理】

如果 git-latest.tar.gz 大小为 0,请下载 git-latest-tar.xz

然后 xz -d git-latest.tar.xz 解压为.tar

再 tar xvf git-latest.tar

3、安装 ffmpeg 及其依赖包:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure –enable-shared
#make
#make install
#cd ..

++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure –enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis-1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++++
#git clone http://git.chromium.org/webm/libvpx.git
#cd libvpx
#./configure  –enable-shared
#make
#make install
#cd ..
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make install
#cd ..

【错误处理】

编译 FAAC-1.28 时遇到错误:

mpeg4ip.h:126: error: new declaration‘char* strcasestr(const char*, const char*)’

解决方法:

从 123 行开始修改此文件 mpeg4ip.h,到 129 行结束。
修改前:
#ifdef __cplusplus
extern “C” {
#endif
char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

修改后:
#ifdef __cplusplus
extern “C++” {
#endif
const char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
cd ../../../

++++++++ffmpeg+++++++++++
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure  –prefix=/opt/ffmpeg/ –enable-version3  –enable-libvpx –enable-libfaac –enable-libmp3lame  –enable-libvorbis –enable-libx264 –enable-libxvid –enable-shared –enable-gpl –enable-postproc –enable-nonfree  –enable-avfilter –enable-pthreads
#make && make install
#cd ..
【错误处理】

如果提示 libvpx decoder version must be >=0.91,请从 Baidu 搜索一下 libvpx-v1.1.0.tar.bz 下载。

bzip2 -d  libvpx-v1.1.0.tar.bz2

tar xvf  libvpx-v1.1.0.tar.bz2

cd libvpx-v1.1.0

./configure –enable-shared –enable-vp8

make

make install

修改 /etc/ld.so.conf 如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig

【说明】

动态装入器找到共享库要依靠两个文件 — /etc/ld.so.conf 和 /etc/ld.so.cache。

安装完成后,ffmpeg 位于 /opt/ffmpeg/bin 目录下。

Linux 下编译 FFmpeg 之下载源文件并编译 http://www.linuxidc.com/Linux/2012-02/54565.htm

Linux 编译升级 FFmpeg 步骤 http://www.linuxidc.com/Linux/2013-08/88190.htm

CentOS 5.6 上安装 FFMPEG http://www.linuxidc.com/Linux/2011-09/42793.htm

在 Ubuntu 下安装 FFmpeg http://www.linuxidc.com/Linux/2012-12/75408.htm

Ubuntu 12.04 下编译 ffmpeg  http://www.linuxidc.com/Linux/2013-02/78857.htm

Ubuntu 14.04 下 PPA 安装 FFmpeg 2.2.2  http://www.linuxidc.com/Linux/2014-05/101322.htm

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2015-01/111182p2.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计11083字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7977687
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
星哥带你玩飞牛NAS-1:安装飞牛NAS

星哥带你玩飞牛NAS-1:安装飞牛NAS

星哥带你玩飞牛 NAS-1:安装飞牛 NAS 前言 在家庭和小型工作室场景中,NAS(Network Atta...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...
300元就能买到的”小钢炮”?惠普7L四盘位小主机解析

300元就能买到的”小钢炮”?惠普7L四盘位小主机解析

  300 元就能买到的 ” 小钢炮 ”?惠普 7L 四盘位小主机解析 最近...
4盘位、4K输出、J3455、遥控,NAS硬件入门性价比之王

4盘位、4K输出、J3455、遥控,NAS硬件入门性价比之王

  4 盘位、4K 输出、J3455、遥控,NAS 硬件入门性价比之王 开篇 在 NAS 市场中,威...
免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

  免费无广告!这款跨平台 AI RSS 阅读器,拯救你的信息焦虑 在算法推荐主导信息流的时代,我们...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
240 元左右!五盘位 NAS主机,7 代U硬解4K稳如狗,拓展性碾压同价位

240 元左右!五盘位 NAS主机,7 代U硬解4K稳如狗,拓展性碾压同价位

  240 元左右!五盘位 NAS 主机,7 代 U 硬解 4K 稳如狗,拓展性碾压同价位 在 NA...
安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装 Black 群晖 DSM7.2 系统安装教程(在 Vmware 虚拟机中、实体机均可)! 前言 大家好,...
星哥带你玩飞牛NAS-14:解锁公网自由!Lucky功能工具安装使用保姆级教程

星哥带你玩飞牛NAS-14:解锁公网自由!Lucky功能工具安装使用保姆级教程

星哥带你玩飞牛 NAS-14:解锁公网自由!Lucky 功能工具安装使用保姆级教程 作为 NAS 玩家,咱们最...
颠覆 AI 开发效率!开源工具一站式管控 30+大模型ApiKey,秘钥付费+负载均衡全搞定

颠覆 AI 开发效率!开源工具一站式管控 30+大模型ApiKey,秘钥付费+负载均衡全搞定

  颠覆 AI 开发效率!开源工具一站式管控 30+ 大模型 ApiKey,秘钥付费 + 负载均衡全...
多服务器管理神器 Nexterm 横空出世!NAS/Win/Linux 通吃,SSH/VNC/RDP 一站式搞定

多服务器管理神器 Nexterm 横空出世!NAS/Win/Linux 通吃,SSH/VNC/RDP 一站式搞定

多服务器管理神器 Nexterm 横空出世!NAS/Win/Linux 通吃,SSH/VNC/RDP 一站式搞...