winshining / nginx-http-flv-module

A media streaming server based on nginx-rtmp-module. In addtion to the features nginx-rtmp-module provides, HTTP-FLV, GOP cache, VHosts (one IP for multi domain names) and JSON style statistics are supported now.
BSD 2-Clause "Simplified" License
2.75k stars 574 forks source link

[配置问题]摄像头推流,HTTP-FLV + flv.js播放失败 #97

Closed forest-yang closed 5 years ago

forest-yang commented 5 years ago

我测试使用nginx + nginx-http-flv-module + flv.js直接视频,报错(使用VLC播放正常):

nginx error.log错误日志:

flv live: app args MUST be specified, client: 192.168.89.1, server: myserver, request: "GET /live.html HTTP/1.1", host: "192.168.89.134:8080"

关键配置说明:

nginx配置

http{
   ...
   server{
   ...
    location /live {
            flv_live on;
            chunked_transfer_encoding  on; #open 'Transfer-Encoding: chunked' response
            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
        }

    }
}

rtmp {
    server {
        listen 1935;
        server_name 192.168.89.134;

        application myapp {
            live on;
            gop_cache on; 
          }
    }
}

推流命令:

ffmpeg -f avfoundation -s 1280x720 -r 30  -i 0 -f flv rtmp://192.168.89.134:1935/myapp/test

使用VLC播放链接:

http://192.168.89.134:8080/live?port=1935&app=myapp&stream=test

html代码片段:

 if (flvjs.isSupported()) {
            var videoElement = document.getElementById('videoElement');
            var flvPlayer = flvjs.createPlayer({
                type: 'flv',
                'isLive': true,
                url: 'http://192.168.89.134:8080/live?port=1935&app=myapp&stream=test'
            });
            flvPlayer.attachMediaElement(videoElement);
            flvPlayer.load();
            flvPlayer.play();
        }

请问是哪个环节出了问题?多谢! 环境: Nginx: 1.15.10 nginx-http-flv-module: 1.2.6 Chrome: 73

winshining commented 5 years ago

flv.js只支持H.264视频编码和AAC/MP3音频编码,请确认摄像头推流的音视频编码格式

forest-yang commented 5 years ago

我的机器是MAC OS,设备列表输出如下: [AVFoundation input device @ 0x7f84c591e9c0] AVFoundation video devices: [AVFoundation input device @ 0x7f84c591e9c0] [0] FaceTime HD Camera [AVFoundation input device @ 0x7f84c591e9c0] [1] Capture screen 0 [AVFoundation input device @ 0x7f84c591e9c0] [2] Capture screen 1 [AVFoundation input device @ 0x7f84c591e9c0] AVFoundation audio devices: [AVFoundation input device @ 0x7f84c591e9c0] [0] Built-in Microphone

我换了个命令从摄像头获取视频:

ffmpeg -f avfoundation -framerate 30 -i 0  -c:v libx264 -r 30 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -preset veryslow -f flv rtmp://192.168.31.132:1935/myapp/test

同样,推流命令可以执行,VLC观看成功,但是flv.js就是不成功。nginx日志报错:

flv live: app args MUST be specified, client: 192.168.31.67, server: localhost, request: "GET /live.html HTTP/1.1", host: "192.168.31.132:8080"

请问可能是哪里的错误呢?是HTML里的访问链接错误还是nginx.conf里的live小节配置错误?

winshining commented 5 years ago

那跟nginx-http-flv-module没关系,我在Mac上也从来没成功过,用Windows和Linux上的Chrome和Firefox都测试过,没问题。flv.js需要MSE的支持:https://github.com/bilibili/flv.js#overview Apple对MSE(故意)支持不太好!

forest-yang commented 5 years ago

非常感谢提示(和nginx-http-flv-module没关系),是我配置的原因,我直接打开html页面(里面包含访问流的链接,不通过nginx web服务器),可以访问了。

当然还有个小问题,为什么直播页面html放在nginx里不行,我自己再琢磨下。

winshining commented 5 years ago

你再看看吧,祝好运! 准确点说是在Mac上的Safari测试从来没成功过,Mac的Firefox是可以的,README里的截图用的就是Mac上的Firefox截图。很奇怪的是用 http://html5test.com/ 查看Safari是支持MSE的,但是确实从来没成功过。