-
+
diff --git a/src/lib/server/helpers.ts b/src/lib/server/helpers.ts
new file mode 100644
index 0000000..4b42bbb
--- /dev/null
+++ b/src/lib/server/helpers.ts
@@ -0,0 +1,20 @@
+import formats from '$lib/common/supportedFormats.json';
+import winston from 'winston';
+import { dev } from '$app/environment';
+
+export const logger = winston.createLogger({
+ level: dev ? 'debug' : 'error',
+ format: winston.format.json(),
+ transports: [new winston.transports.Console()]
+});
+const formatMime = new Map(Object.entries(formats));
+export const isURLValid = (url: string) => {
+ try {
+ new URL(url);
+ } catch {
+ return false;
+ }
+
+ return true;
+};
+export const mimeTypeMap = formatMime;
diff --git a/src/lib/server/ytdlp.ts b/src/lib/server/ytdlp.ts
index f37ddef..bbeecab 100644
--- a/src/lib/server/ytdlp.ts
+++ b/src/lib/server/ytdlp.ts
@@ -1,7 +1,11 @@
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 { logger, mimeTypeMap } from '$lib/server/helpers';
+
const YTDLP_PATH: string = env.YTDLP_PATH as string;
+const HTTPS_PROXY: string = env.v as string;
export const ytdl = create(YTDLP_PATH);
@@ -13,7 +17,8 @@ export async function getYouTubeMetadata(link: string) {
dumpSingleJson: true,
noCheckCertificates: true,
noWarnings: true,
- preferFreeFormats: true
+ preferFreeFormats: true,
+ proxy: HTTPS_PROXY ? HTTPS_PROXY : ''
});
}
@@ -21,25 +26,55 @@ export async function getYouTubeMetadata(link: string) {
* Streams the YouTube video/audio using youtube-dl-exec
*/
export function streamYouTube(link: string, format: string): ReadableStream
{
+ logger.debug(`Starting to stream: ${link}`);
+ const mimeType: string | undefined = mimeTypeMap.get(format);
+
+ if (!mimeType) {
+ throw new Error('Unsupported format');
+ }
+ logger.debug(`Given format is compatible: ${mimeType}`);
+
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 args = ['--no-write-thumbnail', '-o', '-'];
- const process = spawn('yt-dlp', [...args, link], { stdio: ['ignore', 'pipe', 'ignore'] });
+ if (HTTPS_PROXY) {
+ args.push('--proxy', HTTPS_PROXY);
+ }
- process.stdout.on('data', (chunk) => controller.enqueue(chunk));
- process.stdout.on('end', () => controller.close());
+ 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]);
+ }
+
+ const cmd = `${YTDLP_PATH} ${args.join(' ')} ${link}`;
+ logger.debug(`Running: ${cmd}`);
+
+ const process = spawn(YTDLP_PATH, [...args, link], {
+ cwd: '/tmp',
+ stdio: ['ignore', 'pipe', 'pipe']
+ });
+
+ process.stdout.on('data', (chunk) => {
+ try {
+ controller.enqueue(chunk);
+ } catch (ex) {
+ process.kill();
+ }
+ });
+ process.stderr.on('data', (chunk) => logger.debug(chunk.toString()));
+ process.stdout.on('end', () => {
+ try {
+ controller.close();
+ } catch (ex) {
+ logger.error(ex);
+ }
+ });
process.stdout.on('error', (err) => {
- console.error('Stream error:', err);
+ logger.error('Stream error:', err);
controller.error(err);
});
}
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte
index 61642b3..0d97a1a 100644
--- a/src/routes/+layout.svelte
+++ b/src/routes/+layout.svelte
@@ -17,4 +17,11 @@
transform: rotate(180deg);
display: inline-block;
}
+
+ @media screen and (max-height: 600px) {
+ /* Your CSS rules here */
+ footer {
+ display: none;
+ }
+ }
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts
new file mode 100644
index 0000000..5b66645
--- /dev/null
+++ b/src/routes/+page.server.ts
@@ -0,0 +1,3 @@
+export const prerender = true;
+export const ssr = true;
+export const csr = true;
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 098ef91..07793c0 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -1,35 +1,88 @@