eta and speed

This commit is contained in:
localhost 2024-03-29 19:30:04 +01:00
parent 7403330025
commit b805566045
2 changed files with 30 additions and 6 deletions

View File

@ -8,7 +8,4 @@ WORKDIR /usr/src/preservetube/backend
COPY . /usr/src/preservetube/backend
RUN yarn
RUN wget https://github.com/yt-dlp/yt-dlp-nightly-builds/releases/download/2024.03.19.232701/yt-dlp -q
RUN chmod +x yt-dlp
CMD ["node", "index.js"]

View File

@ -10,8 +10,17 @@ async function downloadVideo(url, ws, id) {
if (video.lengthSeconds > 1200) quality = '480p' // 20 minutes
if (video.lengthSeconds > 2100) quality = '360p' // 35 minutes
const downloadJson = await metadata.getVideoDownload(url, quality)
if (downloadJson.status == 'error') {
resolve({
message: 'Failed to request Youtube. Please retry...',
fail: true
})
}
let size = ''
let startTime = Date.now()
let prevBytes = 0
let speed = 0
const alreadyPrecentages = []
const download = wget.download(downloadJson.url, `./videos/${id}.webm`)
@ -24,7 +33,18 @@ async function downloadVideo(url, ws, id) {
if (alreadyPrecentages.includes((progress*100).toFixed(0))) return
alreadyPrecentages.push((progress*100).toFixed(0))
if (ws) ws.send(`DATA - [download] ${(progress*100).toFixed(2)}% of ${hr.fromBytes(size)}`)
const currentTime = Date.now()
const elapsedTime = (currentTime - startTime) / 1000
const currentBytes = progress * size
const bytesDownloaded = currentBytes - prevBytes
speed = bytesDownloaded / elapsedTime
prevBytes = currentBytes
const speedInMBps = speed / 1048576
const remainingBytes = size - currentBytes
const remainingTime = remainingBytes / speed
if (ws) ws.send(`DATA - [download] ${(progress*100).toFixed(2)}% of ${hr.fromBytes(size)} at ${speedInMBps.toFixed(2)} MB/s ETA ${secondsToTime(remainingTime.toFixed(0))}`)
})
download.on('error', err => {
@ -38,7 +58,7 @@ async function downloadVideo(url, ws, id) {
fail: false
})
} else {
reject({
resolve({
fail: true
})
}
@ -46,4 +66,11 @@ async function downloadVideo(url, ws, id) {
})
}
function secondsToTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
const formattedSeconds = remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds;
return `${minutes}:${formattedSeconds}`;
}
module.exports = { downloadVideo }