Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: show percentage while downloading video

Christian Feiee05d9645637
diff --git a/client/components/video-element.js b/client/components/video-element.js
index b9803e6..4b788d7 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -20,19 +20,25 @@ class VideoElement extends HTMLElement {
}
static get observedAttributes () {
- return ['data-data', 'data-summarizing', 'data-downloading']
+ return ['data-data', 'data-summarizing', 'data-downloading', 'data-progress']
}
attributeChangedCallback (name, _, newValue) {
- if (name === 'data-data') {
- this.video = JSON.parse(this.dataset.data)
- this.render()
- }
if (name === 'data-downloading' && this.querySelector('.action.download')) {
this.querySelector('.action.download').innerText = this.downloadStartedText
+ return
+ }
+ if (name === 'data-progress' && this.querySelector('.action.download')) {
+ this.querySelector('.action.download').innerText = this.downloadStartedText + ` (${this.dataset.progress}%)`
+ return
}
if (name === 'data-summarizing' && this.querySelector('.action.summarize')) {
this.querySelector('.action.summarize').innerText = this.summaryStartedText
+ return
+ }
+ if (name === 'data-data') {
+ this.video = JSON.parse(this.dataset.data)
+ this.render()
}
}
@@ -47,6 +53,7 @@ class VideoElement extends HTMLElement {
this.dataset.summarized = this.video.summary ? 'true' : 'false'
this.dataset.downloaded = this.video.downloaded ? 'true' : 'false'
this.dataset.ignored = this.video.ignored ? 'true' : 'false'
+ this.dataset.progress = this.video.progress
this.innerHTML = /* html */`
${this.video.downloaded
diff --git a/client/main.js b/client/main.js
index 1ff277f..77e2163 100644
--- a/client/main.js
+++ b/client/main.js
@@ -50,6 +50,14 @@ window.eventSource.onmessage = (message) => {
$downloadLogLines.scrollTop = $downloadLogLines.scrollHeight
return
}
+ if (data.type === 'download-progress' && data.progress) {
+ const videoId = data.progress.id
+ ;[...document.querySelectorAll(`[data-video-id="${videoId}"]`)].forEach($video => {
+ $video.dataset.progress = data.progress.percent
+ })
+
+ return
+ }
if (data.type === 'new-videos' && data.videos) {
const $videosContainer = document.querySelector('videos-container')
if (!$videosContainer) return
diff --git a/lib/workers/video.js b/lib/workers/video.js
index 2e56e91..915c5d4 100644
--- a/lib/workers/video.js
+++ b/lib/workers/video.js
@@ -41,14 +41,74 @@ function video (id, quality) {
const { id, quality } = workerData
let location, format
const process = video(id, quality)
+// Helper: parse human readable sizes like "4.99MiB", "1023KiB", "123B"
+function parseSize (str) {
+ if (!str) return null
+ const m = String(str).trim().match(/^([\d.]+)\s*(B|KiB|MiB|GiB|KB|MB|GB)?$/i)
+ if (!m) return null
+ const val = parseFloat(m[1])
+ const unit = (m[2] || 'B').toUpperCase()
+ switch (unit) {
+ case 'B': return val
+ case 'KB': return val * 1e3
+ case 'KIB': return val * 1024
+ case 'MB': return val * 1e6
+ case 'MIB': return val * 1024 * 1024
+ case 'GB': return val * 1e9
+ case 'GIB': return val * 1024 * 1024 * 1024
+ default: return val
+ }
+}
-process.stdout.on('data', (data) => {
- const line = data.toString().replace(/\n/, '')
+// Regex to match yt-dlp download progress lines like:
+// [download] 12.3% of 50.00MiB at 1.23MiB/s ETA 00:35
+// [download] 100% of 4.99MiB in 00:04
+const downloadProgressRegex = /\[download\]\s+([\d.]+)%\s+of\s+([^\s]+)(?:\s+at\s+([^\s]+)\/s\s+ETA\s+([^\s]+)|\s+in\s+([^\s]+))?/
+
+function handleLine (raw, stream = 'stdout') {
+ const line = String(raw).replace(/\r?\n/g, '')
+
+ parentPort.postMessage({ status: 'info', line })
+ // Detect merger output like before
if (line && line.startsWith('[Merger] Merging formats into')) {
location = line.substring(line.lastIndexOf(' ') + 1).replace(/"/g, '').replace(/\n/, '')
format = location.substring(location.lastIndexOf('.') + 1).replace(/\n/, '')
+ return
}
- parentPort.postMessage({ status: 'info', line })
+
+ // Try to parse download progress
+ const m = line.match(downloadProgressRegex)
+ if (m) {
+ const percent = parseFloat(m[1])
+ const totalRaw = m[2]
+ const speedRaw = m[3] || null
+ const eta = m[4] || m[5] || null
+ const total = parseSize(totalRaw)
+ const downloaded = (total != null && !Number.isNaN(percent)) ? Math.round(total * percent / 100) : null
+
+ parentPort.postMessage({
+ status: 'progress',
+ id,
+ percent,
+ downloaded,
+ total,
+ totalRaw,
+ speed: speedRaw,
+ eta,
+ line,
+ stream
+ })
+ }
+}
+
+process.stdout.on('data', (data) => {
+ // yt-dlp may emit multiple lines per chunk; split and handle each
+ String(data).split(/\r?\n/).forEach((l) => { if (l) handleLine(l, 'stdout') })
+})
+
+// yt-dlp often outputs progress to stderr; listen there too
+process.stderr.on('data', (data) => {
+ String(data).split(/\r?\n/).forEach((l) => { if (l) handleLine(l, 'stderr') })
})
// process.stderr.on('data', (line) => {
diff --git a/lib/youtube.js b/lib/youtube.js
index a8a6bec..a320d20 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -27,7 +27,10 @@ function runVideoInWorker (id, quality, callback) {
reject(message.error)
}
if (message.status === 'info') {
- callback(message.line)
+ callback(message)
+ }
+ if (message.status === 'progress') {
+ callback(message)
}
})
@@ -53,9 +56,13 @@ export async function downloadVideo (id, repo, callback = () => {}) {
const quality = repo.getVideoQualitySetting()
let location, format
- await runVideoInWorker(id, quality, (line) => {
- console.log(line)
- callback(line)
+ await runVideoInWorker(id, quality, (message) => {
+ if (message.status === 'info') {
+ console.log(message.line)
+ callback(message)
+ } else {
+ callback(message)
+ }
})
.then((result) => {
console.log('Video processing completed', result)
diff --git a/server/router/api.js b/server/router/api.js
index 129a783..9ae3ddf 100644
--- a/server/router/api.js
+++ b/server/router/api.js
@@ -108,12 +108,17 @@ async function downloadVideoHandler (req, res, repo, connections = [], state = {
state.downloading[id] = { lines: [] }
let broadcastNewVideoOnce = false
- downloadVideo(id, repo, (line) => {
+ downloadVideo(id, repo, (message) => {
if (external && !broadcastNewVideoOnce) {
broadcastSSE(JSON.stringify({ type: 'new-videos', videos: [repo.getVideo(id)] }), connections)
broadcastNewVideoOnce = true
}
- broadcastSSE(JSON.stringify({ type: 'download-log-line', line }), connections)
+ if (message.status === 'info') {
+ broadcastSSE(JSON.stringify({ type: 'download-log-line', line: message.line }), connections)
+ }
+ if (message.status === 'progress') {
+ broadcastSSE(JSON.stringify({ type: 'download-progress', progress: message }), connections)
+ }
})
.then(() => {
const video = repo.getVideo(id)