Ubuntu 14.10下基於Nginx搭建mp4/flv流媒體服務器(可隨意拖動)並支持RTMP/HLS協議(含轉碼工具)


Ubuntu 14.10下基於Nginx搭建mp4/flv流媒體服務器(可隨意拖動)並支持RTMP/HLS協議(含轉碼工具)

最近因為項目關系,收朋友之托,想制作秀場網站,但是因為之前一直沒有涉及到這方面的東西,沒有什么感覺可言;搜索了一些資料以后,雖然有開業的實現方式,但是大家都沒有完成的說明的,或者說我還沒有更深入的研究到。不過讓我找到了關於假設流媒體播放器的資料,不過是基於centos的操作,那我就試做成ubuntu的step by step吧。

步驟如下:

step1准備ubuntu的環境:

sudo apt-get update

期間遇到了一些ubuntu的問題,總之就是倒騰ubuntu的依賴包。
一些指導:

sudo apt-get -f dist-upgrade
sudo apt-get update
sudo apt-get install -f
apt-cache depends linux-{image,headers}-generic
apt-cache rdepend  linux-image-3.16.0-41-generic:i386

do-release-upgrade 又提示 ubuntu boot空間不足
然后就是手動的mv 一個 文件到其他地方,騰出boot的空間,然后重新啟動reboot,然后再次 apt-get update 就可以了

step2 安裝必要的一些libs:

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

sudo apt-get install gcc make automake bzip2 unzip patch subversion libjpeg62-dev

我使用的是root用戶
cd ~
mkdir working
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar zxvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make && make install
cd ~/working

step3 卸載系統原有的ffmgeg和x.264,沒有則跳過:

apt-get remove ffmpeg x264
step4 先安裝一些Mplayer編碼庫(僅限於64位系統):
wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2

tar xvjf essential-amd64-20071007.tar.bz2
mkdir /usr/local/lib/codecs
cp -Rvp essential-amd64-20071007/* /usr/local/lib/codecs/
編輯下面文件
vim /etc/ld.so.conf
添加以下兩行到上面的文件里
 /usr/lib 
 /usr/local/lib

step5 再安裝一些格式轉換常用的編碼庫:

sudo apt-get install amrnb opencore-amr amrwb libvorbis libtheora xvidcore
sudo apt-get install build-essential git-core checkinstall texi2html libfaac-dev
libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev
libvorbis-dev libx11-dev libxfixes-dev zlib1g-dev libxvidcore-dev
圖片

setp6 安裝x.264:

wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/last_stable_x264.tar.bz2

tar xvjf last_stable_x264.tar.bz2
cd x264-snapshot-20150803-2245-stable/
./configure –enable-shared –enable-pic
make && make install
cd ~/working

setp7 安裝libvpx:

wget http://webm.googlecode.com/files/libvpx-v1.4.0.tar.bz2

tar xvjf libvpx-v1.4.0.tar.bz2
cd libvpx-v1.4.0
./configure –enable-shared –enable-pic
make && make install
cd ~/working

step8 安裝FFmpeg

wget http://ffmpeg.org/releases/ffmpeg-2.7.2.tar.bz2

tar xvjf ffmpeg-2.7.2.tar.bz2
cd ffmpeg-2.7.2
 ./configure --enable-gpl --enable-version3 --enable-shared --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid
 make && make install
 cd ~/working
 讓動態鏈接庫被系統共享
 ldconfig

step9 安裝mplayer and mencoder

apt-get install libmp3lame-dev 
svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer (使用最新的代碼可以編譯安裝,否則使用http://www.mplayerhq.hu/MPlayer/releases/mplayer-checkout-snapshot.tar.bz2 的話出錯,)
cd mplayer/
./configure
make && make install

step 10 安裝flvtool2

sudo apt-get install ruby
gem -v
gem souce -l
https://rubygems.org/
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l
因為網絡的原因,切換為淘寶的服務器
ll /usr/local/bin/flvtool2

step11 查看一下已經安裝好的音頻和視頻編碼器

查看所有所支持的音頻編碼

 mencoder -oac help

查看所有所支持的視頻編碼
mencoder -ovc help

具體結果可參考

setp12 安裝配置Nginx:

先安裝各種依賴(nginx需要pcre支持,yamdi用來為flv創建關鍵幀才能隨意拖動)

sudo apt-get install gcc g++ libssl-dev zlib1g-dev libpcre3-dev yamdi

下載所需的nginx模塊

第一個是nginx_mod_h264_streaming,讓nginx支持flv/mp4流播放

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

注意:先要修改一下這家伙的源碼,注釋掉nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c的158到161行
/* TODO: Win32 */
//if (r->zero_in_uri)
// {
// return NGX_DECLINED;
// }

第二個是nginx-rtmp-module,讓nginx支持rtmp/hls協議

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

下載安裝nginx:

wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --user=daemon --group=daemon --prefix=/usr/local/nginx/ --add-module=../nginx-rtmp-module-master --add-module=../ngx_cache_purge-master --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_flv_module
make && make install

編譯過程中出錯:
make[1]: *** [objs/addon/src/mp4_reader.o] 錯誤 1

解決方法:
vim objs/Makefile (修改objs/Makefile文件, 去掉其中的"-Werror"), 然后就能夠正常編譯了.

step13 設置nginx為系統服務

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
sudo service nginx start
sudo service nginx stop

然后用瀏覽器你的服務器IP,看到welcome就對了
或者到nginx的sbin目錄下,運行一下nginx -V,看看列表出來的modules對不對

step14 各種配置nginx:
編輯/usr/local/nginx/conf/nginx.conf文件,最好用sftp軟件(如windows下的flashfxp/Mac下的tramnsmit)下載過來本地編輯。

#filename:nginx.conf
#user  nobody;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        # video on demand
        application vod {
            play /mnt/media/vod;
        }

        # 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 /mnt/media/app;
            hls_fragment 10s;
        }
    }
}

http {

    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    gzip on;
    
     #log format

    log_format  access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
             ‘$status $body_bytes_sent “$http_referer” ‘
             ‘”$http_user_agent” $http_x_forwarded_for’;    
    
     #定義一個名為addr的limit_zone,大小10M內存來存儲session
    limit_conn_zone $binary_remote_addr zone=addr:10m;     

    server {
        listen 8080;
         server_name localhost;

         # HTTP can be used for accessing RTMP stats
        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root /mnt/soft/nginx-rtmp-module-master;
        }
         location /control {
            rtmp_control all;
        }
        location / {
            root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;
        }
    }
    
     server {
        listen 80;
         server_name localhost;
         
        location / {
                root /mnt/wwwroot;
                index index.html;
              }         

         location ~ \.flv$ {
               root /mnt/media/vod;
             flv;
             limit_conn addr 20;
             limit_rate 200k;
        }
        location ~ \.mp4$ {
             root /mnt/media/video;
             mp4;
             limit_conn addr 20;
             limit_rate 200k;
        }

         location /hls {
            # Serve HLS fragments
            alias /mnt/media/app;
        }

         access_log  logs/nginxflv_access.log access;
    }
    
       
}

step14 把自己的電影轉換成mp4和flv格式來測試nginx搭的環境:

准備兩部電影,硬盤上隨便找,我找了“諜影重重A.mp4”和“鹿鼎記033.rmvb”,盡量找小一點十來分鍾的,等下我們還要看完測試一下轉換的結果有沒有音影不同步的情況。
 我把兩部電影重命名為 movie1.mp4和 movie2.rmvb,並上傳到服務器/mnt/media/video下面,這里目錄用來存放我們的原始視頻。還有一個目錄是/mnt/media/vod 用來存放轉換后的視頻。
 我這里的具體目錄結構為:
 /mnt/media/video -> 存放原始視頻
 /mnt/media/app  -> 存放轉成m3u8的視頻,供http訪問(HLS)
 /mnt/media/vod  -> 存放轉換后的flv和mp4視頻,供http或rtmp訪問

用ffmpeg轉換mp4文件(ffmpeg不支持rmvb)

cd /mnt/media/video/
ffmpeg -y -i movie1.mp4 -vcodec copy -acodec copy ../vod/movie1.flv
cd ../vod
mv movie1.flv movie1-src.flv
yamdi -i movie1-src.flv -o movie1.flv
rm -rf movie1-src.flv

step15 安裝flv web播放器:

ckplayer播放器安裝簡單。就拿這個做實例吧。

cd /mnt
mkidr wwwroot

下載 ckplayer6.7
解壓,將所以文件上傳到wwwroot下面

step16 制作頁面播放流媒體:
cd /mnt/wwwroot
touch a.html
vi a.html
輸入以下內容:

<div id="a1"></div>
<script type="text/javascript" src="/ckplayer/ckplayer.js" charset="utf-8"></script>
<script type="text/javascript">
var flashvars={
    f:'http://你自己的ip地址/movie1.flv',
    c:0
};
var params={bgcolor:'#FFF',allowFullScreen:true,allowScriptAccess:'always',wmode:'transparent'};
var video=['http://你自己的ip地址/movie1.mp4->video/mp4'];
CKobject.embed('/ckplayer/ckplayer.swf','a1','ckplayer_a1','100%','100%',true,flashvars,video,params);
</script>

具體配置的含義,可以參考再見配置工作,這里

step 17 結束:

歡迎各位批評指正!
如有問題,歡迎與我聯系!

參考頁面:

https://www.vultr.com/docs/setup-nginx-rtmp-on-ubuntu-14-04
https://www.vimp.com/en/web/faq-installation/items/how-to-install-the-transcoding-tools-on-ubuntu-1404.html
http://hdu104.com/294
baidu.com
google.com
等等.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM