File size: 11,628 Bytes
ec7d71c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db3d06
 
ec7d71c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const express = require("express");
const multer = require("multer");
const { TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const input = require("input");
const fs = require("fs");
const path = require("path");
const ffmpeg = require("fluent-ffmpeg");
const ffmpegPath = require("ffmpeg-static");

ffmpeg.setFfmpegPath(ffmpegPath);

const app = express();
const port = process.env.PORT || 7860;

const API_ID = 21436919;
const API_HASH = "6f289f8dccefd28e2d8077fd05568004";
const BOT_TOKEN = "8300006322:AAHnZf0xlt5mrzlaBjXYmVceZwo3xmwTZaA";
const BIN_CHANNEL = -1003569314973;
const SYSTEM_URL = process.env.SPACE_HOST 
    ? `https://${process.env.SPACE_ID.replace('/', '-')}-${process.env.SPACE_HOST}`
    : "http://localhost:7860";

const clients = [];
const upload = multer({ dest: "uploads/" });

app.use(express.json());

async function getNextClient() {
    const stringSession = new StringSession("");
    const client = new TelegramClient(stringSession, API_ID, API_HASH, {
        connectionRetries: 5,
    });
    
    await client.start({
        botAuthToken: BOT_TOKEN,
    });
    
    clients.push(client);
    return client;
}

async function startAllBots() {
    console.log("Starting Dulaksha Stream bot...");
    await getNextClient();
    console.log("Bot connected successfully!");
}

function getVideoResolution(inputPath) {
    return new Promise((resolve, reject) => {
        ffmpeg.ffprobe(inputPath, (err, metadata) => {
            if (err) return reject(err);
            const videoStream = metadata.streams.find(s => s.codec_type === 'video');
            if (!videoStream) return reject(new Error("No video stream found"));
            resolve({ width: videoStream.width, height: videoStream.height });
        });
    });
}

function convertToMultiQualityHLS(inputPath, outputDir) {
    return new Promise(async (resolve, reject) => {
        try {
            const resolution = await getVideoResolution(inputPath);
            const height = resolution.height;
            
            const qualities = [];
            if (height >= 2160) {
                qualities.push({ name: "1080p", height: 1080, bitrate: "5000k" });
                qualities.push({ name: "720p", height: 720, bitrate: "2800k" });
                qualities.push({ name: "480p", height: 480, bitrate: "1400k" });
            } else if (height >= 1080) {
                qualities.push({ name: "1080p", height: 1080, bitrate: "5000k" });
                qualities.push({ name: "720p", height: 720, bitrate: "2800k" });
                qualities.push({ name: "480p", height: 480, bitrate: "1400k" });
            } else if (height >= 720) {
                qualities.push({ name: "720p", height: 720, bitrate: "2800k" });
                qualities.push({ name: "480p", height: 480, bitrate: "1400k" });
            } else {
                qualities.push({ name: "480p", height: 480, bitrate: "1400k" });
            }
            
            const masterPlaylist = path.join(outputDir, "master.m3u8");
            let masterContent = "#EXTM3U\n#EXT-X-VERSION:3\n";
            
            const conversionPromises = qualities.map((quality, index) => {
                return new Promise((res, rej) => {
                    const qualityDir = path.join(outputDir, quality.name);
                    fs.mkdirSync(qualityDir, { recursive: true });
                    const playlistPath = path.join(qualityDir, "index.m3u8");
                    
                    ffmpeg(inputPath)
                        .outputOptions([
                            `-vf scale=-2:${quality.height}`,
                            `-c:v libx264`,
                            `-b:v ${quality.bitrate}`,
                            `-c:a aac`,
                            `-b:a 128k`,
                            `-start_number 0`,
                            `-hls_time 10`,
                            `-hls_list_size 0`,
                            `-f hls`
                        ])
                        .output(playlistPath)
                        .on("end", () => {
                            masterContent += `#EXT-X-STREAM-INF:BANDWIDTH=${parseInt(quality.bitrate) * 1000},RESOLUTION=${Math.floor(quality.height * 16 / 9)}x${quality.height}\n`;
                            masterContent += `${quality.name}/index.m3u8\n`;
                            res();
                        })
                        .on("error", rej)
                        .run();
                });
            });
            
            await Promise.all(conversionPromises);
            
            fs.writeFileSync(masterPlaylist, masterContent);
            resolve(masterPlaylist);
            
        } catch (err) {
            reject(err);
        }
    });
}

async function uploadHLSFiles(client, hlsDir, originalName) {
    const uploadedFiles = { qualities: {} };
    
    const masterPath = path.join(hlsDir, "master.m3u8");
    const masterResult = await client.sendFile(BIN_CHANNEL, {
        file: masterPath,
        caption: `πŸ“‚ ${originalName} - master.m3u8`,
        forceDocument: true
    });
    uploadedFiles.master = masterResult.id;
    
    const qualityDirs = fs.readdirSync(hlsDir).filter(f => 
        fs.statSync(path.join(hlsDir, f)).isDirectory()
    );
    
    for (const qualityDir of qualityDirs) {
        const qualityPath = path.join(hlsDir, qualityDir);
        const files = fs.readdirSync(qualityPath);
        
        const m3u8File = files.find(f => f.endsWith(".m3u8"));
        const tsFiles = files.filter(f => f.endsWith(".ts"));
        
        uploadedFiles.qualities[qualityDir] = { segments: [] };
        
        const m3u8Path = path.join(qualityPath, m3u8File);
        const m3u8Result = await client.sendFile(BIN_CHANNEL, {
            file: m3u8Path,
            caption: `πŸ“‚ ${originalName} - ${qualityDir}/index.m3u8`,
            forceDocument: true
        });
        uploadedFiles.qualities[qualityDir].index = m3u8Result.id;
        
        for (const tsFile of tsFiles) {
            const tsPath = path.join(qualityPath, tsFile);
            const tsResult = await client.sendFile(BIN_CHANNEL, {
                file: tsPath,
                caption: `πŸ“‚ ${originalName} - ${qualityDir}/${tsFile}`,
                forceDocument: true
            });
            uploadedFiles.qualities[qualityDir].segments.push({
                name: tsFile,
                id: tsResult.id
            });
        }
    }
    
    return uploadedFiles;
}

app.post("/upload", upload.single('file'), async (req, res) => {
    if (!req.file) return res.status(400).json({ error: "No file" });
    
    const originalName = req.file.originalname;
    const safeName = encodeURIComponent(originalName.replace(/\s+/g, '_'));
    const extension = path.extname(originalName).toLowerCase();
    const tempPath = req.file.path;
    const newPath = tempPath + extension;
    
    const videoExtensions = ['.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.webm'];
    const isVideo = videoExtensions.includes(extension);
    
    try {
        fs.renameSync(tempPath, newPath);
        const client = clients[0] || await getNextClient();
        if (!client) throw new Error("Server Busy");
        
        if (isVideo) {
            const hlsDir = path.join("uploads", `hls_${Date.now()}`);
            fs.mkdirSync(hlsDir, { recursive: true });
            
            await convertToMultiQualityHLS(newPath, hlsDir);
            const hlsFiles = await uploadHLSFiles(client, hlsDir, originalName);
            
            fs.rmSync(hlsDir, { recursive: true, force: true });
            if (fs.existsSync(newPath)) fs.unlinkSync(newPath);
            
            res.json({
                status: "success",
                stream_link: `${SYSTEM_URL}/stream/${hlsFiles.master}/${safeName}`,
                qualities: Object.keys(hlsFiles.qualities)
            });
        } else {
            const result = await client.sendFile(BIN_CHANNEL, {
                file: newPath,
                caption: `πŸ“‚ Uploaded: ${originalName}`,
                forceDocument: false
            });
            
            if (fs.existsSync(newPath)) fs.unlinkSync(newPath);
            
            res.json({
                status: "success",
                link: `${SYSTEM_URL}/stream/${result.id}`,
                stream_link: `${SYSTEM_URL}/stream/${result.id}/${safeName}`,
                download_link: `${SYSTEM_URL}/download/${result.id}/${safeName}`
            });
        }
    } catch (e) {
        console.error("Upload error:", e);
        res.status(500).json({ error: e.message });
        if (fs.existsSync(newPath)) fs.unlinkSync(newPath);
        else if (fs.existsSync(tempPath)) fs.unlinkSync(tempPath);
    }
});

app.get("/stream/:id/:filename?", async (req, res) => {
    try {
        const messageId = parseInt(req.params.id);
        const client = clients[0];
        if (!client) return res.status(503).send("Service unavailable");
        
        const messages = await client.getMessages(BIN_CHANNEL, { ids: [messageId] });
        const message = messages[0];
        
        if (!message || !message.media) {
            return res.status(404).send("File not found");
        }
        
        const fileSize = message.media.document.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;
            
            res.writeHead(206, {
                "Content-Range": `bytes ${start}-${end}/${fileSize}`,
                "Accept-Ranges": "bytes",
                "Content-Length": chunkSize,
                "Content-Type": "application/octet-stream"
            });
            
            const buffer = await client.downloadMedia(message, {
                offset: start,
                limit: chunkSize
            });
            res.end(buffer);
        } else {
            res.writeHead(200, {
                "Content-Length": fileSize,
                "Content-Type": "application/octet-stream"
            });
            
            const buffer = await client.downloadMedia(message);
            res.end(buffer);
        }
    } catch (e) {
        console.error("Stream error:", e);
        res.status(500).send("Error streaming file");
    }
});

app.get("/download/:id/:filename", async (req, res) => {
    try {
        const messageId = parseInt(req.params.id);
        const filename = decodeURIComponent(req.params.filename);
        const client = clients[0];
        if (!client) return res.status(503).send("Service unavailable");
        
        const messages = await client.getMessages(BIN_CHANNEL, { ids: [messageId] });
        const message = messages[0];
        
        if (!message || !message.media) {
            return res.status(404).send("File not found");
        }
        
        res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
        res.setHeader("Content-Type", "application/octet-stream");
        
        const buffer = await client.downloadMedia(message);
        res.end(buffer);
    } catch (e) {
        console.error("Download error:", e);
        res.status(500).send("Error downloading file");
    }
});

app.listen(port, () => {
    console.log(`Dulaksha Stream Server Running on Port ${port}`);
    startAllBots().catch(console.error);
});