Add loader and new formats
Some checks failed
Bump deps (only minor versions) / ci (push) Failing after 11s

This commit is contained in:
0d0 2025-02-23 04:55:09 +01:00
parent 204bceeee3
commit dcb7cfec27
6 changed files with 148 additions and 40 deletions

View file

@ -1,6 +1,9 @@
import { create } from 'youtube-dl-exec';
import { env } from '$env/dynamic/private';
import { spawn } from 'node:child_process';
import supportedFormats from '$lib/common/supportedFormats.json';
import { mimeTypeMap } from '$lib/server/helpers';
const YTDLP_PATH: string = env.YTDLP_PATH as string;
export const ytdl = create(YTDLP_PATH);
@ -21,19 +24,27 @@ export async function getYouTubeMetadata(link: string) {
* Streams the YouTube video/audio using youtube-dl-exec
*/
export function streamYouTube(link: string, format: string): ReadableStream<Uint8Array> {
const mimeType: string | undefined = mimeTypeMap.get(format)
if (!mimeType) {
throw new Error("Unsupported format");
}
return new ReadableStream({
start(controller) {
const args = [
'-o',
'-',
format === 'mp3' ? '--embed-metadata' : '',
'--format',
format === 'mp3' ? 'bestaudio' : 'best',
'--audio-format',
format === 'mp3' ? 'mp3' : '',
'--no-playlist'
].filter(Boolean);
if(mimeType?.includes('audio')) {
args.push(...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format])
} else if (mimeType.includes('video')) {
args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format])
}
console.info(`yt-dlp ${args.join(' ')} ${link}`)
const process = spawn('yt-dlp', [...args, link], { stdio: ['ignore', 'pipe', 'ignore'] });
process.stdout.on('data', (chunk) => controller.enqueue(chunk));