A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
refactor: usage of nano-spawn with args as array
Christian Fei08c5ff5d0ac1
diff --git a/lib/ffmpeg.js b/lib/ffmpeg.jsnew file mode 100644index 0000000..48c601e--- /dev/null+++ b/lib/ffmpeg.js@@ -0,0 +1,26 @@+import spawn from 'nano-spawn'++export function transcode(location, format) {+ console.log('trascoding video', location)+ 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)+ ])+}\ No newline at end of filediff --git a/lib/youtube.js b/lib/youtube.jsindex 61c6aa8..edbd91a 100644--- a/lib/youtube.js+++ b/lib/youtube.js@@ -1,7 +1,8 @@import spawn from 'nano-spawn'import fs from 'fs'import * as ytdlp from './yt-dlp.js'-import { exec, execSync } from 'child_process'+import * as ffmpeg from './ffmpeg.js'+import { execSync } from 'child_process';export const ytUrlRegExp = /^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/(?:watch\?v=)?)([\w-]+)/;@@ -25,22 +26,6 @@ export async function downloadVideo(id, repo, callback = () => {}) {format = location.substring(location.lastIndexOf('.') + 1)}}- if (process.env.SKIP_TRANSCODING !== 'true') {- try {- console.log(`transcoding video ${id} to h264`)- // exec(`ffmpeg -i ${location} -vcodec libx264 -acodec aac -movflags +faststart ${location.replace(format, `-h264.${format}`)}`, (error) => {- exec(`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, `-h264.${format}`)}`, (error) => {- if (!error) {- console.log(`succcessfully transcoded video ${id} to h264`)- return execSync(`mv ${location.replace(format, `-h264.${format}`)} ${location}`)- }- console.error(error)- })- } catch (error) {- console.error('failed to transcode video to h264', error)- }- }-if (!repo.getVideo(id)) {const video = await getVideo(id)video.location = location@@ -48,8 +33,18 @@ export async function downloadVideo(id, repo, callback = () => {}) {repo.upsertVideos([video])callback(`downloaded external video ${id}`)}+ const video = repo.updateVideo(id, {downloaded: true, location, format})- repo.updateVideo(id, {downloaded: true, location, format})+ if (process.env.SKIP_TRANSCODING !== 'true' && video.location && video.format) {+ for await (const line of ffmpeg.transcode(video.location, video.format)) {+ console.log(line)+ callback(line)+ }+ execSync(`mv ${location.replace(format, 'tmp.'+format)} ${location}`)++ console.log(`successfully transcoded video ${video.location}`)+ callback(`successfully transcoded video ${video.location}`)+ }normalizeSubtitleFiles(id)resolve(id)diff --git a/lib/yt-dlp.js b/lib/yt-dlp.jsindex 60812e5..b5bded5 100644--- a/lib/yt-dlp.js+++ b/lib/yt-dlp.js@@ -1,22 +1,56 @@import fs from 'fs'import spawn from 'nano-spawn'-const subtitlesYTDLPArgs = `--write-subs --write-auto-subs --sub-format vtt --convert-subs srt -k`-const videoYTDLPArgs = quality => `--concurrent-fragments 5 --newline --progress --progress-delta 1 --merge-output-format webm/mp4 -f bestvideo[height<=${quality}]+bestaudio/best[height<=${quality}] --sponsorblock-remove sponsor --embed-metadata`+const subtitlesYTDLPArgs = [+ '--write-subs',+ '--write-auto-subs',+ '--sub-format',+ 'vtt',+ '--convert-subs',+ 'srt',+ '-k'+]+const videoYTDLPArgs = quality => [+ '--concurrent-fragments',+ '5',+ '--newline',+ '--progress',+ '--progress-delta',+ '1',+ '--merge-output-format',+ 'webm/mp4',+ '-f',+ `bestvideo[height<=${quality}]+bestaudio/best[height<=${quality}]`,+ '--sponsorblock-remove',+ 'sponsor',+ '--embed-metadata'+]export function video(id, quality) {const cookiesPath = getOptionalCookiesPath()- let cookiesOption = cookiesPath ? `--cookies ${cookiesPath}` : ''- const commandArgs = `-o ./data/videos/${id}.%(ext)s ${cookiesOption} ${videoYTDLPArgs(quality)} ${subtitlesYTDLPArgs} -- ${id}`.split(/ +/)- console.log('running yt-dlp', commandArgs.join(' '))- return spawn('yt-dlp', commandArgs)+ let cookiesOption = cookiesPath ? ['--cookies', cookiesPath] : []+ return spawn('yt-dlp', [+ '-o',+ `./data/videos/${id}.%(ext)s`,+ ...cookiesOption,+ ...videoYTDLPArgs(quality),+ ...subtitlesYTDLPArgs,+ '--',+ id+ ])}export function subtitles(id) {const cookiesPath = getOptionalCookiesPath()- let cookiesOption = cookiesPath ? `--cookies ${cookiesPath}` : ''- const commandArgs = `-o ./data/videos/${id} --skip-download ${cookiesOption} ${subtitlesYTDLPArgs} -- ${id}`.split(/ +/)- console.log('running yt-dlp', commandArgs.join(' '))- return spawn('yt-dlp', commandArgs)+ let cookiesOption = cookiesPath ? ['--cookies', cookiesPath] : []+ return spawn('yt-dlp', [+ '-o',+ `./data/videos/${id}`,+ '--skip-download',+ ...cookiesOption,+ ...subtitlesYTDLPArgs,+ '--',+ id+ ])}function getOptionalCookiesPath() {