解决express video 手机无法播放的问题

http://stackoverflow.com/questions/24976123/streaming-a-video-file-to-an-html5-video-player-with-node-js-so-that-the-video-c

项目地址  https://github.com/shenggen1987/mp4-demo 

express  index.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  video(src="/video" controls="controls" type="video/mp4" width="320px" height="480px")

 express  routes/users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  //res.send('respond with a resource');
  var fs = require("fs") ;

  //var video = fs.createReadStream('./public/images/sina.mp4');
  //res.set('Content-Type', 'video/mp4');
  //video.pipe(res)


  var file = './public/images/sina.mp4';
  fs.stat(file, function(err, stats) {
    if (err) {
      if (err.code === 'ENOENT') {
        // 404 Error if file not found
        return res.sendStatus(404);
      }
      res.end(err);
    }
    var range = req.headers.range;
    if (!range) {
      // 416 Wrong range
      return res.sendStatus(416);
    }
    var positions = range.replace(/bytes=/, "").split("-");
    var start = parseInt(positions[0], 10);
    var total = stats.size;
    var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
    var chunksize = (end - start) + 1;

    res.writeHead(206, {
      "Content-Range": "bytes " + start + "-" + end + "/" + total,
      "Accept-Ranges": "bytes",
      "Content-Length": chunksize,
      "Content-Type": "video/mp4"
    });

    var stream = fs.createReadStream(file, { start: start, end: end })
        .on("open", function() {
          stream.pipe(res);
        }).on("error", function(err) {
          res.end(err);
        });
  });

});

module.exports = router;

  

 

posted on 2016-07-13 18:37  shenggen  阅读(286)  评论(0编辑  收藏  举报

导航