プロジェクトでストリームをトランスコードしてプッシュし、flv.jsを使用してWebページでビデオ再生機能を実現し、関連する環境構築などをここに記録する必要がありました。
一、ffmpeg 安装
注意:ffmpegをインストールするときは、関連する依存関係をインストールするだけでなく、x264を省略しないように注意してください。そうしないと、libx264を使用してストリームをトランスコードおよびプッシュするときにエラーが発生します。
1.安装基础工具(如果已有可以忽略)
1. yum install lrzsz -y2. yum -y install gcc automake autoconf libtool make gcc gcc-c++ bzip2 xz3. yum install unzip -y4. yum install gcc-c++ -y5. yum install pcre pcre-devel -y6. yum install zlib zlib-devel -y7. yum install openssl openssl-devel -y
2.安装nasm和yasm( 相关安装方式可以百度,此处提供安装参考)
如果没有 wget 命令,则执行 yum install wget1.安装yasmmkdir 一个新的文件夹 yasm,然后 cd 到该目录下按顺序依次执行如下命令:wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gztar -zxvf yasm-1.3.0.tar.gzcd yasm-1.3.0./configuremake && make install2.安装nasmmkdir 一个新的文件夹 nasm,然后 cd 到该目录下按顺序依次执行如下命令:wget http://www.nasm.us/pub/nasm/releasebuilds/2.13/nasm-2.13.tar.gztar -zxvf yasm-1.3.0.tar.gzcd yasm-1.3.0./configuremake && make install
3.安装x264
1.安装git yum install git (若不使用git方式,可以到 http://www.videolan.org/developers/x264.html 下载安装包)2.mkdir 一个新的文件夹 x2643.cd到这个新的文件夹执行 git clone https://code.videolan.org/videolan/x264.git4.cd x264 执行 ./configure --enable-shared (可能出现错误提示,根据提示加上对应内容即可,如可能需要变成 ./configure --enable-shared --disable-asm)5.make && make install
4.编译,安装ffmpeg
1.在官网 http://ffmpeg.org/download.html 下载最新的ffmpeg安装包ffmpeg-4.2.1.tar.bz2(或者其他途径也行,但是先别编译安装)2.在/usr/local/ 下新建一个目录为 ffmpeg,并将安装包放置这个目录下3.解压ffmpeg至当前目录后进入根目录4.执行 ./configure --enable-gpl --enable-libx264 (注意此处加上 x264,可能很多ffmpeg安装教程没有加上)5.make && make install6.检查是否安装成功 ffmpeg -version7.如果上诉命令报错,则检查下系统配置文件,加上lib库文件的路径(可根据提示先找到需要的文件在哪里,一般是libx264.so文件)vi /etc/ld.so.conf.d/user.conf文件末尾加上 /usr/local/lib (此处仅表示可能的路径,实际路径需要根据安装路径来找)
二、nginx-http-flv-module 安装
1.新建一个nginx文件夹
2.cd nginx、module フォルダーを作成して、cd module //nginx-http-flv-module-1.2.2.tar.gz ?
git clone https://github.com/winshining/nginx-http-flv-module にアップロードします。
3.解压 tar -zxvf nginx-http-flv-module-1.2.2.tar.gz 并解压后目录名称改为 nginx-http-flv-module方便后续操作
三、nginx 安装
1.cd nginx //新しく作成した ディレクトリに移動します
2.执行 wget https://nginx.org/download/nginx-1.20.1.tar.gz
3.执行 tar -zxvf nginx-1.20.1.tar.gz //展開後 nginx-1.20.1をnginx-videoに変更
4.执行 cd nginx-video
5.执行 ./configure --add-module=/usr/local/nginx/module/nginx-http-flv-module //flvモジュールのパスを指定
6.make && make install
7.構成ファイルを変更します
#user nobody;worker_processes 5;events {worker_connections 1024;}rtmp_auto_push on;rtmp_auto_push_reconnect 1s;rtmp_socket_dir /tmp;rtmp {out_queue 4096;out_cork 8;max_streams 128;timeout 15s;drop_idle_publisher 15s;log_interval 5s; #interval used by log module to log in access.log, it is very useful for debuglog_size 1m; #buffer size used by log module to log in access.logserver {listen 1935;server_name localhost; #for suffix wildcard matching of virtual host nameapplication http_flv {live on;gop_cache on; #open GOP cache for reducing the wating time for the first picture of video}application hls {live on;hls on;hls_path /tmp/hls;hls_playlist_length 4s;hls_fragment 1s;}application dash {live on;dash on;dash_path /tmp/dash;}}}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 9080; #//此处对应推流使用端口server_name localhost;location / {root html;index index.html index.htm;}location /live { #//此处表示推流后访问入口 http://ip:port/live?xxxxxxflv_live on; #open flv live streaming (subscribe)chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' responseadd_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP headeradd_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header}location /hls {types {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root /tmp;add_header 'Cache-Control' 'no-cache';add_header Access-Control-Allow-Origin *;}location /dash {root /tmp;add_header 'Cache-Control' 'no-cache';}location /stat {#推流播放和录制统计数据的配置rtmp_stat all;rtmp_stat_stylesheet stat.xsl;}location /stat.xsl {root /usr/local/nginx/module/nginx-http-flv-module/; #指定stat.xsl的位置,需要根据实际修改}#如果需要JSON风格的stat, 不用指定stat.xsl#但是需要指定一个新的配置项rtmp_stat_format#location /stat {# rtmp_stat all;# rtmp_stat_format json;#}location /control {rtmp_control all; #rtmp控制模块的配置}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
8.启动nginx
注意:此处需要指定配置文件启动,cd 到nginx 的 sbin 目录 执行 ./nginx -c xxx/xx/nginx.conf 具体写配置文件的路径
四、プッシュストリーミングスクリプト
プッシュストリームコマンド:
ffmpeg -re -rtsp_transport tcp -i rtsp://admin:12345@192.168.xx.xx/ch33/sub/av_stream -vcodec libx264 -an -f flv -s 960×540 rtmp://192.168.xx.xx:9080/http_flv/tower
注意:
1.ここで、9080は、上記のnginx構成ファイルで構成されたポートを表します
2.http_flvはアプリを意味します
3.towerはstreamを意味します
flvプルストリームアドレス:
http://192.168.xx.xx/live?port=8580&app=http_flv&stream=tower2
五、测试
VLCプレーヤーを使用して、テストし、ネットワークストリームを開き、アピールアドレスを入力します。