FFmpeg 安装及使用和 RTMP 推流

359 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

FFmpeg

安装 FFmpeg

安装ffmpeg

#安装epel包
yum install -y epel-release 
#导入签名 
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 
#导入签名 
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro 
#升级软件包 
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm 
#更新软件包 
yum update -y 
#安装ffmpeg 
yum install -y ffmpeg ffmpeg-devel
#检查版本 
ffmpeg -version

FFmpeg 实现视频分片及播放

转码分片、截取封面

ffmpeg -i video.mp4 -c:v libx264 -hls_time 120 -hls_list_size 0 -c:a aac -strict -2 -f hls /play/video.m3u8 
ffmpeg -i video.mp4 -y -f mjpeg -ss 3 -t 1 -s 900x500 /play/video.jpg 

nginx设置

server节点下添加

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

访问视频

http://192.168.23.241/play/video.m3u8

Centos7 搭建 RTMP 流媒体服务

下载nginx-rtmp-module模块

wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip -o master.zip

安装nginx

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
wget http://nginx.org/download/nginx-1.19.6.tar.gz
tar -zxvf nginx-1.19.6.tar.gz -C /usr/local/
cd /usr/local/nginx-1.19.6
# 配置nginx增加上面下载的模块
./configure --prefix=/usr/local/nginx --add-module=/root/nginx-rtmp-module
# 编译、安装
make
make install 

修改nginx配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

# rtmp配置
rtmp{
    server{
        listen 1935;
        application hls{
            live on;
            hls on;
            hls_path /tmp/hls;
            # 不清除旧的ts文件
            hls_cleanup off;
            # 从头开始播放
            hls_type event;
            # 限制播放列表时长:避免m3u8文件序列没有从0开始
            hls_playlist_length 100m;
        }
    }
}

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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    
        location /hls {
            # 跨域
            add_header Access-Control-Allow-Origin *;
            types{
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

启动nginx

/usr/local/nginx/sbin/nginx

测试

ffmpeg -re -i testVideo.mp4 -vcodec copy -codec copy -f flv rtmp://192.168.247.130/hls/testVideo

访问视频

http://192.168.247.130/hls/testVideo.m3u8

参考资料

centos7+nginx+rtmp+ffmpeg搭建流媒体服务器

如何在CentOS 7上安装和使用FFmpeg

CentOS 7中搭建基础的Nginx RMPT服务器

Nginx RTMP服务器配置