Server node.js for livestreaming

这一生的挚爱 提交于 2020-06-01 07:24:47

问题


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()

回答1:


You better try express as http server.

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)

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



来源:https://stackoverflow.com/questions/48401234/server-node-js-for-livestreaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!