Fork me on GitHub

ffmpeg视频流转发初探

ffmpeg视频流转发初探

  • 主机:CentOS 7.6

  • 前置工作

# 下载nginx
https://nginx.org/download/nginx-1.14.2.tar.gz
# 下载nginx-http-flv-module
git clone https://github.com/winshining/nginx-http-flv-module
  • 解压nginx
tar zxvf nginx-1.14.2.tar.gz 
  • nginx-http-flv-module在:/opt/stream/nginx-http-flv-module

  • 安装依赖

yum -y install unzip
yum -y install gcc-c++ 
yum -y install pcre pcre-devel  
yum -y install zlib zlib-devel 
yum -y install openssl openssl-devel
  • nginx编译安装
./configure --prefix=/usr/local/nginx  --add-module=/opt/stream/nginx-http-flv-module
make && make install
  • 修改nginx.conf
vi /usr/local/nginx/conf/nginx.conf
rtmp{
    server{
        listen 1935;
        application live{
            live on;
            record off;
            allow play all;
        }
        application hls{
            live on;
            hls on;
            # hls_path nginx-rtmp-module/hls;
            hls_path /tmp/hls;
            hls_cleanup off;
            hls_fragment 10s;
        	hls_playlist_length 3s;
        	record off;
        }
    }
}
server {
        listen 9999;
        server_name  test;
        location /live {
            flv_live on;
            chunked_transfer_encoding  on;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Origin' '*';
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            add_header 'Cache-Control' 'no-cache';
        }
    }

ffmpeg CentOS7 yum安装

ffmpeg CentOS7编译安装详解

  • 安装jdk
yum install libgdk*
yum install libgtk*
  • 安装依赖包
yum install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c++ git libtool make pkgconfig
yum install zlib zlib-devel curl curl-devel alsa-lib alsa-lib-devel gettext gettext-devel expat expat-devel gnutls gnutls-devel bzip2 bzip2-devel -y
  • 编译安装NASM
wget https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.xz
xz -d nasm-2.15.05.tar.xz
tar xvf nasm-2.15.05.tar
cd nasm-2.15.05
./configure --prefix=/usr
make
make install
  • 编译安装Yasm
wget https://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix=/usr
make
make install
  • 编译安装x264
wget https://anduin.linuxfromscratch.org/BLFS/x264/x264-20210211.tar.xz
xz -d x264-20210211.tar.xz
tar xvf x264-20210211.tar
cd x264-20210211
./configure --prefix=/usr/local/ffmpeg --enable-shared --disable-cli
make
make install
  • 编译安装x265
wget https://anduin.linuxfromscratch.org/BLFS/x265/x265_3.4.tar.gz
tar xzvf x265_3.4.tar.gz
cd x265_3.4
mkdir bld && cd bld
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/ffmpeg ../source
make
make install
rm -vf /usr/local/ffmpeg/lib/libx265.a
  • 编译安装fdk-aac
wget https://downloads.sourceforge.net/opencore-amr/fdk-aac-2.0.2.tar.gz
tar xzvf fdk-aac-2.0.2.tar.gz
cd fdk-aac-2.0.2
./configure --prefix=/usr/local/ffmpeg --disable-static
make
make install
  • 编译安装lame
wget https://downloads.sourceforge.net/lame/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix=/usr/local/ffmpeg --enable-mp3rtp --disable-static
make
make pkghtmldir=/usr/local/ffmpeg/share/doc/lame-3.100 install
  • 编译安装opus
wget https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
tar xzvf opus-1.3.1.tar.gz
cd opus-1.3.1
./configure --prefix=/usr/local/ffmpeg --disable-static --docdir=/usr/share/doc/opus-1.3.1
make
make install
  • 编译安装libvpx
wget https://github.com/webmproject/libvpx/archive/v1.10.0/libvpx-1.10.0.tar.gz
tar xzvf libvpx-1.10.0.tar.gz
cd libvpx-1.10.0
sed -i 's/cp -p/cp/' build/make/Makefile
mkdir libvpx-build && cd libvpx-build
../configure --prefix=/usr/local/ffmpeg --enable-shared --disable-static
make
make install
  • 安装ffmpeg
wget https://ffmpeg.org/releases/ffmpeg-4.4.tar.xz
xz -d ffmpeg-4.4.tar.xz
tar xvf ffmpeg-4.4.tar
cd ffmpeg-4.4
export PATH="/usr/local/ffmpeg/bin:$PATH"
export PKG_CONFIG_PATH="/usr/local/ffmpeg/lib/pkgconfig"
./configure --prefix=/usr/local/ffmpeg --pkg-config-flags="--static" --extra-cflags="-I/usr/local/ffmpeg/include" --extra-ldflags="-L/usr/local/ffmpeg/lib" --enable-gpl --enable-version3 --enable-nonfree --disable-static --enable-shared --disable-debug --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libx264 --enable-libx265 --enable-openssl
make
make install
hash -d ffmpeg
  • 配置环境变量
echo "/usr/local/ffmpeg/lib" >> /etc/ld.so.conf
ldconfig
echo 'export PATH=/usr/local/ffmpeg/bin:$PATH' >> /etc/profile
source /etc/profile

测试ffmpeg

  • 版本
ffmpeg -version
  • 测试rtsp转接rtmp
ffmpeg -rtsp_transport tcp  -i rtsp://admin:hh123456@192.168.110.64:554 -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 1920*1080 -f flv rtmp://192.168.110.183:1935/live/233

vlc打开:rtmp://192.168.110.183:1935/live/233

  • 测试rtsp转hls -> m3u8
ffmpeg -v verbose -i rtsp://admin:hh123456@192.168.110.64:554 -c:v libx264 -c:a aac -ac 1 -strict -2 -crf 18 -profile:v baseline -s 640x360 -maxrate 400k -bufsize 1835k -pix_fmt yuv420p -flags -global_header -hls_time 10 -hls_list_size 6 -hls_wrap 10 -start_number 1 /usr/local/nginx/html/hls/test.m3u8

参照文档:

posted @ 2022-02-15 15:30  是阿凯啊  阅读(438)  评论(0编辑  收藏  举报