【问题标题】:Server node.js for livestreaming用于直播的服务器 node.js
【发布时间】:2018-07-02 05:51:41
【问题描述】:

我正在尝试在 node.js 中创建一个服务器,它接收 RTMP 数据包并将它们转换为 HLS 数据包,然后将数据包发回。我这样做是为了创建一个与 iOS 不支持 RTMP 的那一刻起与所有配置兼容的直播服务。这是我的代码,但我被困在我应该放入回调的内容中。很抱歉造成混乱,但我不是 JS 程序员,这是我进入 JS 项目的第一步。提前致谢!我的流客户端将是 OBS。

    import { Server } from 'https';

var hls = require('hls-server')(8000);
var ffmpeg = require('fluent-ffmpeg')

// host, port and path to the RTMP stream
var host = 'localhost'
var port = '8000'
var path = '/live/test'



clients = [];

function callback(){        

}  
fmpeg('rtmp://'+host+':'+port+path, { timeout: 432000 }).addOptions([
    '-c:v libx264',
    '-c:a aac',
    '-ac 1',
    '-strict -2',
    '-crf 18',
    '-profile:v baseline',
    '-maxrate 400k',
    '-bufsize 1835k',
    '-pix_fmt yuv420p',
    '-hls_time 10',
    '-hls_list_size 6',
    '-hls_wrap 10',
    '-start_number 1'
  ]).output('public/videos/output.m3u8').on('end', callback).run()

【问题讨论】:

    标签: node.js live-streaming


    【解决方案1】:

    你最好尝试express作为http服务器。

    您可以通过快速路由回调中的resfluent-ffmpeg 获取视频流和.pipe 将结果发送到客户端。

    const express = require('express')
    const app = express()
    
    const HOST = '...'
    const PORT = '...'
    const PATH = '...'
    
    let video = fmpeg(`rtmp://${HOST}:${PORT}${PATH}`, { timeout: 432000 }).addOptions([
    '-c:v libx264',
    '-c:a aac',
    '-ac 1',
    '-strict -2',
    '-crf 18',
    '-profile:v baseline',
    '-maxrate 400k',
    '-bufsize 1835k',
    '-pix_fmt yuv420p',
    '-hls_time 10',
    '-hls_list_size 6',
    '-hls_wrap 10',
    '-start_number 1'
    ]).pipe()
    
    app.get('/myLiveVideo', (req, res) => {
      res.writeHead(200, {
        'Content-Type': 'video/mp4'
      })
      video.pipe(res) // sending video to the client
    })
    
    app.listen(3000)
    

    现在调用http://localhost:3000/myLiveVideo 应该会返回视频流。

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      相关资源
      最近更新 更多