ffmpeg實現直播


 

第一章 安裝ffmpeg

首先安裝h264 視頻編解碼庫:

 

sudo apt-get update

 

sudo apt-get install libx264-dev

 

安裝ffmpeg ,依次輸入以下命令:

 

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

 

sudo tar jxvf ffmpeg-3.0.9.tar.bz2

 

cd ffmpeg-3.0.9/

 

sudo ./configure --enable-shared --enable-pthreads --enable-gpl  --enable-avresample --enable-libx264 --enable-libtheora  --disable-yasm

 

sudo make   

 

sudo make install

 

第二章 安裝nginx

1、nginx.org 下載穩定版本的nginx

2、 https://github.com/arut/nginx-rtmp-module 下載rtmp模塊(git clone https://github.com/arut/nginx-rtmp-module.git)

3、解壓nginxtar包;nginx trmp模塊在同一目錄 nginx-1.12.2  nginx-1.12.2.tar.gz  nginx-rtmp-module

4、nginx解壓目錄配置編譯參數 ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module.1.1.4 --with-http_ssl_module 

5、make && make install 安裝

6、nginxconfig文件中添加以下配置

 

server {

    listen 8080;

        #配置RTMP狀態一覽HTTP頁面=========================================

        location /stat {

            rtmp_stat all;

            rtmp_stat_stylesheet stat.xsl;

        }

        location /stat.xsl {

            root /usr/local/nginx/nginx-rtmp-module/;

        }

        #配置RTMP狀態一覽界面結束==========================HTTP協議訪問直播流文件配置

        location /hls {  #添加視頻流存放地址。

                types {

                    application/vnd.apple.mpegurl m3u8;

                    video/mp2t ts;

                }

                #訪問權限開啟,否則訪問這個地址會報403

                autoindex on;

                alias /usr/share/nginx/html/hls;

                #視頻流存放地址,與下面的hls_path相對應,這里rootalias的區別可自行百度

                expires -1;

                add_header Cache-Control no-cache;

                #防止跨域問題

                add_header 'Access-Control-Allow-Origin' '*';

                add_header 'Access-Control-Allow-Credentials' 'true';

                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            }

  }

 

rtmp {

    server {

        listen 1935;

        chunk_size 4000;

        application vod {

            play /usr/share/nginx/html/vod/flvs/;

        }

        application live {

            live on;

        }

        application hls {

            live on;

            hls on;

            hls_path /usr/share/nginx/html/hls;

            hls_fragment 5s;

            hls_playlist_length 15s;

            hls_continuous on; #連續模式。

            hls_cleanup on;    #對多余的切片進行刪除。

            hls_nested on;     #嵌套模式。

        }

    }

}

 

其中rtmp就是rtmp服務器模塊,端口是1935application我理解為一個路徑。可以通過訪問rtmp://localhost/live來訪問live這個資源。live on 表示這是實時的傳輸,這不同於點播,點播就好比我在某視頻網站上想看一個視頻,無論我什么時候去點擊,它會從頭開始播放。而實時傳輸(直播),就是好比看電視,我在19:20去打開電視(打開直播路),視頻不會從頭開始播放,而是從當前(19:20)的視頻數據開始播放。

第三章 ffmpeg實現直播推流

1.推流MP4文件

視頻文件地址:/Desktop/bangbangbang.mp4

推流拉流地址:rtmp://localhost:1935/rtmplive/home

l accRTMP的音頻格式

l flv: RTMP的視頻格式

ffmpeg -re -i /Users/xu/Desktop/bangbangbang.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/rtmplive/home

2.本地拉流MP4文件

打開VCL播放器

 

 

 

l 設置播放地址

 

 

 

l 設置推流地址並播放

 

 

 

附錄

出現問題

問題1

ERROR: libtheora not found

 

 

 

 

If you think configure made a mistake, make sure you are using the latest

version from Git.  If the latest version fails, report the problem to the

ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.

Include the log file "ffbuild/config.log" produced by configure as this will help

solve the problem.

解決方案

sudo apt-get install libtheora-dev

 


免責聲明!

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



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