Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
307 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...