A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
feat: add transcode progress tracking and update UI accordingly
Christian Feie97e601e1e16
diff --git a/client/components/video-element.js b/client/components/video-element.jsindex 4b788d7..9759c1c 100644--- a/client/components/video-element.js+++ b/client/components/video-element.js@@ -20,7 +20,7 @@ class VideoElement extends HTMLElement {}static get observedAttributes () {- return ['data-data', 'data-summarizing', 'data-downloading', 'data-progress']+ return ['data-data', 'data-summarizing', 'data-downloading', 'data-progress', 'data-transcode-progress']}attributeChangedCallback (name, _, newValue) {@@ -32,6 +32,10 @@ class VideoElement extends HTMLElement {this.querySelector('.action.download').innerText = this.downloadStartedText + ` (${this.dataset.progress}%)`return}+ if (name === 'data-transcode-progress' && this.querySelector('.action.download')) {+ this.querySelector('.action.download').innerText = '⚙️ Transcoding..' + ` (${this.dataset.transcodeProgress}%)`+ return+ }if (name === 'data-summarizing' && this.querySelector('.action.summarize')) {this.querySelector('.action.summarize').innerText = this.summaryStartedTextreturndiff --git a/client/main.js b/client/main.jsindex 77e2163..73d9452 100644--- a/client/main.js+++ b/client/main.js@@ -45,7 +45,7 @@ window.eventSource.onmessage = (message) => {$state.classList.add('updated')downloadLogTimeoutHandle = setTimeout(() => $state.classList.remove('updated'), 10000)- const $downloadLogLines = $state.querySelector(' .lines')+ const $downloadLogLines = $state.querySelector('.lines')$downloadLogLines.innerText += '\n' + data.line$downloadLogLines.scrollTop = $downloadLogLines.scrollHeightreturn@@ -58,6 +58,15 @@ window.eventSource.onmessage = (message) => {return}++ if (data.type === 'transcode-progress' && data.progress) {+ const videoId = data.progress.id+ ;[...document.querySelectorAll(`[data-video-id="${videoId}"]`)].forEach($video => {+ $video.dataset.transcodeProgress = data.progress.percent+ })++ return+ }if (data.type === 'new-videos' && data.videos) {const $videosContainer = document.querySelector('videos-container')if (!$videosContainer) returndiff --git a/lib/workers/ffmpeg.js b/lib/workers/ffmpeg.jsindex b7730cc..e374479 100644--- a/lib/workers/ffmpeg.js+++ b/lib/workers/ffmpeg.js@@ -2,7 +2,7 @@import { spawn } from 'child_process'import { parentPort, workerData } from 'worker_threads'-const { location, format } = workerData+const { location, format, duration } = workerDatafunction transcode (location, format) {return spawn('ffmpeg', [@@ -28,11 +28,58 @@ function transcode (location, format) {])}+// Helper: parse time like "00:00:44.40" to seconds+function parseTime (str) {+ if (!str) return null+ const m = String(str).trim().match(/^(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)$/)+ if (!m) return null+ const hours = parseInt(m[1], 10)+ const minutes = parseInt(m[2], 10)+ const seconds = parseFloat(m[3])+ return hours * 3600 + minutes * 60 + seconds+}+const process = transcode(location, format)function handleLine (raw, stream = 'stdout') {const line = String(raw).replace(/\r?\n/g, '')+parentPort.postMessage({ status: 'info', line, stream })++ // Try to parse ffmpeg progress lines like:+ // frame= 1112 fps=441 q=33.0 size= 2304KiB time=00:00:44.40 bitrate= 425.1kbits/s speed=17.6x elapsed=0:00:02.52+ const progressRegex = /frame=\s+(\d+)\s+fps=\s*(\d+)\s+q=\s*([\d.]+)\s+size=\s+([\d.]+KiB)\s+time=\s*([\d:]+(?:\.\d+)?)\s+bitrate=\s*([\d.]+kbits\/s)\s+speed=\s*([\d.]+x)/+ const m = line.match(progressRegex)+ if (m) {+ const frame = parseInt(m[1], 10)+ const fps = parseInt(m[2], 10)+ const timeRaw = m[5]+ const currentTime = parseTime(timeRaw)+ const percent = duration && currentTime != null ? Math.round((currentTime / duration) * 100) : null++ console.log('[ffmpeg worker] Progress:', {+ frame,+ fps,+ currentTime,+ duration,+ percent+ })++ parentPort.postMessage({+ status: 'progress',+ frame,+ fps,+ q: parseFloat(m[3]),+ size: m[4],+ time: timeRaw,+ currentTime,+ bitrate: m[6],+ speed: m[7],+ percent,+ line,+ stream+ })+ }}process.stdout.on('data', (data) => {diff --git a/lib/youtube.js b/lib/youtube.jsindex 39c0f34..f58a55e 100644--- a/lib/youtube.js+++ b/lib/youtube.js@@ -12,6 +12,17 @@ const fetchYoutubeHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'}+function parseDuration (durationStr) {+ if (!durationStr) return null+ const parts = durationStr.split(':').map(Number)+ if (parts.length === 2) {+ return parts[0] * 60 + parts[1]+ } else if (parts.length === 3) {+ return parts[0] * 3600 + parts[1] * 60 + parts[2]+ }+ return null+}+function runVideoInWorker (id, quality, callback) {return new Promise((resolve, reject) => {const worker = new Worker('./lib/workers/video.js', {@@ -45,10 +56,10 @@ function runVideoInWorker (id, quality, callback) {})}-function runFfmpegInWorker (location, format, callback) {+function runFfmpegInWorker (location, format, duration, callback) {return new Promise((resolve, reject) => {const worker = new Worker('./lib/workers/ffmpeg.js', {- workerData: { location, format }+ workerData: { location, format, duration }})worker.on('message', (message) => {@@ -61,6 +72,10 @@ function runFfmpegInWorker (location, format, callback) {if (message.status === 'info') {callback(message)}+ if (message.status === 'progress') {+ console.log('HERE WE ARE message', message)+ callback(message)+ }})worker.on('error', (error) => {@@ -105,7 +120,7 @@ export async function downloadVideo (id, repo, callback = () => {}) {const video = repo.updateVideo(id, { location, format })if (repo.getTranscodeVideosSetting() && video.location && video.format) {- await runFfmpegInWorker(video.location, video.format, (message) => {+ await runFfmpegInWorker(video.location, video.format, parseDuration(video.duration), (message) => {console.log(message.line)callback(message)})@@ -194,19 +209,25 @@ export async function getVideosFor (channelName) {function toInternalVideo (v) {if (!v || !v.richItemRenderer) returnconst data = v.richItemRenderer.content.videoRenderer- if (!data.viewCountText?.simpleText) return- // comments contain yt-dlp info downloaded with -J+ if (!data) {+ console.warn('Missing videoRenderer data:', v)+ return+ }++ // Ensure viewCountText exists before accessing simpleText+ const viewCount = data.viewCountText?.simpleText || '0 views'+return {channelName,- title: data.title?.runs[0].text, // .title- url: `https://www.youtube.com/watch?v=${data.videoId}`, // .url+ title: data.title?.runs[0]?.text || 'Untitled',+ url: `https://www.youtube.com/watch?v=${data.videoId}`,thumbnail: `https://img.youtube.com/vi/${data.videoId}/mq2.jpg`,- description: data.descriptionSnippet?.runs[0].text, // .description- id: data.videoId, // .id- publishedTime: data.publishedTimeText?.simpleText,+ description: data.descriptionSnippet?.runs[0]?.text || 'No description',+ id: data.videoId,+ publishedTime: data.publishedTimeText?.simpleText || 'Unknown time',publishedAt: parseRelativeTime(data.publishedTimeText?.simpleText),- viewCount: data.viewCountText?.simpleText, // .view_count- duration: data.lengthText?.simpleText // .duration_string+ viewCount,+ duration: data.lengthText?.simpleText || 'Unknown duration'}}}diff --git a/server/router/api.js b/server/router/api.jsindex 9ae3ddf..ed6a7aa 100644--- a/server/router/api.js+++ b/server/router/api.js@@ -113,11 +113,29 @@ async function downloadVideoHandler (req, res, repo, connections = [], state = {broadcastSSE(JSON.stringify({ type: 'new-videos', videos: [repo.getVideo(id)] }), connections)broadcastNewVideoOnce = true}+ // Log and broadcast info/progress messages from workersif (message.status === 'info') {+ console.log('[api] download info:', message.line)broadcastSSE(JSON.stringify({ type: 'download-log-line', line: message.line }), connections)}if (message.status === 'progress') {- broadcastSSE(JSON.stringify({ type: 'download-progress', progress: message }), connections)+ // Ensure progress messages include the video id so the client can map updates+ const progressPayload = Object.assign({}, message)+ if (!progressPayload.id) progressPayload.id = id+ // Add a phase marker so UI can distinguish download vs transcode+ if (!progressPayload.phase) progressPayload.phase = (progressPayload.frame || (progressPayload.time && progressPayload.time.indexOf(':') >= 0 && !progressPayload.total)) ? 'transcode' : 'download'+ try {+ console.log('[api] download progress:', { id: progressPayload.id, percent: progressPayload.percent, time: progressPayload.time, phase: progressPayload.phase })+ } catch (err) {+ console.log('[api] download progress (log error)', err && err.message)+ }+ // Broadcast separate event types for download vs transcode so client can+ // render different UI states.+ if (progressPayload.phase === 'transcode') {+ broadcastSSE(JSON.stringify({ type: 'transcode-progress', progress: progressPayload }), connections)+ } else {+ broadcastSSE(JSON.stringify({ type: 'download-progress', progress: progressPayload }), connections)+ }}}).then(() => {diff --git a/server/sse.js b/server/sse.jsindex 2f9f658..3d28ba0 100644--- a/server/sse.js+++ b/server/sse.js@@ -13,6 +13,20 @@ export function handleSSE (res, connections = []) {export function broadcastSSE (data, connections = []) {connections.forEach(connection => {if (!connection) return+ // Log broadcast attempts for debugging SSE delivery+ try {+ const parsed = (() => {+ try { return JSON.parse(data) } catch (err) { return null }+ })()+ if (parsed && parsed.type) {+ console.log(`[sse] broadcasting type=${parsed.type} to ${connections.length} connection(s)`)+ } else {+ console.log('[sse] broadcasting data to', connections.length, 'connection(s)')+ }+ } catch (err) {+ console.log('[sse] broadcast log error', err && err.message)+ }+const id = new Date().toISOString()connection.write('id: ' + id + '\n')connection.write('data: ' + data + '\n\n')