-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamer.js
100 lines (87 loc) · 3.02 KB
/
streamer.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
import { Client } from "discord.js-selfbot-v13";
import { streamLivestreamVideo, Streamer } from "@dank074/discord-video-stream";
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
// Setup logging
const logStream = fs.createWriteStream(`streamer_${new Date().toISOString().split('T')[0]}.log`, { flags: 'a' });
function log(message, level = 'INFO') {
const timestamp = new Date().toISOString();
const logMessage = `${timestamp} [${level}] ${message}\n`;
logStream.write(logMessage);
console.log(logMessage);
}
dotenv.config();
const TOKEN = process.env.TOKEN;
const HEIGHT = process.env.HEIGHT || 1920;
const WIDTH = process.env.WIDTH || 1080;
const FPS = process.env.FPS || 30;
const streamer = new Streamer(new Client());
// Queue to store incoming requests
let requestQueue = [];
// Process requests only after client is ready
streamer.client.on("ready", () => {
log(`Streamer client ready - Logged in as ${streamer.client.user.tag}`);
// Process any queued requests
processQueuedRequests();
});
function processQueuedRequests() {
while (requestQueue.length > 0) {
handleStreamRequest(requestQueue.shift());
}
}
async function handleStreamRequest(message) {
log(`Processing stream request for channel ${message.channel_id}`);
const channel = await streamer.client.channels.fetch(message.channel_id);
if (!channel) {
log(`Channel ${message.channel_id} not found`, 'ERROR');
return;
}
log(`Joining voice channel ${message.guild_id}/${message.channel_id}`);
await streamer.joinVoice(message.guild_id, message.channel_id);
log('Creating stream with default settings');
const streamUdpConn = await streamer.createStream({
width: WIDTH,
height: HEIGHT,
fps: FPS,
bitrateKbps: 1000,
maxBitrateKbps: 2000,
hardwareAcceleratedDecoding: true
});
log(`Starting video stream for URL: ${message.video_url}`);
try {
log('Starting video playback');
await streamLivestreamVideo(message.video_url, streamUdpConn, true);
log('Video stream started successfully');
} catch (e) {
log(`Stream playback error: ${e.message}`, 'ERROR');
log(e.stack, 'ERROR');
} finally {
log('Cleaning up stream resources');
streamer.stopStream();
streamer.leaveVoice();
}
}
process.stdin.on('data', async (data) => {
try {
const message = JSON.parse(data);
if (!streamer.client.isReady()) {
log('Client not ready, queueing request');
requestQueue.push(message);
} else {
await handleStreamRequest(message);
}
} catch (error) {
log(`Fatal error: ${error.message}`, 'ERROR');
log(error.stack, 'ERROR');
}
});
streamer.client.login(TOKEN).catch(error => {
log(`Login failed: ${error.message}`, 'ERROR');
log(error.stack, 'ERROR');
});
// Cleanup on exit
process.on('exit', () => {
log('Shutting down streamer client');
logStream.end();
});