Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

wip: worker download video

Christian Feia6aaa8b41247
diff --git a/lib/workers/video.js b/lib/workers/video.js
new file mode 100644
index 0000000..2e56e91
--- /dev/null
+++ b/lib/workers/video.js
@@ -0,0 +1,66 @@
+// videoWorker.js
+import fs from 'fs'
+import { spawn } from 'child_process'
+import { parentPort, workerData } from 'worker_threads'
+
+function getOptionalCookiesPath () {
+ if (fs.existsSync('/app/cookies.txt')) return '/app/cookies.txt'
+ if (fs.existsSync('./cookies.txt')) return './cookies.txt'
+}
+console.log('worker')
+const videoYTDLPArgs = quality => [
+ '--concurrent-fragments',
+ '10',
+ '--newline',
+ '--progress',
+ '--progress-delta',
+ '1',
+ '--sponsorblock-remove',
+ 'all,-filler',
+ '--merge-output-format',
+ 'mp4',
+ '-f',
+ `bestvideo[height<=${quality}]+bestaudio/best`,
+ '--check-formats',
+ '--verbose'
+]
+
+function video (id, quality) {
+ const cookiesPath = getOptionalCookiesPath()
+ const cookiesOption = cookiesPath ? ['--cookies', cookiesPath] : []
+ return spawn('yt-dlp', [
+ '-o',
+ `./data/videos/${id}.%(ext)s`,
+ ...cookiesOption,
+ ...videoYTDLPArgs(quality),
+ '--',
+ id
+ ])
+}
+
+const { id, quality } = workerData
+let location, format
+const process = video(id, quality)
+
+process.stdout.on('data', (data) => {
+ const line = data.toString().replace(/\n/, '')
+ 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/, '')
+ }
+ parentPort.postMessage({ status: 'info', line })
+})
+
+// process.stderr.on('data', (line) => {
+// console.error(`stderr: ${line}`)
+// })
+
+process.on('close', (code) => {
+ parentPort.postMessage({ status: 'done', code, location, format })
+})
+
+// Handle errors
+process.on('uncaughtException', (error) => {
+ console.error('Worker error:', error)
+ parentPort.postMessage({ status: 'error', error: error.message })
+})
diff --git a/lib/youtube.js b/lib/youtube.js
index 9e67944..b0df75d 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -4,6 +4,7 @@ import fs from 'fs'
import * as ytdlp from './yt-dlp.js'
import * as ffmpeg from './ffmpeg.js'
import { execSync } from 'child_process'
+import { Worker } from 'worker_threads'
export const ytUrlRegExp = /^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/(?:watch\?v=)?)([\w-]+)/
@@ -12,6 +13,35 @@ 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 runVideoInWorker (id, quality, callback) {
+ return new Promise((resolve, reject) => {
+ const worker = new Worker('./lib/workers/video.js', {
+ workerData: { id, quality }
+ })
+
+ worker.on('message', (message) => {
+ if (message.status === 'done') {
+ resolve({location: message.location, format: message.format})
+ }
+ if (message.status === 'error') {
+ reject(message.error)
+ }
+ if (message.status === 'info') {
+ callback(message.line)
+ }
+ })
+
+ worker.on('error', (error) => {
+ reject(error)
+ })
+
+ worker.on('exit', (code) => {
+ if (code !== 0) {
+ reject(new Error(`Worker stopped with exit code ${code}`))
+ }
+ })
+ })
+}
export async function downloadVideo (id, repo, callback = () => {}) {
return new Promise(async (resolve, reject) => {
if (isUnsupportedUrl(id)) return reject(new Error('unsupported url'))
@@ -22,15 +52,20 @@ export async function downloadVideo (id, repo, callback = () => {}) {
}
const quality = repo.getVideoQualitySetting()
let location, format
- ytdlp.subtitles(id).catch(err => console.error('Failed to download subtitles', id, err.message))
- for await (const line of ytdlp.video(id, quality)) {
+
+ await runVideoInWorker(id, quality, (line) => {
console.log(line)
callback(line)
- if (line.startsWith('[Merger] Merging formats into')) {
- location = line.substring(line.lastIndexOf(' ') + 1).replace(/"/g, '')
- format = location.substring(location.lastIndexOf('.') + 1)
- }
- }
+ })
+ .then((result) => {
+ console.log('Video processing completed', result)
+ location = result.location
+ format = result.format
+ })
+ .catch((error) => {
+ console.error(`Error: ${error.message}`)
+ })
+
const video = repo.updateVideo(id, { location, format })
if (repo.getTranscodeVideosSetting() && video.location && video.format) {