it's cool that we have format
All checks were successful
Bump deps (only minor versions) / ci (push) Successful in 18s
All checks were successful
Bump deps (only minor versions) / ci (push) Successful in 18s
This commit is contained in:
parent
760812c692
commit
652208aa57
6 changed files with 69 additions and 60 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"mp3": "audio/mpeg",
|
||||
"opus": "audio/ogg",
|
||||
"wav": "audio/wav",
|
||||
"mp4": "video/mp4"
|
||||
}
|
||||
"mp3": "audio/mpeg",
|
||||
"opus": "audio/ogg",
|
||||
"wav": "audio/wav",
|
||||
"mp4": "video/mp4"
|
||||
}
|
||||
|
|
|
@ -2,18 +2,18 @@ import formats from '$lib/common/supportedFormats.json';
|
|||
import winston from 'winston';
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: 'debug',
|
||||
format: winston.format.json(),
|
||||
transports: [new winston.transports.Console()],
|
||||
level: 'debug',
|
||||
format: winston.format.json(),
|
||||
transports: [new winston.transports.Console()]
|
||||
});
|
||||
const formatMime = new Map(Object.entries(formats))
|
||||
const formatMime = new Map(Object.entries(formats));
|
||||
export const isURLValid = (url: string) => {
|
||||
try {
|
||||
new URL(url)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
try {
|
||||
new URL(url);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
export const mimeTypeMap = formatMime;
|
||||
return true;
|
||||
};
|
||||
export const mimeTypeMap = formatMime;
|
||||
|
|
|
@ -25,45 +25,46 @@ export async function getYouTubeMetadata(link: string) {
|
|||
*/
|
||||
export function streamYouTube(link: string, format: string): ReadableStream<Uint8Array> {
|
||||
logger.debug(`Starting to stream: ${link}`);
|
||||
const mimeType: string | undefined = mimeTypeMap.get(format)
|
||||
const mimeType: string | undefined = mimeTypeMap.get(format);
|
||||
|
||||
if (!mimeType) {
|
||||
throw new Error("Unsupported format");
|
||||
throw new Error('Unsupported format');
|
||||
}
|
||||
logger.debug(`Given format is compatible: ${mimeType}`);
|
||||
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
const args = [
|
||||
'--no-write-thumbnail',
|
||||
'-o',
|
||||
'-',
|
||||
].filter(Boolean);
|
||||
const args = ['--no-write-thumbnail', '-o', '-'].filter(Boolean);
|
||||
|
||||
if (mimeType?.includes('audio')) {
|
||||
args.push(...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format])
|
||||
args.push(
|
||||
...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format]
|
||||
);
|
||||
} else if (mimeType.includes('video')) {
|
||||
args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format])
|
||||
args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format]);
|
||||
}
|
||||
|
||||
const cmd = `${YTDLP_PATH} ${args.join(' ')} ${link}`
|
||||
const cmd = `${YTDLP_PATH} ${args.join(' ')} ${link}`;
|
||||
logger.debug(`Running: ${cmd}`);
|
||||
|
||||
const process = spawn(YTDLP_PATH, [...args, link], { cwd: "/tmp", stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
const process = spawn(YTDLP_PATH, [...args, link], {
|
||||
cwd: '/tmp',
|
||||
stdio: ['ignore', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
process.stdout.on('data', (chunk) => {
|
||||
try {
|
||||
controller.enqueue(chunk)
|
||||
controller.enqueue(chunk);
|
||||
} catch (ex) {
|
||||
process.kill()
|
||||
process.kill();
|
||||
}
|
||||
});
|
||||
process.stderr.on('data', (chunk) => logger.debug(chunk.toString()));
|
||||
process.stdout.on('end', () => {
|
||||
try {
|
||||
controller.close()
|
||||
controller.close();
|
||||
} catch (ex) {
|
||||
logger.error(ex)
|
||||
logger.error(ex);
|
||||
}
|
||||
});
|
||||
process.stdout.on('error', (err) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue