说明:–add-dynamic-module指编译为动态模块,会生成一个.so动态库文件,如果不想编译为动态库的形式可以替换为–add-module
至此nginx-http-flv-module模块编译成功
至此,采用直接安装方式可以略过以下编译ffmpeg的步骤
lspci | grep -i nvidia
uname -m && cat /etc/*release
sudo apt-get install gcc g++ build-essential
sudo sh cuda_9.0.176_384.81_linux.run
说明:单击回车,一路往下运行,直到提示“是否为NVIDIA安装驱动nvidia-384?”,选择否,因为已经安装好驱动程序了,其他的全都是默认,不过要记住安装位置,默认是安装在/usr/local/cuda文件夹下。
sudo vim /etc/profile 打开文件后在文件末尾添加路径,也就是安装目录,命令如下:
export PATH=/usr/local/cuda-9.0/bin: P A T H e x p o r t L D L I B R A R Y P A T H = / u s r / l o c a l / c u d a − 9.0 / l i b 64 PATH export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64 PATHexportLDLIBRARYPATH=/usr/local/cuda−9.0/lib64LD_LIBRARY_PATH
保存,然后重启电脑
sudo reboot
cat /proc/driver/nvidia/version
nvcc -V
sudo ap-get update
sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev
sudo apt-get install yasm
sudo apt-get install libx264-dev
sudo apt-get install libx265-dev
sudo apt-get install libvpx-dev
sudo apt-get install libfdk-aac-dev
sudo apt-get install libmp3lame-dev
sudo apt-get install libopus-dev
sudo apt-get -y install glew-utils libglew-dbg libglew-dev libglew1.13 libglewmx-dev libglewmx-dbg freeglut3 freeglut3-dev freeglut3-dbg libghc-glut-dev libghc-glut-doc libghc-glut-prof libalut-dev libxmu-dev libxmu-headers libxmu6 libxmu6-dbg libxmuu-dev libxmuu1 libxmuu1-dbg
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make && sudo make install
mkdir -p ~/ffmpeg_sources ~/bin
cd ~/ffmpeg_sources && git -C aom pull 2> /dev/null || git clone --depth 1 https://aomedia.googlesource.com/aom && mkdir -p aom_build && cd aom_build && PATH=“ H O M E / b i n : HOME/bin: HOME/bin:PATH” cmake -G “Unix Makefiles” -DCMAKE_INSTALL_PREFIX=“KaTeX parse error: Expected 'EOF', got '&' at position 64: …NASM=on ../aom &̲& PATH="HOME/bin:$PATH” make && make install
cd ~/ffmpeg_sources && \
wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 && \
tar xjvf ffmpeg-snapshot.tar.bz2 && \
cd ffmpeg && \
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
--prefix="$HOME/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I$HOME/ffmpeg_build/include" \
--extra-ldflags="-L$HOME/ffmpeg_build/lib" \
--extra-libs="-lpthread -lm" \
--extra-cflags="-I/usr/local/cuda/include" \
--extra-ldflags="-L/usr/local/cuda/lib64" \
--bindir="$HOME/bin" \
--enable-gpl \
--enable-libaom \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-cuda \
--enable-cuvid \
--enable-nvenc \
--enable-libnpp \
--enable-nonfree && \
PATH="$HOME/bin:$PATH"
最后编译
make -j4 && sudo make install
至此,ffmpeg编译完成,如果不需要gpu加速,将编译参数 --enable-cuda --enable-cuvid --enable-nvenc去掉,其实哪个功能不需要或者编译的时候出错也可以去掉,具体问题具体操作。
cd /etc/nginx
sudo cp /usr/share/nginx/modules ./ (主要是找到ngx_http_flv_live_module.so放到/etc/nginx/modules目录下方便nginx.conf加载)
sudo vim nginx.conf
worker_processes 1; #运行在Windows上时,设置为1,因为Windows不支持Unix domain socket
#worker_processes auto; #1.3.8和1.2.5以及之后的版本
#worker_cpu_affinity 0001 0010 0100 1000; #只能用于FreeBSD和Linux
#worker_cpu_affinity auto; #1.9.10以及之后的版本
error_log logs/error.log error;
#如果此模块被编译为动态模块并且要使用与RTMP相关的功
#能时,必须指定下面的配置项并且它必须位于events配置
#项之前,否则NGINX启动时不会加载此模块或者加载失败
#load_module modules/ngx_http_flv_live_module.so;
events {
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80;
location / {
root /var/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /live {
flv_live on; #打开HTTP播放FLV直播流功能
chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回复
add_header 'Access-Control-Allow-Origin' '*'; #添加额外的HTTP头
add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的HTTP头
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /stat {
#push和pull状态的配置
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/rtmp; #指定stat.xsl的位置
}
#如果需要JSON风格的stat, 不用指定stat.xsl
#但是需要指定一个新的配置项rtmp_stat_format
#location /stat {
# rtmp_stat all;
# rtmp_stat_format json;
#}
location /control {
rtmp_control all; #rtmp控制模块的配置
}
}
}
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; #log模块在access.log中记录日志的间隔时间,对调试非常有用
log_size 1m; #log模块用来记录日志的缓冲区大小
server {
listen 1935;
server_name www.test.*; #用于虚拟主机名后缀通配
application myapp {
live on;
gop_cache on; #打开GOP缓存,减少首屏等待时间
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
server {
listen 1935;
server_name *.test.com; #用于虚拟主机名前缀通配
application myapp {
live on;
gop_cache on; #打开GOP缓存,减少首屏等待时间
}
}
server {
listen 1935;
server_name www.test.com; #用于虚拟主机名完全匹配
application myapp {
live on;
gop_cache on; #打开GOP缓存,减少首屏等待时间
}
}
}
说明:如果采用动态编译nginx-http-flv-module模块的方式,需要将load_module “modules/ngx_http_flv_live_module.so”;前面的#去掉
sudo nginx -c /etc/nginx/nginx.conf
ffmpeg -rtsp_transport tcp -i “rtsp://3.84.6.190/vod/mp4:BigBuckBunny_115k.mov” -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1920x1080 -q 10 -f flv “rtmp://192.168.30.205:1935/myapp/mystream”
git clone https://github.com/bilibili/flv.js
cd flv.js
npm install
测试代码: