From 4e8d141eb97764e05fbbb6149b4e044d368dfd35 Mon Sep 17 00:00:00 2001 From: JorisKohl Date: Fri, 24 Jan 2025 15:36:06 +0100 Subject: [PATCH] Added a variable to make sure all files transcoded are in h265. Very advanced use. --- .env.example | 6 +++++- src/app.controller.ts | 6 ++---- src/app.service.ts | 23 +++++++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index fe2250e..aaa4c71 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,8 @@ # TIME_TO_KEEP_FILES=8 # Hours. Non-option when REMOVE_FILE_AFTER_RIGHT_DOWNLOAD=false # File management -# MAX_CACHED_PER_USER=10 \ No newline at end of file +# MAX_CACHED_PER_USER=10 + +# Download format +# ONLY EDIT IF YOU KNOW WHAT YOU ARE DOING! THIS COULD BREAK COMPATIBILITY WITH YOUR DEVICE +# FORCE_ALL_DOWNLOADS_TO_H265=false # Forces all downloads into h265 improving file size \ No newline at end of file diff --git a/src/app.controller.ts b/src/app.controller.ts index f17981e..63ae8ca 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -42,7 +42,6 @@ export class AppController { let jellyfinUrl = process.env.JELLYFIN_URL; let finalUrl: string; - if (jellyfinUrl) { jellyfinUrl = jellyfinUrl.replace(/\/$/, ''); // If JELLYFIN_URL is set, use it to replace the base of the incoming URL @@ -55,7 +54,7 @@ export class AppController { // If JELLYFIN_URL is not set, use the incoming URL as is finalUrl = url; } - + const id = await this.appService.downloadAndCombine( finalUrl, fileExtension, @@ -119,15 +118,14 @@ export class AppController { } const stat = fs.statSync(filePath); - res.setHeader('Content-Length', stat.size); res.setHeader('Content-Type', 'video/mp4'); res.setHeader( 'Content-Disposition', `attachment; filename=transcoded_${id}.mp4`, ); - const fileStream = fs.createReadStream(filePath); + this.logger.log(`Download started for ${filePath}`) return new Promise((resolve, reject) => { diff --git a/src/app.service.ts b/src/app.service.ts index 06b402b..7218abe 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -38,7 +38,8 @@ export class AppService { private maxCachedPerUser: number; private cacheDir: string; private immediateRemoval: boolean; - + private forceAllDownloadsToH265: boolean; + constructor( private logger: Logger, private configService: ConfigService, @@ -58,6 +59,21 @@ export class AppService { 'REMOVE_FILE_AFTER_RIGHT_DOWNLOAD', true, ); + this.maxCachedPerUser = this.configService.get( + 'MAX_CACHED_PER_USER', + 10, + ); + + this.immediateRemoval = this.configService.get('REMOVE_FILE_AFTER_RIGHT_DOWNLOAD', 'false').toLowerCase() === 'true'; + + this.forceAllDownloadsToH265 = this.configService.get('FORCE_ALL_DOWNLOADS_TO_H265', 'false').toLowerCase() === 'true'; + } + + urlEditor(url){ + if (this.forceAllDownloadsToH265 === true){ + url = url.replace(/VideoCodec=h264/g, "VideoCodec=h265"); + } + return url } async downloadAndCombine( @@ -70,10 +86,10 @@ export class AppService { ): Promise { const jobId = uuidv4(); const outputPath = path.join(this.cacheDir, `combined_${jobId}.mp4`); - this.logger.log( `Queueing job ${jobId.padEnd(36)} | URL: ${(url.slice(0, 50) + '...').padEnd(53)} | Path: ${outputPath}`, ); + url = this.urlEditor(url) this.activeJobs.push({ id: jobId, @@ -407,7 +423,6 @@ export class AppService { } if (code === 0) { - job.status = 'completed'; job.progress = 100; // Update the file size @@ -449,7 +464,7 @@ export class AppService { } } - + private async getVideoDuration( inputUrl: string, jobId: string,