Skip to content

Commit

Permalink
show ffmpeg progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Araxeus committed Mar 4, 2023
1 parent ad484ab commit 42baf9d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions plugins/downloader/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async function downloadSong(url, playlistFolder = undefined, trackId = undefined
}

if (!presets[config.get('preset')]) {
const fileBuffer = await toMP3(iterableStream, metadata, format.content_length, sendFeedback, increasePlaylistProgress);
const fileBuffer = await bufferToMP3(iterableStream, metadata, format.content_length, sendFeedback, increasePlaylistProgress);
writeFileSync(filePath, await writeID3(fileBuffer, metadata, sendFeedback));
} else {
const file = createWriteStream(filePath);
Expand Down Expand Up @@ -186,7 +186,7 @@ async function writeID3(buffer, metadata, sendFeedback) {
}
}

async function toMP3(stream, metadata, content_length, sendFeedback, increasePlaylistProgress = () => { }, extension = "mp3") {
async function bufferToMP3(stream, metadata, content_length, sendFeedback, increasePlaylistProgress = () => { }, extension = "mp3") {
const chunks = [];
let downloaded = 0;
let total = content_length;
Expand All @@ -196,7 +196,7 @@ async function toMP3(stream, metadata, content_length, sendFeedback, increasePla
const ratio = downloaded / total;
const progress = Math.floor(ratio * 100);
sendFeedback("Download: " + progress + "%", ratio);
increasePlaylistProgress(ratio);
increasePlaylistProgress(ratio * 0.15);
}
sendFeedback("Loading…", 2); // indefinite progress bar after download

Expand All @@ -214,6 +214,11 @@ async function toMP3(stream, metadata, content_length, sendFeedback, increasePla

sendFeedback("Converting…");

ffmpeg.setProgress(({ ratio }) => {
sendFeedback("Converting: " + Math.floor(ratio * 100) + "%", ratio);
increasePlaylistProgress(0.15 + ratio * 0.85);
});

await ffmpeg.run(
"-i",
safeVideoName,
Expand Down Expand Up @@ -307,6 +312,12 @@ async function downloadPlaylist(givenUrl) {
sendError("Error getting playlist info: make sure it isn't a private or \"Mixed for you\" playlist\n\n" + e);
return;
}
if (playlist.items.length === 0) sendError(new Error("Playlist is empty"));
if (playlist.items.length === 1) {
sendFeedback("Playlist has only one item, downloading it directly");
await downloadSong(playlist.items[0].url);
return;
}
let isAlbum = playlist.title.startsWith('Album - ');
if (isAlbum) {
playlist.title = playlist.title.slice(8);
Expand Down

0 comments on commit 42baf9d

Please sign in to comment.