Permalink
Browse files

Stream de vídeo

  • Loading branch information...
emerleite committed May 17, 2011
1 parent fda8dff commit 7e49c6082ab4a4b6c590a7952243f8f8db7a52d2
Showing with 40 additions and 0 deletions.
  1. +40 −0 video.js
  2. BIN video.mp4
View
@@ -0,0 +1,40 @@
+var http = require('http'),
+ fileSystem = require('fs'),
+ path = require('path'),
+ util = require('util');
+
+var server = http.createServer(function(request, response) {
+ var filePath = path.join(__dirname, 'video.mp4');
+ var stat = fileSystem.statSync(filePath);
+
+ var inicio = 0;
+ var fim = 0;
+
+ var range = request.headers.range;
+ console.log(range);
+
+ if (range) {
+ inicio = parseInt(range.slice(range.indexOf("bytes=") + 6, range.indexOf("-")));
+ fim = parseInt(range.slice(range.indexOf("-") + 1, range.length));
+ }
+
+ if (isNaN(fim) || fim == 0) {
+ fim = stat.size-1;
+ }
+
+ if (inicio > fim) return;
+
+ response.writeHead(206, {
+ 'Content-Type': 'video/mp4',
+ 'Content-Length': stat.size,
+ 'Content-Range':'bytes '+ inicio + '-' + fim + '/' + stat.size,
+ 'Transfer-Encoding':'chunked'
+ });
+
+ var readStream = fileSystem.createReadStream(filePath, { flags: 'r', start: inicio, end: fim});
+
+ util.pump(readStream, response);
+});
+
+server.listen(2000);
+
View
BIN video.mp4
Binary file not shown.

0 comments on commit 7e49c60

Please sign in to comment.