简体   繁体   中英

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

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). eg`

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)

`

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM