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: refactor video download and transcoding to use worker functions

Christian Fei67b041ce72e0
diff --git a/lib/workers/run-worker-video-download.js b/lib/workers/run-worker-video-download.js
new file mode 100644
index 0000000..d79d976
--- /dev/null
+++ b/lib/workers/run-worker-video-download.js
@@ -0,0 +1,34 @@
+import { Worker } from 'worker_threads'
+
+export function runWorkerVideoDownload (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)
+ }
+ if (message.status === 'progress') {
+ callback(message)
+ }
+ })
+
+ worker.on('error', (error) => {
+ reject(error)
+ })
+
+ worker.on('exit', (code) => {
+ if (code !== 0) {
+ reject(new Error(`Worker stopped with exit code ${code}`))
+ }
+ })
+ })
+}
diff --git a/lib/workers/run-worker-video-transcription.js b/lib/workers/run-worker-video-transcription.js
new file mode 100644
index 0000000..31e1e2b
--- /dev/null
+++ b/lib/workers/run-worker-video-transcription.js
@@ -0,0 +1,34 @@
+import { Worker } from 'worker_threads'
+
+export function runWorkerVideoTranscription (location, format, duration, callback) {
+ return new Promise((resolve, reject) => {
+ const worker = new Worker('./lib/workers/ffmpeg.js', {
+ workerData: { location, format, duration }
+ })
+
+ worker.on('message', (message) => {
+ if (message.status === 'done') {
+ resolve()
+ }
+ if (message.status === 'error') {
+ reject(message.error)
+ }
+ if (message.status === 'info') {
+ callback(message)
+ }
+ if (message.status === 'progress') {
+ callback(message)
+ }
+ })
+
+ worker.on('error', (error) => {
+ reject(error)
+ })
+
+ worker.on('exit', (code) => {
+ if (code !== 0) {
+ reject(new Error(`Worker stopped with exit code ${code}`))
+ }
+ })
+ })
+}
diff --git a/lib/workers/run-worker.js b/lib/workers/run-worker.js
new file mode 100644
index 0000000..57bacb6
--- /dev/null
+++ b/lib/workers/run-worker.js
@@ -0,0 +1,38 @@
+import { Worker } from 'worker_threads'
+
+export function runWorker (workerPath, workerData, callback) {
+ return new Promise((resolve, reject) => {
+ const worker = new Worker(workerPath, { workerData })
+
+ worker.on('message', (message) => {
+ if (message.status === 'done') {
+ resolve(message)
+ }
+ if (message.status === 'error') {
+ reject(message.error)
+ }
+ if (message.status === 'info') {
+ callback(message)
+ }
+ if (message.status === 'progress') {
+ callback(message)
+ }
+ })
+
+ worker.on('error', (error) => {
+ reject(error)
+ })
+
+ worker.on('exit', (code) => {
+ if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`))
+ })
+ })
+}
+
+export function runWorkerDownload (id, quality, callback) {
+ return runWorker('./lib/workers/video.js', { id, quality }, callback)
+}
+
+export function runWorkerTranscode (location, format, duration, callback) {
+ return runWorker('./lib/workers/ffmpeg.js', { location, format, duration }, callback)
+}
diff --git a/lib/youtube.js b/lib/youtube.js
index f58a55e..6ebc9d6 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -3,7 +3,7 @@ import spawn from 'nano-spawn'
import fs from 'fs'
import * as ytdlp from './yt-dlp.js'
import { execSync } from 'child_process'
-import { Worker } from 'worker_threads'
+import { runWorkerDownload, runWorkerTranscode } from './workers/run-worker.js'
export const ytUrlRegExp = /^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/(?:watch\?v=)?)([\w-]+)/
@@ -23,72 +23,6 @@ function parseDuration (durationStr) {
return null
}
-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)
- }
- if (message.status === 'progress') {
- callback(message)
- }
- })
-
- worker.on('error', (error) => {
- reject(error)
- })
-
- worker.on('exit', (code) => {
- if (code !== 0) {
- reject(new Error(`Worker stopped with exit code ${code}`))
- }
- })
- })
-}
-
-function runFfmpegInWorker (location, format, duration, callback) {
- return new Promise((resolve, reject) => {
- const worker = new Worker('./lib/workers/ffmpeg.js', {
- workerData: { location, format, duration }
- })
-
- worker.on('message', (message) => {
- if (message.status === 'done') {
- resolve()
- }
- if (message.status === 'error') {
- reject(message.error)
- }
- if (message.status === 'info') {
- callback(message)
- }
- if (message.status === 'progress') {
- console.log('HERE WE ARE message', message)
- callback(message)
- }
- })
-
- 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'))
@@ -100,7 +34,7 @@ export async function downloadVideo (id, repo, callback = () => {}) {
const quality = repo.getVideoQualitySetting()
let location, format
- await runVideoInWorker(id, quality, (message) => {
+ await runWorkerDownload(id, quality, (message) => {
if (message.status === 'info') {
console.log(message.line)
callback(message)
@@ -120,7 +54,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, parseDuration(video.duration), (message) => {
+ await runWorkerTranscode(video.location, video.format, parseDuration(video.duration), (message) => {
console.log(message.line)
callback(message)
})