How do I convert an h.264 stream to MP4 using ffmpeg and pipe the result to the client?

 ̄綄美尐妖づ 提交于 2020-01-02 08:11:30

问题


I have an h.264 encoded stream of video on my server (node.js) and I want to use ffmpeg to convert it to an MP4 stream. Then I want to pipe that MP4 stream from the child process to the client using the response of an HTTP server that I have set up. I am very confused about all the options ffmpeg has and not sure how to pipe the output of the child process to the HTTP response.

I have tried several combinations of ffmpeg options but the video does not play in the browser (or show any sign of receiving data) and I don't know if I am converting it to MP4 corretly. I am also not sure I am piping it correctly. I am not getting any errors on either the server or client side.

HTML:

<video id="videoPlayer" poster="assets/logo_25.png" autoplay muted controls>
    <source src="http://localhost:3001" type="video/mp4">
</video>

Node Server:

const videoPORT = 3001;
var videoServer = http.createServer(function (req, res) {
    var videoIncommingPORT = 11111;
    var videoSocket = dgram.createSocket('udp4');

    videoSocket.on('listening', function () {
        var address = videoSocket.address();
    });

    videoSocket.on('message', function (message, remote) {
        var child_converter = spawn('ffmpeg', ['-fflags', '+genpts', '-r', '25', '-i', `${message}`, '-vcodec', 'libx264', '-f', 'mp4', 'copy', '-']);//convert h.264 to MP4 container

        child_converter.stdout.pipe(res);//pipe to response

        child_converter.stdout.on('data', (data) => {
            console.log(data);
        });
        child_converter.stderr.on('data', (data) => { 
            console.log(data); 
        });

    });
    videoSocket.bind(videoIncommingPORT);
});
videoServer.listen(videoPORT);

来源:https://stackoverflow.com/questions/56407245/how-do-i-convert-an-h-264-stream-to-mp4-using-ffmpeg-and-pipe-the-result-to-the

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