-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
154 lines (146 loc) · 5.1 KB
/
Copy pathpreload.js
File metadata and controls
154 lines (146 loc) · 5.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
process.on("uncaughtException",(e)=>{console.error(e);});
const ytdl=require("@distube/ytdl-core");
const ffmpegPath=require("@ffmpeg-installer/ffmpeg").path;
const ffmpeg=require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegPath);
const fs=require("fs");
const ytpl=require("ytpl");
const os=require("os");
const path=require("path");
window.addEventListener("DOMContentLoaded",()=>{
var simultaneousLimit=10;
var playlistLimit=Infinity;
var playlistUrl="";
var downloadPath="downloads/";
var downloaded=0;
var total=0;
var downloadedInChunk=0;
var playlistName="";
var songs=[];
var chunks=[];
var count=1;
var chunkDownloadStarted=false;
var currentChunkTotal=0;
var retryCount=0;
var downloading=false;
async function download(url,output){
var stream=ytdl(url,{filter:"audioonly",fmt:"mp3",highWaterMark:1<<62,bitrate:256,quality:"highestaudio"});
ffmpeg(stream).audioBitrate(256).save(`${downloadPath}/${output}`).on("end",()=>{
downloaded++;
downloadedInChunk++;
var percentage=((downloaded/total)*100).toFixed(2);
document.querySelector("#progress div").style.width=`${percentage}%`;
document.querySelector("#progress h3").innerText=`${downloaded}/${total} (${percentage}%)`;
if(downloadedInChunk>=simultaneousLimit){chunkDownloadStarted=false;}
}).on("error",err=>{
console.log(err);
retryCount++;
if(retryCount>3){
downloaded++;
downloadedInChunk++;
}else{
download(url,output);
}
});
}
async function downloadPlaylist(url){
if(songs.length<1){document.querySelector("#progress h3").innerText=`No songs on the list!`;return;}
downloading=true;
downloadButton.disabled=true;
downloadButton.innerText="Downloading";
downloaded=0;
document.querySelector("#progress div").style.width=`0%`;
document.querySelector("#progress h3").innerText=`${downloaded}/${total} (0%)`;
downloadPathElement.disabled=true;
playlistUrlElement.disabled=true;
singleUrlElement.disabled=true;
chunks=[];
count=1;
for(var i=0;i<songs.length;i+=simultaneousLimit){chunks.push(songs.slice(i,i+simultaneousLimit));}
nextChunk();
}
function finished(){
downloading=false;
downloadButton.disabled=false;
downloadButton.innerText="Download";
document.querySelector("#progress h3").innerText=`Done!`;
document.querySelector("#progress div").style.width=`100%`;
downloadPathElement.disabled=false;
playlistUrlElement.disabled=false;
singleUrlElement.disabled=false;
}
function nextChunk(){
if(downloadedInChunk<currentChunkTotal&&chunkDownloadStarted){setTimeout(nextChunk,100);}else{
if(chunks.length<1){finished();return;}
downloadedInChunk=0;
retryCount=0;
chunkDownloadStarted=true;
var chunk=chunks[0];
currentChunkTotal=chunk.length;
chunks.shift();
chunk.forEach(async song=>{
download(song.url,`${"0".repeat(total.toString().length-count.toString().length)}${count}. ${`${song.title} - ${song.author}`.replaceAll(" ","_").match(/[a-zA-Z0-9_-]+/g).join("").substring(0,122)}.mp3`);
count++;
});
nextChunk();
}}
async function loadPlaylist(url){
var playlist=await ytpl(url,{limit:playlistLimit});
playlistName=playlist.title;
songs=[];
playlist.items.forEach(song=>{songs.push({title:song.title,url:song.shortUrl,author:song.author.name});});
total=playlist.items.length;
renderPlaylist();
}
async function removeSong(id){
songs.splice(id,1);
total--;
renderPlaylist();
}
async function renderPlaylist(){
songsElement.innerHTML=`<legend><h4 id="details">${playlistName} (${total} song${total>1?"s":""})</h4></legend>`;
var counter=0;
songs.forEach(song=>{
var element=document.createElement("h2");
songsElement.appendChild(element);
element.classList.add("song");
element.id=counter;
element.innerHTML=`${song.title}<span>${song.author}</span>`;
element.addEventListener("dblclick",e=>{if(downloading)return;removeSong(parseInt(e.target.id))});
counter++;
});
}
async function changeDownloadFolder(path){
if(!(await fs.existsSync(path))){try{await fs.mkdirSync(path);}catch(e){}}
if(await fs.existsSync(path)){
downloadPath=path;
}
downloadPathElement.value=downloadPath;
}
async function showPercentage(value){
document.querySelector("#progress>div").style.width=`${value}%`;
document.querySelector("#progress>h3").innerText=`${value}%`;
}
var downloadPathElement=document.querySelector("#downloadPath");
var playlistUrlElement=document.querySelector("#playlistUrl");
var singleUrlElement=document.querySelector("#singleUrl");
var songsElement=document.querySelector("#songs");
var downloadButton=document.querySelector("#download");
downloadPathElement.value=downloadPath;
downloadPathElement.addEventListener("change",e=>{changeDownloadFolder(downloadPathElement.value);});
playlistUrlElement.addEventListener("change",e=>{loadPlaylist(playlistUrlElement.value);});
singleUrlElement.addEventListener("keyup",async e=>{
if(e.keyCode!=13)return;
if(singleUrlElement.value=="")return;
var url=singleUrlElement.value;
singleUrlElement.value="";
var song=await ytdl.getInfo(url);
songs.push({title:song.videoDetails.title,url:song.videoDetails.video_url,author:song.videoDetails.ownerChannelName});
total++;
renderPlaylist();
});
downloadButton.addEventListener("click",e=>{downloadPlaylist();});
changeDownloadFolder(path.join(os.homedir(),"Desktop","yt-download"));
loadPlaylist(playlistUrlElement.value);
playlistUrlElement.focus();
});