如何在nodejs中流式传输视频

时间:2014-07-11 14:58:09

标签: node.js express video-streaming media-source

我对视频流非常陌生,这让我很困惑

前端:Video.js

<video id="example_video_1" controls="" preload="auto" width="854" height="480" poster="/thumb/1405003437328.mp4-thumbnail-1-undefinedxundefined.jpg" data-setup="{&quot;example_option&quot;:true}" class="video-js vjs-default-skin">
  <source src="/stream/53bea6ad7ff1919812067e74">
</video>

此视频我有两条路线

第一条路线

 /video/video._id/videoname.mp4
 I use this route to render the video streaming html which include a video tag

 exports.get = function(req, res) {
    Video.findById(req.params._id, function(err, video) {
        if (err) {
            res.send(500, {
                error: err
            })
        }
        res.render('video', {
            video: video
        })
    })
 }

第二条路线

 /stream/video._id
 I use this video for streaming

 exports.stream = function(req, res) {
     Video.findById(req.params._id, function(err, video) {
        if (err) {
            throw err
        }
        console.log(video)
        var videopath = path.join(__dirname, '../../userUpload/', video.src);
        console.log(videopath)

        res.contentType('flv');
    // make sure you set the correct path to your video file storage

        var proc =new  ffmpeg({source: videopath})
            // use the 'flashvideo' preset (located in /lib/presets/flashvideo.js)
            .usingPreset('flashvideo')
            // setup event handlers
            .on('end', function() {
                console.log('file has been converted succesfully');
            })
            .on('error', function(err) {
                console.log('an error happened: ' + err.message);
            })
            // save to stream
            .writeToStream(res, {
                end: true
            });
        })
    }

我使用fluent-ffmpeg播放视频,上面的代码位于examples

当提供第一条路线时,它会请求/stream/video._id路线,但是当我点击网络上的播放按钮时没有发生任何事情,请帮我理解视频流!

感谢名单

0 个答案:

没有答案