Add ytdlp binary
This commit is contained in:
parent
ce59825161
commit
7789c0921c
4 changed files with 149 additions and 117 deletions
46
src/lib/server/ytdlp.ts
Normal file
46
src/lib/server/ytdlp.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { create } from 'youtube-dl-exec';
|
||||
import { YTDLP_PATH } from '$env/static/private';
|
||||
import { spawn } from 'node:child_process';
|
||||
|
||||
export const ytdl = create(YTDLP_PATH);
|
||||
|
||||
/**
|
||||
* Fetch YouTube metadata (title, uploader/artist)
|
||||
*/
|
||||
export async function getYouTubeMetadata(link: string) {
|
||||
return await ytdl(link, {
|
||||
dumpSingleJson: true,
|
||||
noCheckCertificates: true,
|
||||
noWarnings: true,
|
||||
preferFreeFormats: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams the YouTube video/audio using youtube-dl-exec
|
||||
*/
|
||||
export function streamYouTube(link: string, format: string): ReadableStream<Uint8Array> {
|
||||
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);
|
||||
|
||||
const process = spawn('yt-dlp', [...args, link], { stdio: ['ignore', 'pipe', 'ignore'] });
|
||||
|
||||
process.stdout.on('data', (chunk) => controller.enqueue(chunk));
|
||||
process.stdout.on('end', () => controller.close());
|
||||
process.stdout.on('error', (err) => {
|
||||
console.error('Stream error:', err);
|
||||
controller.error(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue