zoukankan      html  css  js  c++  java
  • 解决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;
    

      

  • 相关阅读:
    hdu 2295 DLX
    hdu 4714 树形DP
    hdu 4711 动态规划
    hdu 3656 DLX
    hust 1017 DLX
    hdu 3938 并查集
    hdu 3652 打表
    poj 2152 树形DP
    洛谷P1266速度限制
    洛谷P1841重要的城市
  • 原文地址:https://www.cnblogs.com/shenggen/p/5667910.html
Copyright © 2011-2022 走看看