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: implement ffmpeg worker for video transcoding

Christian Feiaeeef99df16d
diff --git a/lib/workers/ffmpeg.js b/lib/workers/ffmpeg.js
new file mode 100644
index 0000000..b7730cc
--- /dev/null
+++ b/lib/workers/ffmpeg.js
@@ -0,0 +1,54 @@
+// ffmpegWorker.js
+import { spawn } from 'child_process'
+import { parentPort, workerData } from 'worker_threads'
+
+const { location, format } = workerData
+
+function transcode (location, format) {
+ return spawn('ffmpeg', [
+ '-i',
+ location,
+ '-c:v',
+ 'libx264',
+ '-preset',
+ 'veryfast',
+ '-crf',
+ '28',
+ '-x264-params',
+ 'opencl=true',
+ '-vf',
+ 'format=yuv420p',
+ '-profile:v',
+ 'main',
+ '-movflags',
+ '+faststart',
+ '-c:a',
+ 'copy',
+ location.replace(format, 'tmp.' + format)
+ ])
+}
+
+const process = transcode(location, format)
+
+function handleLine (raw, stream = 'stdout') {
+ const line = String(raw).replace(/\r?\n/g, '')
+ parentPort.postMessage({ status: 'info', line, stream })
+}
+
+process.stdout.on('data', (data) => {
+ String(data).split(/\r?\n/).forEach((l) => { if (l) handleLine(l, 'stdout') })
+})
+
+process.stderr.on('data', (data) => {
+ String(data).split(/\r?\n/).forEach((l) => { if (l) handleLine(l, 'stderr') })
+})
+
+process.on('close', (code) => {
+ parentPort.postMessage({ status: 'done', code })
+})
+
+// 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 a320d20..39c0f34 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -2,7 +2,6 @@
import spawn from 'nano-spawn'
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'
@@ -45,6 +44,36 @@ function runVideoInWorker (id, quality, callback) {
})
})
}
+
+function runFfmpegInWorker (location, format, callback) {
+ return new Promise((resolve, reject) => {
+ const worker = new Worker('./lib/workers/ffmpeg.js', {
+ workerData: { location, format }
+ })
+
+ worker.on('message', (message) => {
+ if (message.status === 'done') {
+ resolve()
+ }
+ if (message.status === 'error') {
+ reject(message.error)
+ }
+ if (message.status === 'info') {
+ 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'))
@@ -76,10 +105,10 @@ export async function downloadVideo (id, repo, callback = () => {}) {
const video = repo.updateVideo(id, { location, format })
if (repo.getTranscodeVideosSetting() && video.location && video.format) {
- for await (const line of ffmpeg.transcode(video.location, video.format)) {
- console.log(line)
- callback(line)
- }
+ await runFfmpegInWorker(video.location, video.format, (message) => {
+ console.log(message.line)
+ callback(message)
+ })
execSync(`mv ${location.replace(format, 'tmp.' + format)} ${location}`)
console.log(`successfully transcoded video ${video.location}`)