服务器node.js用于直播 [英] Server node.js for livestreaming

查看:105
本文介绍了服务器node.js用于直播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在node.js中创建一个服务器,该服务器接收RTMP数据包并将其转换为HLS数据包,然后将其发送回.从iOS不支持RTMP的那一刻起,我就开始创建与每个发布版本兼容的实时流服务.这是我的代码,但是我卡在回调中.很抱歉,我不是JS程序员,这是我进入JS项目的第一步.提前致谢!我的流客户端将是OBS.

I'm trying to create a server in node.js that receives RTMP packets and converts them in HLS packets, then it sends back the packets. I'm doing this to create a livestream service compatible with every dispositive from the moment that iOS doesn't support RTMP. This is my code, but i'm stuck in what i should put into the callback. Sorry for the mess but I'm not a JS programmer and this are my first steps into a JS project. Thanks in advance! My stream client will be 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()

推荐答案

最好将 express 用作http服务器.

You better try express as http server.

您可以从 fluent-ffmpeg 中获取视频流,并通过快递路线中的 res .pipe 结果发送到客户端.回调.

You can get from fluent-ffmpeg a video stream and .pipe the results to the client, through the res in the express routes callbacks.

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 应该会返回视频流.

Now calling http://localhost:3000/myLiveVideo should return the video streaming.

这篇关于服务器node.js用于直播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆