-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
91 lines (72 loc) · 2.16 KB
/
main.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
const express = require('express');
const path = require('path');
const fs = require('fs');
const { STATUS_CODES } = require('http');
const ffprobe = require('ffprobe');
const ffprobeStatic = require('ffprobe-static');
const app = express();
var stream_info = {
audio: '',
title: '',
author: '',
time: 0,
duration: 0,
isPlaying: false
}
app.use(express.static(path.join(__dirname, 'application/build')));
app.use('/obs', express.static(path.join(__dirname, 'OBS/')));
app.use('/audio', express.static(path.join(__dirname, 'audio')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'application/build/index.html'));
});
app.get('/obs', (req, res) => {
res.sendFile(path.join(__dirname, 'OBS/index.html'));
});
app.get('/stream', (req, res) => {
res.json(stream_info);
})
app.get('/next', async (req, res) => {
await NextSong();
res.json(stream_info);
});
app.put('/update_stream/:time', (req, res)=>{
stream_info.time = req.params.time;
console.log(req.params.time);
res.sendStatus(200);
})
async function NextSong() {
var audio_files = [];
fs.readdirSync(path.join(__dirname, '/audio')).forEach(file => {
console.log(file);
audio_files.push(file);
});
const audio_file = audio_files[Math.floor(Math.random()*audio_files.length)];
const basename = path.basename(audio_file).replace(/.mp3/, '');
const author = basename.split('.')[0];
const title = basename.split('.')[1];
let info = await ffprobe(`./audio/${audio_file}`, { path: ffprobeStatic.path });
stream_info.duration = parseInt(info.streams[0].duration);
stream_info.audio = `http://localhost:1337/audio/${basename}.mp3`;
stream_info.title = title;
stream_info.author = author;
stream_info.time = 0;
stream_info.isPlaying = true;
}
function Tick() {
if(stream_info.isPlaying)
{
if(stream_info.time >= stream_info.duration)
{
NextSong();
}
else if(stream_info.isPlaying === true)
{
stream_info.time++;
}
}
console.log(stream_info);
setTimeout(Tick, 1000);
}
NextSong();
Tick();
app.listen(1337);