forked from codingstudios/yt-player
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (131 loc) · 5.22 KB
/
index.js
File metadata and controls
139 lines (131 loc) · 5.22 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
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
const PLAYLIST_ID = "PLPEUDHLKro0oqkwnw3I-ZhqeYGnNTkOmu";
const ffmpeg = require('fluent-ffmpeg');
const axios = require('axios');
const getSize = require('get-folder-size');
const { performance } = require('perf_hooks');
const ytdl = require('ytdl-core');
const search = require("youtube-sr").default;
const fs = require('fs');
const { Collection } = require('@discordjs/collection');
const collection = new Set();
const collection2 = new Collection();
const path = require('path');
const downloaded = [];
const cannot = [];
const content = [];
const playlist = [];
const thisDownload = [];
const all = [];
var trackNumber = 0;
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getAudio = (video) => new Promise((resolve, reject) => {
var stream = ytdl(video?.url, { filter: 'audioonly' });
var file = fs.createWriteStream(`./music/${video?.title.split("/").join(" ").split(".").join(" ")}.mp3`);
ffmpeg(stream)
.format('mp3')
.save(file)
.on('end', () => {
collection.add(`${video?.title}`);
resolve(`Done ${video?.title}`);
})
});
async function run() {
var jobStartTime = performance.now();
const dir = fs.readdirSync('./music').filter(file => file.endsWith('.mp3'));
for (i in dir) {
// fs.unlinkSync(`./music/${dir[i]}`);
collection.add(dir[i].slice(0, -4));
downloaded.push(dir[i].slice(0, -4));
}
await wait(5000);
const data = (await search.getPlaylist(PLAYLIST_ID).then(playlist => playlist.fetch()));
if(!Array.isArray(data?.videos)) throw new Error("No videos found");
var videos = data.videos;
var startDownloadTime = performance.now();
for(i in videos) {
all.push(videos[i].title);
collection2.set(videos[i].title, videos[i].url);
if(!collection.has(videos[i]?.title.split("/").join(" ").split(".").join(" "))) {
try {
console.log(await getAudio(videos[i]));
downloaded.push(videos[i]?.title);
thisDownload.push(videos[i]?.title);
console.log(downloaded.length, videos.length, i)
}catch(e) {console.log(e)}
}
}
var endDownloadTime = performance.now();
console.log(all.length, downloaded.length);
const music = fs.readdirSync('./music').filter(file => file.endsWith('.mp3'));
for(i in music){
trackNumber++;
content.push({
title: `${encodeURIComponent(music[i].slice(0, -4))}`,
track: `./music/${encodeURIComponent(music[i])}`,
trackNumber
})
// await lyrics(music[i].slice(0, -4));
}
for (let i = 0; i < content.length; i += 20) {
playlist.push(content.slice(i, i + 20));
}
playlist.forEach((e, i) => {
fs.writeFileSync(`./playlist/playlist-${i}.json`, JSON.stringify({
page: i,
list: e,
totalPages: playlist.length
}));
})
fs.writeFileSync(`./playlist/playlist.json`, JSON.stringify(content));
const statsPlaylist = fs.statSync("./playlist/playlist.json");
function bts(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
var jobEndTime = performance.now();
getSize('./music', (err, size) =>
fs.writeFileSync(`./stats.md`,
`## Github Player Stats\n\n#### **Total audio files**: ${music.length}\n\n#### **Total size of audio files**: ${bts(size)}\n\n#### **Playlist index file size**: ${bts(statsPlaylist.size)}\n\n#### **Current download**: ${thisDownload.length}\n\n#### **Download time consumed**: ${msTmin(endDownloadTime - startDownloadTime)} minutes\n\n#### **Total job time consumed**: ${msTmin(jobEndTime - jobStartTime)} minutes`
));
};
run();
const lyricsFinder = require('./lyrics.js');
async function lyrics(title) {
if(fs.existsSync(`./lyrics/${title}.txt`))return;
const query = title.toLowerCase()
.split("lyrics").join("")
.split("official").join("")
.split("mv").join("")
.split("video").join("")
.split("audio").join("")
.split("(").join("").split(")").join("")
.split("[").join("").split("]").join("")
.split("【").join("").split("】").join("")
.split("「").join("").split("」").join("")
.split("《").join("").split("》").join("")
.split("♪").join("").split("動態歌詞").join("")
.split("cover").join("").split("mp3").join("")
.split("ost").join("").split("歌词版").join("")
.split("4k").join("").replace(/\s+/g,' ').trim()
.split("-");
try {
if(!query) return;
const lyrics = await lyricsFinder(`${query.join(" ")}`).catch(async () => await lyricsFinder(`${query.join(" ")}`));
if(!lyrics) return;
console.log(`Lyrics: ${query.join(" ")}`);
fs.writeFileSync(`./lyrics/${title}.txt`, `${lyrics}`);
}catch(e) {
console.log(`${query.join(" ")} was not downloaded`)
}
}
function msTmin(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
process.on('uncaughtException', async function (err) {
console.log(err)
});