Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

node.js - 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). 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)

`

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...