forked from codingstudios/yt-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (34 loc) · 1.27 KB
/
index.js
File metadata and controls
38 lines (34 loc) · 1.27 KB
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
const fs = require('fs');
const SoundCloud = require("soundcloud-scraper");
const client = new SoundCloud.Client();
const ffmpeg = require('fluent-ffmpeg');
const { exec } = require('child_process');
var content = ``;
const music = fs.readdirSync('./music').filter(file => file.endsWith('.mp3'));
const html = fs.readFileSync('./index.html');
const download = (video, file) => new Promise(async (resolve, reject) => {
try {
console.log(`Downloading ${file}`);
const song = await client.getSongInfo(video?.url);
const stream = await song.downloadProgressive();
const writer = stream.pipe(fs.createWriteStream(`./music/${file}.mp3`));
writer.on("finish", () => {
console.log(`Done ${video.title}`)
});
}catch(e) {
reject(e)
}
});
music.forEach(async (file) => {
try {
const video = await client.search(file.slice(0, -4));
if(!video[0]) throw new Error(`Unable to find ${file.slice(0, -4)}`);
content += `<audio controls src="./music/${file}"></audio>`;
fs.writeFileSync("./index.html", content)
const stats = fs.statSync(`./music/${file}`);
const fileSizeInBytes = stats.size;
const size = fileSizeInBytes / (1024*1024);
if(size > 1) return;
await download(video[0], file.slice(0,-4));
}catch(e) {console.log(e)}
})