Skip to content

Commit c190981

Browse files
Video-stream-nodejs
1 parent 077f966 commit c190981

7 files changed

Lines changed: 1038 additions & 1 deletion

File tree

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
"# Simple-Video-Stream"
1+
# Simple-Video-Stream 🎥
2+
3+
A simple video streaming server built with Node.js and Express. Stream videos easily with smooth playback. Perfect for learning or small projects!
4+
5+
---
6+
7+
## How to Use
8+
9+
1. **Clone this repo:**
10+
11+
```bash
12+
git clone https://github.com/your-username/Simple-Video-Stream.git
13+
```
14+
15+
2. **Go to the project folder:**
16+
17+
```bash
18+
cd Simple-Video-Stream
19+
```
20+
21+
3. **Install dependencies:**
22+
23+
```bash
24+
npm install
25+
```
26+
27+
4. **Add your videos:**
28+
Put your video files in the uploads/video folder.
29+
30+
5. **Start the server:**
31+
32+
```bash
33+
npm run dev
34+
```
35+
36+
**How to Play Videos**
37+
Place your video files in the uploads/video folder.
38+
39+
Open your browser and go to http://localhost:3000.
40+
41+
Enter the video filename (like sample.mp4) and click "Play Video".
42+
43+
**How to Contribute**
44+
**Fork this repo.**
45+
46+
**Create a new branch: git checkout -b my-feature.**
47+
48+
**Make your changes and commit: git commit -m "Add new feature".**
49+
50+
**Push to the branch: git push origin my-feature.**
51+
52+
**Submit a pull request.**
53+
54+
If you like this project, give it a ⭐️ on GitHub and follow me for more cool projects!
55+
56+
Made with ❤️ by Dilshodbek-yoqubjonov

backend/app.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const express = require("express");
2+
const fs = require("fs");
3+
const path = require("path");
4+
const cors = require("cors");
5+
6+
const app = express();
7+
const PORT = 3000;
8+
9+
// Videolar joylashgan papka
10+
const videosDir = path.join(__dirname, "../video");
11+
app.use(express.static(path.join(__dirname, "../frontend"))); // for frontend
12+
13+
app.use(cors());
14+
15+
app.get("/", (req, res) => {
16+
res.sendFile(path.join(__dirname, "../frontend", "index.html")); // for frontend
17+
});
18+
19+
app.get("/video/:filename", (req, res) => {
20+
const { filename } = req.params;
21+
const videoPath = path.join(videosDir, filename);
22+
23+
if (!fs.existsSync(videoPath)) {
24+
return res.status(404).send("Video not found");
25+
}
26+
27+
const stat = fs.statSync(videoPath);
28+
const fileSize = stat.size;
29+
const range = req.headers.range;
30+
31+
const chunkSize = 200 * 1024; // 200KB chunk size
32+
33+
if (range) {
34+
const parts = range.replace(/bytes=/, "").split("-");
35+
const start = parseInt(parts[0], 10);
36+
const end = Math.min(start + chunkSize - 1, fileSize - 1);
37+
38+
const file = fs.createReadStream(videoPath, { start, end });
39+
const head = {
40+
"Content-Range": `bytes ${start}-${end}/${fileSize}`,
41+
"Accept-Ranges": "bytes",
42+
"Content-Length": end - start + 1,
43+
"Content-Type": "video/mp4",
44+
};
45+
46+
res.writeHead(206, head);
47+
file.pipe(res);
48+
} else {
49+
const head = {
50+
"Content-Length": fileSize,
51+
"Content-Type": "video/mp4",
52+
};
53+
54+
res.writeHead(200, head);
55+
fs.createReadStream(videoPath).pipe(res);
56+
}
57+
});
58+
59+
app.listen(PORT, () => {
60+
console.log(`http://localhost:${PORT} <- click here to open the app`);
61+
});
62+
63+
// github link: https://github.com/dilshodbek-yoqubjonov

0 commit comments

Comments
 (0)