ffmpeg 低延迟高性能推流方案

以下是 ffmpeg 低延迟高性能推流方案的笔记,不是系统性的文章,Windows 环境,不过其他系统也大同小异,关键点都在下面了,希望对大家有帮助。

  • ffmpeg 关键命令
-c copy 的方式 CPU 占用低,但要求 RTSP 视频源为 H264,否则大部分浏览器不兼容。如果不用 -c copy 则会进行转码,此时必须使用子码流输入,否则CPU占用率高。

如果一定要使用 -c copy,则可以将子码流配置成 H264,作为输入源。


# H5最佳方案:播放速度快、H5可以播放、CPU占用低,用子码流(H265/H264都可以)
# 如果 -hls_wrap 无法使用,可换成 -hls_flags
ffmpeg -rtsp_transport tcp -re -i "rtsp://admin:[email protected]:554/Streaming/Channels/102" -f hls -crf 23 -tag:v hvc1 -preset ultrafast -maxrate 1M -bufsize 300k -r 10 -g 15 -movflags +faststart -tune zerolatency -hls_time 1 -hls_list_size 5 -hls_wrap 6 -start_number 1 -hls_allow_cache 0 -threads 1 -loglevel warning -y -an  "C:\demo-service\demo-nginx\html\hls\123.22.m3u8"


# 播放速度最快、CPU占用最低,但如视频源不是265则H5无法播放
# 如果 -hls_wrap 无法使用,可换成 -hls_flags
ffmpeg -rtsp_transport tcp -re -i "rtsp://admin:[email protected]:554/Streaming/Channels/101" -f hls -c:v copy -preset ultrafast -tune zerolatency -hls_list_size 5 -hls_wrap 6 -r 10 -an  "C:\demo-service\demo-nginx\html\hls\123.27.m3u8"

# 播放速度快、H5可以播放,但CPU占用高
ffmpeg -rtsp_transport tcp -re -i "rtsp://admin:[email protected]:554/Streaming/Channels/101" -f hls -crf 23 -preset ultrafast -maxrate 1M -bufsize 300k -r 10 -g 20 -movflags +faststart -tune zerolatency -hls_time 1 -hls_list_size 5 -hls_wrap 6 -start_number 1 -hls_allow_cache 0 -threads 1 -loglevel warning -y "C:\demo-service\demo-nginx\html\hls\123.22.m3u8"

# windows 批处理脚本: %% 转义 %
set password=123456,.%%2F
start /B ffmpeg -rtsp_transport tcp -re -i "rtsp://admin:%password%@192.168.123.22:554/Streaming/Channels/102" -f hls -crf 23 -tag:v hvc1 -preset ultrafast -maxrate 1M -bufsize 300k -r 10 -g 15 -movflags +faststart -tune zerolatency -hls_time 1 -hls_list_size 5 -hls_wrap 6 -start_number 1 -hls_allow_cache 0 -threads 1 -loglevel warning -y -an  "C:\demo-service\demo-nginx\html\hls\123.22.m3u8"

# 重连机制
ffmpeg -rtsp_transport tcp -re -i "rtsp://admin:[email protected]:554/Streaming/Channels/102" -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_on_network_error 1 -reconnect_on_http_error 1 -reconnect_delay_max 4096 -f hls -crf 23 -tag:v hvc1 -preset ultrafast -maxrate 1M -bufsize 300k -r 10 -g 15 -movflags +faststart -tune zerolatency -hls_time 1 -hls_list_size 5 -hls_wrap 6 -start_number 1 -hls_allow_cache 0 -threads 1 -loglevel warning -y -an  "C:\demo-service\demo-nginx\html\hls\123.22.m3u8"
  • Nginx 配置,不需要安装 nginx-rtmp 等扩展模块
server {
    listen       2002;
    server_name  www.demo.com;

    location /hls {
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        alias   html/hls;
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin '*';
        add_header Access-Control-Allow-Headers 'Content-Type,*';
        add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS';
    }
}

# HTTPS server

server {
    listen       2001 ssl;
    server_name  www.demo.com;

    ssl_certificate      ../cert/cert.crt;
    ssl_certificate_key  ../cert/cert.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    # ssl_ciphers  123456;
    # ssl_prefer_server_ciphers  on;

    location /hls {
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        alias   html/hls;
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin '*';
        add_header Access-Control-Allow-Headers 'Content-Type,*';
        add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS';
    }
}
  • 有用的链接

https://videojs.com/html5-video-support/
https://developer.jwplayer.com/jwplayer/docs
https://hls-js.netlify.app/demo/

你可能感兴趣的:(ffmpeg 低延迟高性能推流方案)