ffmpeg + nginx + nginx-http-flv-module トランスコーディングとストリーミングを実装するための関連する手順 — centos7

プロジェクトでストリームをトランスコードしてプッシュし、flv.jsを使用してWebページでビデオ再生機能を実現し、関連する環境構築などをここに記録する必要がありました。

一、ffmpeg 安装

注意:ffmpegをインストールするときは、関連する依存関係をインストールするだけでなく、x264を省略しないように注意してください。そうしないと、libx264を使用してストリームをトランスコードおよびプッシュするときにエラーが発生します。

1.安装基础工具(如果已有可以忽略)

  1. 1. yum install lrzsz -y
  2. 2. yum -y install gcc automake autoconf libtool make gcc gcc-c++ bzip2 xz
  3. 3. yum install unzip -y
  4. 4. yum install gcc-c++ -y
  5. 5. yum install pcre pcre-devel -y
  6. 6. yum install zlib zlib-devel -y
  7. 7. yum install openssl openssl-devel -y

2.安装nasm和yasm( 相关安装方式可以百度,此处提供安装参考)

  1. 如果没有 wget 命令,则执行 yum install wget
  2. 1.安装yasm
  3. mkdir 一个新的文件夹 yasm,然后 cd 到该目录下按顺序依次执行如下命令:
  4. wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
  5. tar -zxvf yasm-1.3.0.tar.gz
  6. cd yasm-1.3.0
  7. ./configure
  8. make && make install
  9. 2.安装nasm
  10. mkdir 一个新的文件夹 nasm,然后 cd 到该目录下按顺序依次执行如下命令:
  11. wget http://www.nasm.us/pub/nasm/releasebuilds/2.13/nasm-2.13.tar.gz
  12. tar -zxvf yasm-1.3.0.tar.gz
  13. cd yasm-1.3.0
  14. ./configure
  15. make && make install

3.安装x264

  1. 1.安装git yum install git (若不使用git方式,可以到 http://www.videolan.org/developers/x264.html 下载安装包)
  2. 2.mkdir 一个新的文件夹 x264
  3. 3.cd到这个新的文件夹执行 git clone https://code.videolan.org/videolan/x264.git
  4. 4.cd x264 执行 ./configure --enable-shared (可能出现错误提示,根据提示加上对应内容即可,
  5. 如可能需要变成 ./configure --enable-shared --disable-asm
  6. 5.make && make install

4.编译,安装ffmpeg

  1. 1.在官网 http://ffmpeg.org/download.html 下载最新的ffmpeg安装包ffmpeg-4.2.1.tar.bz2(或者其他途径也行,但是先别编译安装)
  2. 2.在/usr/local/ 下新建一个目录为 ffmpeg,并将安装包放置这个目录下
  3. 3.解压ffmpeg至当前目录后进入根目录
  4. 4.执行 ./configure --enable-gpl --enable-libx264 (注意此处加上 x264,可能很多ffmpeg安装教程没有加上)
  5. 5.make && make install
  6. 6.检查是否安装成功 ffmpeg -version
  7. 7.如果上诉命令报错,则检查下系统配置文件,加上lib库文件的路径(可根据提示先找到需要的文件在哪里,一般是libx264.so文件)
  8. vi /etc/ld.so.conf.d/user.conf
  9. 文件末尾加上 /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.構成ファイルを変更します

  1. #user nobody;
  2. worker_processes 5;
  3. events {
  4. worker_connections 1024;
  5. }
  6. rtmp_auto_push on;
  7. rtmp_auto_push_reconnect 1s;
  8. rtmp_socket_dir /tmp;
  9. rtmp {
  10. out_queue 4096;
  11. out_cork 8;
  12. max_streams 128;
  13. timeout 15s;
  14. drop_idle_publisher 15s;
  15. log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
  16. log_size 1m; #buffer size used by log module to log in access.log
  17. server {
  18. listen 1935;
  19. server_name localhost; #for suffix wildcard matching of virtual host name
  20. application http_flv {
  21. live on;
  22. gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
  23. }
  24. application hls {
  25. live on;
  26. hls on;
  27. hls_path /tmp/hls;
  28. hls_playlist_length 4s;
  29. hls_fragment 1s;
  30. }
  31. application dash {
  32. live on;
  33. dash on;
  34. dash_path /tmp/dash;
  35. }
  36. }
  37. }
  38. http {
  39. include mime.types;
  40. default_type application/octet-stream;
  41. sendfile on;
  42. keepalive_timeout 65;
  43. server {
  44. listen 9080; #//此处对应推流使用端口
  45. server_name localhost;
  46. location / {
  47. root html;
  48. index index.html index.htm;
  49. }
  50. location /live { #//此处表示推流后访问入口 http://ip:port/live?xxxxxx
  51. flv_live on; #open flv live streaming (subscribe)
  52. chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
  53. add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
  54. add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
  55. }
  56. location /hls {
  57. types {
  58. application/vnd.apple.mpegurl m3u8;
  59. video/mp2t ts;
  60. }
  61. root /tmp;
  62. add_header 'Cache-Control' 'no-cache';
  63. add_header Access-Control-Allow-Origin *;
  64. }
  65. location /dash {
  66. root /tmp;
  67. add_header 'Cache-Control' 'no-cache';
  68. }
  69. location /stat {
  70. #推流播放和录制统计数据的配置
  71. rtmp_stat all;
  72. rtmp_stat_stylesheet stat.xsl;
  73. }
  74. location /stat.xsl {
  75. root /usr/local/nginx/module/nginx-http-flv-module/; #指定stat.xsl的位置,需要根据实际修改
  76. }
  77. #如果需要JSON风格的stat, 不用指定stat.xsl
  78. #但是需要指定一个新的配置项rtmp_stat_format
  79. #location /stat {
  80. # rtmp_stat all;
  81. # rtmp_stat_format json;
  82. #}
  83. location /control {
  84. rtmp_control all; #rtmp控制模块的配置
  85. }
  86. error_page 500 502 503 504 /50x.html;
  87. location = /50x.html {
  88. root html;
  89. }
  90. }
  91. }

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

五、测试

  1. VLCプレーヤーを使用して、テストし、ネットワークストリームを開き、アピールアドレスを入力します。