๐Ÿ“ฆ harshadixit12 / tiny-video-stream

๐Ÿ“„ app.js ยท 60 lines
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
60const express = require('express');
const fs = require('fs');
const videoList = require('./video-list');

const app = express();

app.get('/', (req, res) => {
  res.json(videoList);
});

app.get('videos/:id/metadata', (req,res)=> {
  const id = parseInt(req.params.id, 10)
  res.json(videoList[id])
});

app.get('/videos/:id/', (req,res)=> {
  const id = parseInt(req.params.id, 10);
  const videoPath = `./assets/${videoList[id-1].path}`;
  const videoStat = fs.statSync(videoPath);
  const fileSize = videoStat.size;

  const videoRange = req.headers.range;

  if (videoRange) {
    const parts = videoRange.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(videoPath, {start, end});

    const header = {
    'Content-Range': `bytes ${start}-${end}/${fileSize}`,
    'Accept-Ranges': 'bytes',
    'Content-Length': chunksize,
    'Content-Type': 'video/mp4',
    };

    res.writeHead(206, header);

    console.log(header, '\n\n', res);

    file.pipe(res);
  }
  else {
    const header = {
      'Content-Length': fileSize,
      'Content-Type': 'video/mp4',
    };
      
    console.log(header, '\n\n', res);
    res.writeHead(200, header);
    
    fs.createReadStream(videoPath).pipe(res);
  }
});

app.listen(8000, function(){
  console.log("info",'Server is running at port : ' + 8000);
});