-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_stream.js
106 lines (93 loc) · 2.47 KB
/
video_stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// var fs=require('fs'),
// http=require('http'),
// url=require('url'),
// path=require('path');
// var indexpage='';
// fs.readFile("index.html",function(err,data){
// if(err)
// {
// throw err;
// }
// indexpage=data;
// });
// http.createServer(function(req,res){
// var reqResource=url.parse(req.url).pathname;
// console.log(reqResource);
// if(reqResource=="/" || reqResource=="/index.html")
// {
// res.writeHead(200,{'content-type':'text/html'});
// res.write(indexpage);
// res.end();
// }
// else if(reqResource='favicon.ico')
// {
// res.writeHead(404);
// res.end();
// }
// else
// {
// console.log(reqResource);
// var file="" + reqResource;
// fs.stat(file,function(err,stats){
// console.log(stats);
// if(err)
// {
// if(err.code==='ENDENT')
// {
// return res.send.status(404);
// }
// res.end(err);
// }
// var range=req.headers.range;
// var parts=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,
// "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);});
// });
// }
// }).listen(8080);
var fs=require('fs'),
http=require('http'),
url=require('url'),
path=require('path');
var express=require('express');
var app=express();
app.get('/video', function(req, res) {
const path = 'videos/Boat_13.mp4'
const stat = fs.statSync(path)
const fileSize = stat.size
const range = req.headers.range
if (range) {
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10)
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize-1
const chunksize = (end-start)+1
const file = fs.createReadStream(path, {start, end})
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
}
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
}
res.writeHead(200, head)
fs.createReadStream(path).pipe(res)
}
});
app.listen(8080);