forked from OmarSaidIbrahim/video-streaming-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
200 lines (177 loc) · 5.03 KB
/
server.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const express = require("express");
var responseTime = require("response-time");
const app = express();
const fs = require("fs");
const axios = require("axios");
let video;
let videoUrl;
let videoQuality = "sd";
let latency;
app.use(responseTime());
app.get("/", async function (req, res) {
let videoId = req?.query?.videoId;
if (req?.query?.videoId) {
videoId = req?.query?.videoId;
} else {
videoId = "6624849";
}
if (req?.query?.videoQuality) {
videoQuality = req?.query?.videoQuality;
}
video = await getVideo(videoId);
videoUrl = video.video_files.find((v) => v.quality === videoQuality)?.link;
if (!videoUrl && videoQuality === "hls") {
videoUrl = video.video_files.find(
(v) => v.width === 1080 && v.height === 1920
)?.link;
if (!videoUrl) {
videoUrl = video.video_files.find(
(v) => v.width === 2048 && v.height === 1080
)?.link;
}
}
res.sendFile(__dirname + "/public/index.html");
});
app.get("/getVideoDetails", async function (req, res) {
const videoDetails = video;
const duration = videoDetails.duration;
const minutes = Math.floor(duration / 60);
const seconds = duration % 60;
const result = `${padTo2Digits(minutes)}:${padTo2Digits(seconds)}`;
const quality = videoQuality;
const size = getSize();
const htmlResponse = `
<p class="text-white">Current Video Details:</p>
<br/>
<p class="text-white">Duration: ${result}</p>
<p class="text-white">Quality: <span class="uppercase">${quality}</spna></p>
<p class="text-white">Size: ${size}MB</p>
<br/>`;
res.send(htmlResponse);
});
app.get("/video", function (req, res) {
if (!!video) {
res.sendFile(__dirname + `/${video?.id}_${videoQuality}.mp4`);
}
});
app.get("/video-with-streaming", function (req, res) {
const range = req.headers.range;
if (!range) {
res.status(400).send("Requires Range header");
}
if (!!video) {
const videoPath = `${video.id}_${videoQuality}.mp4`;
const videoSize = fs.statSync(`${video.id}_${videoQuality}.mp4`).size;
const CHUNK_SIZE = 10 ** 6;
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": "video/mp4",
};
res.writeHead(206, headers);
const videoStream = fs.createReadStream(videoPath, { start, end });
videoStream.pipe(res);
}
});
app.get("/media", async (req, res) => {
res.status(200).send(videoUrl);
});
app.get("/media-with-streaming", async (req, res) => {
if (
await checkFileExists(__dirname + `/saved/${video?.id}_${videoQuality}.mp4`)
) {
res.sendFile(__dirname + `/${video?.id}_${videoQuality}.mp4`);
} else {
try {
const response = await axios({
method: "GET",
url: videoUrl,
responseType: "stream",
});
response.data.pipe(
fs.createWriteStream(
__dirname + `/saved/${video.id}_${videoQuality}.mp4`
)
);
res.setHeader("Content-Type", "video/mp4");
response.data.on("data", (chunk) => {
res.write(chunk); // Write each chunk to the response stream
});
response.data.on("end", () => {
res.end(); // End the response stream when all data has been sent
});
response.data.on("error", (error) => {
console.error("Error streaming video:", error);
res.status(500).send("Error streaming video");
});
} catch (error) {
console.error("Error downloading video:", error);
}
}
});
app.get("/setLatency", (req, res) => {
res.send("");
latency = res.get("X-Response-Time");
});
app.get("/getLatency", (req, res) => {
const htmlResponse = `<p class="text-white">Latency: ${latency}</p>`;
res.send(htmlResponse);
});
async function getVideo(id) {
try {
const response = await axios.get(
`https://api.pexels.com/videos/videos/${id}`,
{
headers: {
Authorization: process.env.PEXELS_API_KEY,
},
}
);
return response.data;
} catch (error) {
console.error("Error fetching video URL:", error);
throw error; // Rethrow the error to handle it elsewhere if needed
}
}
function padTo2Digits(num) {
return num.toString().padStart(2, "0");
}
const getSize = () => {
if (!!video) {
if (video.id === 6624849) {
switch (videoQuality) {
case "sd":
return "0.8";
case "hd":
return "1.5";
case "hls":
return "4";
}
}
if (video.id === 6251859) {
switch (videoQuality) {
case "sd":
return "111.3";
case "hd":
return "201.5";
case "hls":
return "398.6";
}
}
}
};
const checkFileExists = async (filePath) => {
try {
await fs.promises.access(filePath, fs.constants.F_OK);
return true;
} catch {
return false;
}
};
app.listen(8000, function () {
console.log("Listening on port 8000");
});