Solve problems with express video phone not playing

Source: Internet
Author: User

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

Project Address Https://github.com/shenggen1987/mp4-demo

Express Index.jade

Extends Layoutblock 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], ten): 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 (re        s);        }). On ("Error", function (Err) {res.end (err);  }); });}); Module.exports = router;

  

Solve problems with express video phone not playing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.