尝试通过具有FFMPEG的RTMP流实时视频时,我收到连接被拒绝的错误

问题描述

我正在开发一个nodeJs应用程序,该应用程序可以使用RTMP协议将摄像机流发送到第三方平台(即Facebook和Youtube);.在我的本地主机上运行良好,但是一旦部署到服务器,它只会给我带来错误。我收到的错误在此内容下面。 这是我的密码

server.js

const child_process = require('child_process'); // To be used later for running FFmpeg
const express = require('express');
const http = require('http');
const WebSocketServer = require('ws').Server;

const app = express();
const server = http.createServer(app).listen(4000,() => {
  console.log('Listening...');
});

// Serve static files out of the www directory,where we will put our HTML page
app.use(express.static(__dirname + '/www'));


const wss = new WebSocketServer({
  server: server
});
wss.on('connection',(ws,req) => {
  
  
  
  const rtmpurl = 'rtmp://a.rtmp.youtube.com/live2/MyStreamId';
  console.log('Target RTMP URL:',rtmpurl);
  
  // Launch FFmpeg to handle all appropriate transcoding,muxing,and RTMP.
  // If 'ffmpeg' isn't in your path,specify the full path to the ffmpeg binary.
  const ffmpeg = child_process.spawn('ffmpeg',[
    // Facebook requires an audio track,so we create a silent one here.
    // Remove this line,as well as `-shortest`,if you send audio from the browser.
    //'-f','lavfi','-i','anullsrc',// FFmpeg will read input video from STDIN
    '-i','-',// Because we're using a generated audio source which never ends,// specify that we'll stop at end of other input.  Remove this line if you
    // send audio from the browser.
    //'-shortest',// If we're encoding H.264 in-browser,we can set the video codec to 'copy'
    // so that we don't waste any cpu and quality with unnecessary transcoding.
    // If the browser doesn't support H.264,set the video codec to 'libx264'
    // or similar to transcode it to H.264 here on the server.
    '-vcodec','copy',// AAC audio is required for Facebook Live.  No browser currently supports
    // encoding AAC,so we must transcode the audio to AAC here on the server.
    '-acodec','aac',// FLV is the container format used in conjunction with RTMP
    '-f','flv',// The output RTMP URL.
    // For debugging,you Could set this to a filename like 'test.flv',and play
    // the resulting file with VLC.  Please also read the security considerations
    // later on in this tutorial.
    rtmpurl 
  ]);
  
  // If FFmpeg stops for any reason,close the WebSocket connection.
  ffmpeg.on('close',(code,signal) => {
    console.log('FFmpeg child process closed,code ' + code + ',signal ' + signal);
    ws.terminate();
  });
  
  // Handle STDIN pipe errors by logging to the console.
  // These errors most commonly occur when FFmpeg closes and there is still
  // data to write.  If left unhandled,the server will crash.
  ffmpeg.stdin.on('error',(e) => {
    console.log('FFmpeg STDIN Error',e);
  });
  
  // FFmpeg outputs all of its messages to STDERR.  Let's log them to the console.
  ffmpeg.stderr.on('data',(data) => {
    console.log('FFmpeg STDERR:',data.toString());
  });

  // When data comes in from the WebSocket,write it to FFmpeg's STDIN.
  ws.on('message',(msg) => {
    console.log('DATA',msg);
    ffmpeg.stdin.write(msg);
  });
  
  // If the client disconnects,stop FFmpeg.
  ws.on('close',(e) => {
    ffmpeg.kill('SIGINT');
  });
  
});

在server.js文件上,我创建了一个websocket来从客户端接收流数据,然后使用FFMPEG通过RTMP url将流数据发送到youtube

这是我的client.js代码

const ws = new WebSocket(
             'wss://my-websocket-server.com'

        );
         ws.addEventListener('open',(e) => {
             console.log('WebSocket Open',e);
             drawVideosToCanvas();
             mediaStream = getMixedVideoStream(); // 30 FPS
             mediaRecorder = new MediaRecorder(mediaStream,{
               mimeType: 'video/webm;codecs=h264',//videoBitsPerSecond : 3000000000
               bitsPerSecond: 6000000
             });

             mediaRecorder.addEventListener('dataavailable',(e) => {
               ws.send(e.data);
             });
             mediaRecorder.onstop = function() {
              ws.close.bind(ws);
              isRecording = false;
              actionBtn.textContent = 'Start Streaming';
              actionBtn.onclick = startRecording;
             }
             mediaRecorder.onstart = function() {
              isRecording = true;
              actionBtn.textContent = 'Stop Streaming';
              actionBtn.onclick = stopRecording;
              screenShareBtn.onclick = startSharing;
              screenShareBtn.disabled = false;
             }
             //mediaRecorder.addEventListener('stop',ws.close.bind(ws));

             mediaRecorder.start(1000); // Start recording,and dump data every second

           });

在我的client.js文件中,我捕获了用户的相机,然后打开websocket服务器将数据发送到服务器。在我将其部署到实时服务器时,本地主机上的所有工作都正常。 我想知道服务器上是否配置错误。服务器是Centos 7.8,应用程序在Apache软件上运行 这是我为websocket域配置虚拟主机的方式

ServerName my-websocket.com

  RewriteEngine on
  RewriteCond %{HTTP:Upgrade} websocket [NC]
  RewriteCond %{HTTP:Connection} upgrade [NC]
  RewriteRule .* "ws://127.0.0.1:3000/$1" [P,L]

  ProxyPass "/" "http://127.0.0.1:3000/$1"
  ProxyPassReverse "/" "http://127.0.0.1:3000/$1"
  ProxyRequests off

我对服务器的配置了解不多,但我只是认为配置可能与为什么FFMPEg无法打开服务器上的RTMP协议的连接有关。

这里的错误越来越严重

FFmpeg STDERR: Input #0,lavfi,from 'anullsrc':
  Duration:
FFmpeg STDERR: N/A,start: 0.000000,bitrate: 705 kb/s
    Stream #0:0: Audio: pcm_u8,44100 Hz,stereo,u8,705 kb/s

DATA <Buffer 1a>
DATA <Buffer 45 df a3 a3 42 86 81 01 42 f7 81 01 42 f2 81 04 42 f3 81 08 42 82 88 6d 61 74 72 6f 73 6b 61 42 87 81 0442 85 81 02 18 53 80 67 01 ff ff ff ff ff ff ... 53991 more bytes>
DATA <Buffer 40 c1 81 00 f0 80 7b 83 3e 3b 07 d6 4e 1c 11 b4 7f cb 5e 68 9b d5 2a e3 06 c6 f3 94 ff 29 16 b2 ff 60 04ac 37 fb 1a 15 ea 40 29 39 a0 1a cd 02 b8 e3 ... 56206 more bytes>
FFmpeg STDERR: Input #1,matroska,webm,from 'pipe:':
  Metadata:
    encoder         :
FFmpeg STDERR: Chrome
  Duration: N/A,bitrate: N/A
    Stream #1:0(eng): Audio: opus,48000 Hz,mono,fltp (default)
    Stream #1:1(eng): Video: h264 (Constrained Baseline),yuv420p(progressive),1366x768,SAR 1:1 DAR 683:384,30.30 fps,30 tbr,1k tbn,60 tbc (default)

FFmpeg STDERR: [tcp @ 0xe5fac0] Connection to tcp://a.rtmp.youtube.com:1935 Failed (Connection refused),trying next address
[rtmp @ 0xe0fb80] Cannot open connection tcp://a.rtmp.youtube.com:1935

FFmpeg STDERR: rtmp://a.rtmp.youtube.com/live2/mystreamid: Network is unreachable

FFmpeg child process closed,code 1,signal null

如果能对导致此问题的原因或解决该问题的方法有所了解,我将不胜感激。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)