-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
156 lines (122 loc) · 4.26 KB
/
app.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
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const fs = require('fs/promises');
const fsG = require('fs');
const videoUpload = require('./upload');
const jwt = require('jsonwebtoken');
const dotenv = require('dotenv');
const { login } = require('./auth');
dotenv.config();
const cors = require('cors');
const videoProcessor = require('./videoProcessor');
const WebSocket = require('ws');
const app = express();
app.use(cors());
const PORT = process.env.PORT || 5000;
// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.log("Failed to connect to MongoDB", err));
// Middleware
app.use(express.json());
app.use(express.static('uploads'))
const compression = require('compression');
app.use(compression());
// User authentication (Basic JWT auth)
const authenticateJWT = (req, res, next) => {
const token = req.header('Authorization')?.split(' ')[1];
if (!token) return res.sendStatus(403);
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
if (err) return res.sendStatus(403);
req.user = user;
next();
});
};
// Add cache middleware
const cacheControl = (duration) => {
return (req, res, next) => {
res.setHeader('Cache-Control', `public, max-age=${duration}`);
next();
};
};
// Login route
app.post("/login", login);
// Video Upload Route
app.post('/upload', videoUpload, async (req, res) => {
const videoPath = req.file.path;
console.log({videoPath});
const outputDir = path.join('uploads', 'hls', Date.now().toString());
// Set up event listeners for this specific upload
videoProcessor.on('progress', (progress) => {
console.log(`Processing ${progress.resolution}: ${progress.percent}%`);
});
videoProcessor.on('error', (error) => {
console.error('Processing error:', error);
});
try {
// Create output directory
await fs.mkdir(outputDir, { recursive: true });
// Convert video to HLS using the VideoProcessor instance
const result = await videoProcessor.convertToHLS(videoPath, outputDir);
res.json({
message: 'Video uploaded and processed successfully!',
hlsPath: outputDir,
masterPlaylist: result.masterPlaylist,
variants: result.variants
});
} catch (err) {
console.error('Error processing video:', err);
res.status(500).json({ message: 'Error processing video' });
// Clean up: remove the uploaded file and created directory
try {
} catch (unlinkError) {
console.error('Error deleting video file:', unlinkError);
}
try {
await fs.rm(outputDir, { recursive: true, force: true });
} catch (rmError) {
console.error('Error deleting output directory:', rmError);
}
} finally {
// Remove event listeners to prevent memory leaks
videoProcessor.removeAllListeners('progress');
videoProcessor.removeAllListeners('error');
}
});
// Add this route to your app.js
app.get('/video/:filename', async (req, res) => {
const { filename } = req.params;
const videoPath = path.join('uploads', 'hls', filename, 'master.m3u8');
// Check if file exists
try {
await fs.access(videoPath);
} catch (error) {
return res.status(404).json({ message: 'Video not found' });
}
// Set appropriate headers for HLS streaming
res.setHeader('Content-Type', 'application/x-mpegURL');
res.setHeader('Access-Control-Allow-Origin', '*');
// Stream the master playlist
const readStream = fsG.createReadStream(videoPath);
readStream.pipe(res);
});
// Add route for streaming segments
app.get('/video/:filename/:resolution/:segment', cacheControl(86400),async (req, res) => {
const { filename, resolution, segment } = req.params;
const segmentPath = path.join('uploads', 'hls', filename, resolution, segment);
try {
await fs.access(segmentPath);
} catch (error) {
return res.status(404).json({ message: 'Segment not found' });
}
// Set appropriate headers for TS segments
res.setHeader('Content-Type', 'video/MP2T');
res.setHeader('Access-Control-Allow-Origin', '*');
const readStream = fsG.createReadStream(segmentPath);
readStream.pipe(res);
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});