nginx rtmp hls配置,ffmpeg推流

nginx rtmp 服务搭建
nginx rtmp hls配置

# 配置rtmp
# rtmp地址 rtmp://ip:1935/live/xxx
rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        application live {
              live on;
              hls on;
              hls_path /usr/local/hls;
              hls_fragment 1300ms;
              hls_max_fragment 1800ms;
              hls_playlist_length 2s;
              hls_sync 3ms;
              hls_nested on;
              hls_fragment_naming system;
          }

    }
}
# 配置http访问hls流
# http://ip:8080/hls/xxx/index.m3u8
server {
        listen       8080;
        server_name  localhost;

        location /hls {  #Access video stream HTTP storage address
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            #Access permission is enabled, otherwise access to this address will report 403
            autoindex on;
            alias /usr/local/hls;#The video stream storage address is the same as the HLS above_ Path.
            expires -1;
            add_header Cache-Control no-cache;
            #Prevent cross domain problems
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

ffmpeg推流地址

# rtsp推流rtmp
ffmpeg  -i "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov" -f flv -vcodec copy -acodec copy -s 1280x720 -q 10 "rtmp://ip:1935/live/xxx"
# http推流rtmp
ffmpeg  -i "http://xxx" -c copy -f flv "rtmp://ip:1935/live/xxx"
# 海康rtsp推流m3u8
ffmpeg -i rtsp://xxx -fflags flush_packets -max_delay 1 -an -flags -global_header -hls_time 1 -hls_list_size 3 -hls_wrap 3 -vcodec copy -y /usr/local/hls/xxx.m3u8
# 海康rtsp推流rtmp
ffmpeg -y -f rawvideo -vcodec rawvideo -pix_fmt bgr24 -s 1280x720 -r 25.0 -i rtsp://xxx -c:v libx264 -pix_fmt yuv420p -preset ultrafast -an -g 0.2 -f flv rtmp://ip:1935/hls/xxx
# 推流到websocket,-s 1366x768 设置转发到websocket的分辨率,http://127.0.0.1:8081/supersecret 转发的websocket地址
ffmpeg -i "rtsp://127.0.0.1/test" -q 0 -f mpegts -codec:v mpeg1video -s 1366x768 http://127.0.0.1:8081/supersecret

http-flv 在html播放

nginx添加http-flv模块
http-flv服务器配置

# 配置rtmp
# rtmp地址 rtmp://ip:1935/live/xxx
rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        application live {
              live on;
              meta off;
              allow play all;
              #hls on;
              #hls_path /usr/local/hls;
              #hls_fragment 1300ms;
              #hls_max_fragment 1800ms;
              #hls_playlist_length 2600ms;
              #hls_sync 3ms;
              #hls_nested on;
              #hls_cleanup on;
              #hls_fragment_naming system;
          }

    }
}
# 配置http访问http-flv流
# http://ip:8080/flv/xxx.fiv或http://ip:8080/flv?app=live&stream=xxx
location /flv {
            flv_live on;
            chunked_transfer_encoding on;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
location /ws_flv {
         proxy_pass http://board.xncoding.com;
         proxy_http_version 1.1;
         proxy_set_header Connection "upgrade";
         proxy_set_header Upgrade $http_upgrade;
         #proxy_set_header Host $http_host;
         #proxy_set_header Remote_addr_$remote_addr;
         #proxy_set_header X-Forwarded-For $remote_addr:$remote_port; 
         proxy_redirect off;
}
<script src="flv.min.js"></script>
<video id="videoElement"></video>
<script>
    if (flvjs.isSupported()) {
        var videoElement = document.getElementById('videoElement');
        var flvPlayer = flvjs.createPlayer({
            type: 'flv',
            url: 'http://example.com/flv/video-stream.flv'
        });
        flvPlayer.attachMediaElement(videoElement);
        flvPlayer.load();
        flvPlayer.play();
    }
</script>

hls_fragment设置无效问题

问题解决网站
问题
根据官方Wiki的说明,参数hls_fragment是用来设置HLS分片的长度的。默认是5秒。
但实际操作中,你可能会发现hls_fragment设置的值没有效果。

原因
在Issue 1026中翻到了作者arut的回复。

Fragments are started at keyframes. If keyframes are rare, that’s what you get. Try using the -g option of ffmpeg.

hls_fragment设置依赖于视频源中的关键帧,如果源视频中没有关键帧,那么hls_fragment的设置就无效了。可以在ffmpeg中使用-g来插入关键帧。

解决方法
ffmpeg中-g参数的说明: https://trac.ffmpeg.org/wiki/EncodingForStreamingSites#a-g

-g: Use a 2 second GOP (Group of Pictures), so simply multiply your output frame rate * 2. For example, if your input is -framerate 30, then use -g 60.

-g的使用依赖于原始的帧率,如果想要每2秒就插一个关键帧,输入源的帧率是30,那么此处-g就需要设置为60.

附加一个关于使用flv.js报错的处理方法的一篇文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值