使用ffmpeg+nginx使用视频切片播放

服务器:CentOS7

安装依赖脚本

# Script ffmpeg compile for Centos 7.x
# Alvaro Bustos, thanks to Hunter.
# Updated 5-8-2018
# URL base https://trac.ffmpeg.org/wiki/CompilationGuide/Centos
# Install libraries
yum install -y autoconf automake bzip2 cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel x264-devel x265-devel

# Install yasm from repos

# Create a temporary directory for sources.
SOURCES=$(mkdir ~/ffmpeg_sources)
cd ~/ffmpeg_sources

# Download the necessary sources.
curl -O http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.02/nasm-2.13.02.tar.bz2
# git clone --depth 1 http://git.videolan.org/git/x264
wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20180720-2245.tar.bz2
wget https://bitbucket.org/multicoreware/x265/downloads/x265_2.8.tar.gz
#git clone --depth 1 https://github.com/mstorsjo/fdk-aac
wget https://downloads.sourceforge.net/opencore-amr/fdk-aac-0.1.6.tar.gz
curl -O -L http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
wget http://www.mirrorservice.org/sites/distfiles.macports.org/libopus/opus-1.2.1.tar.gz
wget https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.3.tar.gz
wget http://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz
curl -O -L https://ftp.osuosl.org/pub/xiph/releases/theora/libtheora-1.1.1.tar.gz
git clone --depth 1 https://github.com/webmproject/libvpx.git
wget http://ffmpeg.org/releases/ffmpeg-4.0.tar.gz

# Unpack files
for file in `ls ~/ffmpeg_sources/*.tar.*`; do
tar -xvf $file
done

cd nasm-*/
./autogen.sh 
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" 
make
make install 
cd ..

cp /root/bin/nasm /usr/bin

cd yasm-*/
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" && make && make install
cd ..

cp /root/bin/yasm /usr/bin

cd x264-*/
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static && make && make install
cd ..

cd /root/ffmpeg_sources/x265_2.8/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source && make && make install 
cd ~/ffmpeg_sources

cd fdk-aac-*/
autoreconf -fiv && ./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install
cd ..

cd lame-*/
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm && make && make install
cd ..

cd opus-*/
./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install
cd ..

cd libogg-*/
./configure --prefix="$HOME/ffmpeg_build" --disable-shared && make && make install
cd ..

cd libvorbis-*/
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared && make && make install
cd ..

cd libtheora-*/
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared && make && make install
cd ..

cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm && make && make install
cd ..

cd ffmpeg-*/
PATH="$HOME/bin:$PATH" 
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
./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 --extra-libs=-lm --bindir="$HOME/bin" --enable-gpl --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libtheora --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree && make && make install && hash -r 
cd ..

cd ~/bin
cp ffmpeg ffprobe lame x264 /usr/local/bin

cd /root/ffmpeg_build/bin
cp x265 /usr/local/bin

echo "FFmpeg Compilation is Finished!"

安装Nginx

1.环境准备

yum install –y pcre pcre-devel
yum install –y zlib zlib-devel

2.下载nginx及rtmp模块

wget  http://nginx.org/download/nginx-1.6.2.tar.gz

tar xzvf nginx_1.6.2.tar.gz

git clone git://github.com/arut/nginx-rtmp-module.git

3.编译nginx-rtmp

cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_stub_status_module
make
make install
安装完成后,nginx位于/usr/local/nginx/sbin目录下,配置文件nginx.conf在/usr/local/nginx/conf目录下

-------------测试nginx是否安装正确-----------------

cd /usr/local/nginx

./sbin/nginx -c ./conf/nginx.conf

打开网页http://localhost,如果显示Welcome表示安装下正确,如果没有显示,请查看一下nginx的日志。

4.在服务器上部署ckplayer等视频插件

下图是我的效果:
image.png

5.使用ffmpeg切割视频

ffmpeg -i /home/file/test.mp4 -strict -2 -c:v libx264 -c:a aac -f hls m3u8 /home/video/hls/test2.m3u8
(1). /home/file/test.mp4是原视频的路径
(2). /home/video/hls/test2.m3u8是切片后的视频地址

6.修改conf文件通过http协议发布切片后的视频

cd /usr/local/nginx/conf 切换到nginx的安转目录
vi ./nginx.conf

rtmp{
    server{
        listen  1935;
        application myapp{
            live    on;
        }
        application hls{
            live on;
            hls on;
        }
    }
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

    location /images/ {
            root   /usr/local/nginx/html;
            index  index.html index.htm;
        }

    location /hls {
        types{
        application/vnd.apple.mpegurl   m3u8;
        video/mp2t  ts;
        }
        root /root/video;
        add_header Cache-Control no-cache;
            add_header Access-Control-Allow-Origin *;
    }

这样就可以通过http://yourIP:8090//hls/test2.m3u8访问切片后的视频

image.png

你可能感兴趣的:(使用ffmpeg+nginx使用视频切片播放)