centos搭建hls系统

本文介绍如何利用HTTPLiveStreaming(HLS)技术搭建在线点播系统,包括使用Nginx提供HTTP服务并增加对HLS的支持,以及通过ffmpeg进行格式转换。
09
十一

基于HTTP Live Streaming(HLS) 搭建在线点播系统

1、 为何要使用HTTP Live Streaming

可以参考wikipedia

HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的基于HTTP的流媒体 网络传输协议。是苹果公司QuickTime X和iPhone软件系统的一部分。它的工作原理是把整个流分成一个个小的基于HTTP的文件来下载,每次只下载一些。当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速率下载同样的资源,允许流媒体会话适应不同的数据速率。在开始一个流媒体会话时,客户端会下载一个包含元数据的extended M3U (m3u8) playlist文件,用于寻找可用的媒体流。

 

HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防火墙或者代理服务器。它也很容易使用内容分发网络来传输媒体流。

2、 HTTP Live Streaming技术方案

HTTP服务:使用Nginx提供HTTP服务,通过使用nginx-rtmp-module https://github.com/arut/nginx-rtmp-module 来增加对HLS的支持

使用ffmpeg来完成对flv、mp4、mp3等格式的转化

使用ffmpeg或segmenter完成对视频/音频格式文件的切割,切割为m3u8格式及ts文件

3、 准备工作

操作系统CentOS

3.1、准备安装 删除已安装包

yum erase ffmpeg x264 x264-devel
 
  

3.2、安装各种依赖包

yum install  gcc make nasm pkgconfig wget curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64  pcre.i386 pcre.x86_64 pcre-devel.i386 pcre-devel.x86_64

 

3.3、安装git

wget -O git-devel.zip https://github.com/msysgit/git/archive/devel.zip

unzip git-devel.zip

cd git-devel/

autoconf

./configure

make

make install

3.4、创建安装包目录

mkdir ~/ffmpeg-source

 

4、 安装ffmpeg及其依赖包

4.1、Yasm

cd ~/ffmpeg-source

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

4.2、x264

cd ~/ffmpeg-source

git clone git://git.videolan.org/x264

cd x264

./configure –enable-shared

make

make install

4.3、LAME

cd ~/ffmpeg-source

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

4.4、libogg

cd ~/ffmpeg-source

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

4.5、libvorbis

cd ~/ffmpeg-source

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

4.6、libvpx

cd ~/ffmpeg-source

git clone http://git.chromium.org/webm/libvpx.git

cd libvpx

./configure  –enable-shared

make

make install

4.7、 FAAD2

cd ~/ffmpeg-source

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

4.8、FAAC

cd ~/ffmpeg-source

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

 

4.9、Xvid

cd ~/ffmpeg-source

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

 

4.10、FFmpeg

cd ~/ffmpeg-source

wget http://ffmpeg.org/releases/ffmpeg-0.11.2.tar.gz

tar zxvf  ffmpeg-0.11.2.tar.gz

cd ffmpeg-0.11.2

./configure  –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

ldconfig –v

 

2013.03备注:由于ffmpeg 1.0版本以后,将libavcodec/avcodec.h中的enum CodecID变更为enum AVCodecID,而nginx-rtmp-module仍然使用的enum CodecID,因此必须使用ffmpeg 1.0以下版本(例如0.11.2)

5、 安装nginx及nginx-rtmp-module

mkdir ~/nginx-source

cd  ~/nginx-source

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

tar zxvf nginx-1.2.4.tar.gz

wget -O nginx-rtmp-module.zip  https://github.com/arut/nginx-rtmp-module/archive/master.zip

unzip nginx-rtmp-module.zip

wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip

unzip ngx_cache_purge.zip

cd nginx-1.2.4

./configure –user=daemon –group=daemon –prefix=/usr/local/nginx-1.2.4/ –add-module=../nginx-rtmp-module-master –add-module=../nginx-rtmp-module-master/hls –add-module=../ngx_cache_purge-master  –with-http_stub_status_module –with-http_ssl_module –with-http_sub_module –with-md5=/usr/lib –with-sha1=/usr/lib –with-http_gzip_static_module

make

make install

 

在nginx.conf中增加rtmp模块的相关配置,配置例子

rtmp {

 

server {

 

listen 1935;

 

chunk_size 4000;

 

# TV mode: one publisher, many subscribers

application mytv {

 

# enable live streaming

live on;

 

# record first 1K of stream

record all;

record_path /tmp/av;

record_max_size 1K;

 

# append current timestamp to each flv

record_unique on;

 

# publish only from localhost

allow publish 127.0.0.1;

deny publish all;

 

#allow play all;

}

 

# Transcoding (ffmpeg needed)

application big {

live on;

 

# On every pusblished stream run this command (ffmpeg)

# with substitutions: $app/${app}, $name/${name} for application & stream name.

#

# This ffmpeg call receives stream from this application &

# reduces the resolution down to 32×32. The stream is the published to

# ‘small’ application (see below) under the same name.

#

# ffmpeg can do anything with the stream like video/audio

# transcoding, resizing, altering container/codec params etc

#

# Multiple exec lines can be specified.

 

exec /usr/local/bin/ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32×32 -f flv rtmp://localhost:1935/small/${name};

}

 

application small {

live on;

# Video with reduced resolution comes here from ffmpeg

}

 

application mypush {

live on;

 

# Every stream published here

# is automatically pushed to

# these two machines

#push rtmp1.example.com;

#push rtmp2.example.com:1934;

}

 

application mypull {

live on;

 

# Pull all streams from remote machine

# and play locally

#pull rtmp://rtmp3.example.com pageUrl=www.example.com/index.html;

}

 

# video on demand

application vod {

play /var/flvs;

}

 

application vod2 {

play /var/mp4s;

}

 

# Many publishers, many subscribers

# no checks, no recording

application videochat {

 

live on;

 

# The following notifications receive all

# the session variables as well as

# particular call arguments in HTTP POST

# request

 

# Make HTTP request & use HTTP retcode

# to decide whether to allow publishing

# from this connection or not

on_publish http://localhost:8080/publish;

 

# Same with playing

on_play http://localhost:8080/play;

 

# Publish/play end (repeats on disconnect)

on_done http://localhost:8080/done;

 

# All above mentioned notifications receive

# standard connect() arguments as well as

# play/publish ones. If any arguments are sent

# with GET-style syntax to play & publish

# these are also included.

# Example URL:

#   rtmp://localhost/myapp/mystream?a=b&c=d

 

# record 10 video keyframes (no audio) every 2 minutes

record keyframes;

record_path /var/vc;

record_max_frames 10;

record_interval 2m;

 

# Async notify about an flv recorded

on_record_done http://localhost:8080/record_done;

 

}

 

 

# HLS

 

# HLS requires libavformat & should be configured as a separate

# NGINX module in addition to nginx-rtmp-module:

# ./configure … –add-module=/path/to/nginx-rtmp-module/hls …

 

# For HLS to work please create a directory in tmpfs (/tmp/app here)

# for the fragments. The directory contents is served via HTTP (see

# http{} section in config)

#

# Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264

# profile (see ffmpeg example).

# This example creates RTMP stream from movie ready for HLS:

#

# ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264

#    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1

#    -f flv rtmp://localhost:1935/hls/movie

#

# If you need to transcode live stream use ‘exec’ feature.

#

application hls {

hls on;

hls_path /var/app;

hls_fragment 5s;

}

 

}

}

 

# HTTP can be used for accessing RTMP stats

http {

 

server {

 

listen      8080;

 

# This URL provides RTMP statistics in XML

location /stat {

rtmp_stat all;

 

# Use this stylesheet to view XML as web page

# in browser

rtmp_stat_stylesheet stat.xsl;

}

 

location /stat.xsl {

# XML stylesheet to view RTMP stats.

# Copy stat.xsl wherever you want

# and put the full directory path here

root /path/to/stat.xsl/;

}

 

location /hls {

# Serve HLS fragments

alias /var/app;

}

 

}

}

 

6、 安装segmenter

svn co http://httpsegmenter.googlecode.com/svn/

cd  svn/trunk

gcc -Wall -g segmenter.c -o segmenter -lavformat -lavcodec -lavutil -std=c99

cp segmenter /usr/bin/

 

 

从优酷上下载一个视频文件,假定为baluobu.flv

找个mp3文件,假定为10year.mp3

mkdir /var/flvs /var/mp4s /var/vc /var/app /var/app/10year /var/app/baluobu

使用ffmpeg将测试视频和音频转为mpeg ts格式文件

ffmpeg -i /var/flvs/baluobu.flv  -f mpegts -acodec libmp3lame -ar 48000 -ab 128k -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq ‘blurCplx^(1-qComp)’ -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 /var/app/baluobu/baluobu.ts

ffmpeg -i /var/flvs/10year.mp3  -f mpegts -acodec libmp3lame -ar 48000 -ab 128k -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq ‘blurCplx^(1-qComp)’ -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 /var/app/10year/10year.ts

cd /var/app/10year

segmenter -i 10year.ts -d 5 -o 10year -x 10year.m3u8

cd /var/app/baluobu

segmenter -i baluobu.ts -d 5 -o buluobu -x baluobu.m3u8

7、测试

简单起见使用VLC测试。

http://192.168.1.11:8080/hls/10year/10year.m3u8

http://192.168.1.11:8080/hls/baluobu/baluobu.m3u8

如果无问题,说明已经正常配置成功了HTTP Live Streaming(HLS)服务

可以再iOS设备上使用safari访问以上地址

或者在android机器上安装vplayer后访问以上地址

如果需要通过网页形式提供在线视频,需要在nginx的mime.types文件中添加如下MIME类型:

.m3u8 application/x-mpegURL

.ts video/MP2T

同时在HTML5页面中使用video标签包含视频的m3u8文件

 

<video controls>

<source src=http://192.168.1.11:8080/hls/baluobu/baluobu.m3u8 />

</video>

8、开发说明

iOS已经原生支持HTTP Live Streaming(HLS),只需要使用MPMoviePlayerController播放以上地址即可

apple文档:http://bit.ly/Rnpsef

Android 3.x以后通过新增的NuPlayer类才支持HTTP Live streaming,而且功能也较弱,为简化处理,可以使用

Vitamio     http://vov.io/vitamio/ 或 http://code.taobao.org/p/oplayer/src/ 下载

Servestream http://sourceforge.net/projects/servestream/

nginx-rtmp-module 本身也支持RTMP协议,从而也可以基于nginx来搭建在线直播系统。

<think> 我们正在讨论在CentOS 7上搭建MediaServer服务。根据引用[1],我们知道在CentOS 7.9上搭建mediasoup-demo环境需要特定的依赖版本,包括gcc 8.3.1、python3 3.8.12和node 14.19.3。虽然MediaServer的具体实现可能有所不同,但我们可以参考这个环境配置。 另外,引用[3]提供了在CentOS搭建RTMP服务器的步骤,包括安装nginx和nginx-rtmp-module。由于MediaServer也可能使用流媒体协议(如RTMP),因此这些步骤可能有一定的参考价值。 不过,用户的需求是搭建MediaServer服务,这可能有多种实现方式。例如,可以使用开源的媒体服务器如SRS、Nginx-RTMP、Mediasoup等。由于引用[1]提到了mediasoup-demo,我们将以搭建mediasoup-demo作为MediaServer的一个例子。 注意:mediasoup是一个优秀的WebRTC媒体服务器,因此我们选择它作为示例。 步骤概述: 1. 准备CentOS 7.9系统 2. 安装必要的依赖(包括升级gcc、安装python3和nodejs) 3. 安装mediasoup-demo 4. 配置和运行 具体步骤: 1. 系统更新与基础工具安装 ```bash sudo yum update -y sudo yum groupinstall "Development Tools" -y sudo yum install git -y ``` 2. 安装高版本GCC(CentOS7默认gcc版本为4.8,mediasoup需要更高版本) 参考引用[1],需要安装gcc 8.3.1。我们可以通过devtoolset-8来安装。 ```bash sudo yum install centos-release-scl -y sudo yum install devtoolset-8 -y # 启用devtoolset-8 scl enable devtoolset-8 bash # 使gcc 8成为默认的gcc(可选,如果需要永久生效,可以将source命令加入到~/.bashrc) echo "source /opt/rh/devtoolset-8/enable" >> ~/.bashrc source ~/.bashrc gcc --version # 确认版本 ``` 3. 安装Python3.8 CentOS7默认可能安装的是Python2.7,我们需要安装Python3.8。 ```bash sudo yum install epel-release -y sudo yum install https://repo.ius.io/ius-release-el7.rpm -y sudo yum install python38u python38u-devel python38u-pip -y # 创建软链接,将python3指向python3.8 sudo ln -s /usr/bin/python3.8 /usr/bin/python3 ``` 4. 安装Node.js 14.x 参考引用[1],需要node14.19.3。我们可以通过NodeSource仓库安装。 ```bash curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - sudo yum install nodejs -y node -v # 确认版本 ``` 5. 获取mediasoup-demo代码 ```bash git clone https://github.com/versatica/mediasoup-demo.git cd mediasoup-demo git checkout v3 ``` 6. 安装依赖并构建 ```bash npm install # 进入server目录,安装服务器依赖 cd server npm install # 回到项目根目录,启动(注意:启动命令在server目录下运行) # 但根据项目结构,我们需要先配置 ``` 7. 配置mediasoup-demo 复制配置文件并修改: ```bash cd server cp config.example.js config.js ``` 根据需求修改`config.js`,主要是修改监听的IP地址(如果是公网需要修改为0.0.0.0)和域名设置。 8. 运行 ```bash cd server npm start ``` 注意:mediasoup-demo包含一个服务器(server)和一个客户端(app)。客户端需要另外启动(在app目录下): ```bash cd ../app npm install npm start ``` 9. 防火墙设置 需要打开端口(例如TCP:3000(客户端)、TCP:3001(服务器)、UDP:40000-49999(RTP)等): ```bash sudo firewall-cmd --add-port=3000/tcp --permanent sudo firewall-cmd --add-port=3001/tcp --permanent sudo firewall-cmd --add-port=40000-49999/udp --permanent sudo firewall-cmd --reload ``` 10. 测试 访问客户端:http://服务器IP:3000 注意:以上步骤是搭建mediasoup-demo作为媒体服务器的基本流程。实际使用中可能还需要配置TLS证书(HTTPS)和域名等。 除了mediasoup,还有其他媒体服务器方案,如使用SRS(Simple Realtime Server)或Nginx-RTMP(用于RTMP/HLS)等。如果用户的需求是RTMP服务器,可以参考引用[3]搭建Nginx with RTMP module。 如果用户需要搭建RTMP服务器(引用[3]的步骤): 1. 安装依赖 ```bash sudo yum install -y gcc-c++ pcre-devel openssl-devel zlib-devel ``` 2. 下载nginx和nginx-rtmp-module ```bash wget http://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz git clone https://github.com/arut/nginx-rtmp-module.git ``` 3. 编译安装nginx并添加rtmp模块 ```bash cd nginx-1.18.0 ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module make sudo make install ``` 4. 配置nginx 编辑`/usr/local/nginx/conf/nginx.conf`,添加rtmp配置: ```conf rtmp { server { listen 1935; application live { live on; record off; } } } ``` 5. 启动nginx ```bash sudo /usr/local/nginx/sbin/nginx ``` 以上提供了两种搭建媒体服务器的方法:一种是基于WebRTC的mediasoup,另一种是基于RTMP的nginx-rtmp。 请用户根据实际需求选择方案。 §§相关问题§§ 1. 在CentOS 7上搭建媒体服务器有哪些常见问题? 2. 如何配置mediasoup-demo以使用HTTPS? 3. 除了mediasoup和Nginx-RTMP,还有哪些开源的媒体服务器? </think> ### CentOS 7 系统搭建 MediaServer 服务的方法 在 CentOS 7 上搭建 MediaServer 服务(以 mediasoup-demo 为例),需解决依赖版本兼容性问题。以下是已验证的完整步骤: #### 1. 系统准备 ```bash sudo yum update -y sudo yum install epel-release -y sudo yum groupinstall "Development Tools" -y ``` #### 2. 安装高版本 GCC(关键步骤) CentOS 7 默认 GCC 4.8 不兼容,需升级至 8.3.1: ```bash sudo yum install centos-release-scl -y sudo yum install devtoolset-8-gcc* -y scl enable devtoolset-8 bash echo "source /opt/rh/devtoolset-8/enable" >> ~/.bashrc # 永久生效 ``` #### 3. 安装 Python 3.8 ```bash sudo yum install https://repo.ius.io/ius-release-el7.rpm -y sudo yum install python38u python38u-devel python38u-pip -y sudo ln -s /usr/bin/python3.8 /usr/bin/python3 ``` #### 4. 安装 Node.js 14.x ```bash curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - sudo yum install nodejs -y ``` #### 5. 部署 mediasoup-demo ```bash git clone https://github.com/versatica/mediasoup-demo.git cd mediasoup-demo npm install # 配置服务端 cd server cp config.example.js config.js nano config.js # 修改监听IP(0.0.0.0)和域名 npm start ``` #### 6. 防火墙配置 ```bash sudo firewall-cmd --add-port={3000,3001}/tcp --permanent # HTTP/信令端口 sudo firewall-cmd --add-port=40000-49999/udp --permanent # RTP媒体端口 sudo firewall-cmd --reload ``` #### 7. 验证服务 访问客户端: `http://<服务器IP>:3000` 成功显示视频房间界面即部署完成[^1]。 > **避坑提示**: > - 必须严格匹配 GCC 8.3+/Python 3.8+/Node 14.x 版本组合 > - 若启动报 GLIBCXX 错误,执行 `strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX` 验证 GCC 版本 > - 媒体端口范围需在防火墙开放 UDP 协议 #### 备选方案:Nginx-RTMP 媒体服务器 若需传统 RTMP 服务(如直播推流),可使用: ```bash # 编译支持 RTMP 的 Nginx git clone https://github.com/arut/nginx-rtmp-module.git wget http://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz cd nginx-1.18.0 ./configure --add-module=../nginx-rtmp-module make && sudo make install ``` 配置 RTMP 推流/拉流(配置文件路径:`/usr/local/nginx/conf/nginx.conf`)[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值