wrap h.264 stream in mp.4 container and stream it with nodejs

大城市里の小女人 提交于 2019-12-12 03:27:40

问题


I have a stream of h.264 data from a remote webcam. If i save it to a file i'm able to play it in VLC (meaning that the data arrives intact).

The final goal is to turn this stream into a virtual webcam. After looking around I found manyCam as a possible solution - therefor i want to serve the h.264 data on a local IP in MP4 format.

Two questions:

first, I'm trying to wrap the h.264 with the mp4 container using ffmpeg (using fluent-ffmpeg npm library that exposes the ffmpeg API to Nodejs).

Everything works well when i'm handling static files (not streams). e.g.`

var ffmpeg = rquire('fluent-ffmpeg')
var readH264 = fs.createReadStream('./vid.h264')
var proc = ffmpeg(readH264).clone().toFormat('mp4').output('./vid.mp4').run()

`

But when I'm trying to feed a stream - it throws an error "ffmpeg exited with code 1: could not write header for output file.." `

var wrtieMp4 = fs.createWriteStream('./vid.mp4')
var proc = ffmpeg(readH264).clone().toFormat('mp4').output(wrtieMp4).run()`

How can i add it a header..?

Second, I'm a bit confused about the transport layer (rtp, rtsp, etc.). After creating the mp4 stream - wouldn't it be sufficient to serve the stream with MIME type video/mp4? It seem to work fine with static file. `

let read = fs.createReadStream('./vid.mp4')
let server = http.createServer(function (req, res) {
        res.writeHead(200, {'Content-type': "video/mp4"})
        read.pipe(res)
}).listen(9000)

`


回答1:


You can not use MP4 for this purpose. MP4 must write the header at the beginning of the file after the stream is closed. You get "can not write header" error, because ffmpeg knows it will not be able to rewind the stream and write the header later. No, you can not use rtsp eaither. The browser can only do http. Use a format like DASH that was designed for this purpose.



来源:https://stackoverflow.com/questions/43113805/wrap-h-264-stream-in-mp-4-container-and-stream-it-with-nodejs

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