From 63c4c5837f62e2c0f68fcd2a78c5e6d2f7397461 Mon Sep 17 00:00:00 2001 From: regent Date: Sat, 18 Nov 2023 17:55:14 -0500 Subject: [PATCH] Add status indicator to videos that are not yet downscaled --- app/lib/ffmpeg.ts | 43 +- app/routes/index.ts | 6 +- app/views/index.ejs | 70 +- app/views/partials/_fileList.ejs | 176 ++ package-lock.json | 3689 +----------------------------- 5 files changed, 217 insertions(+), 3767 deletions(-) create mode 100644 app/views/partials/_fileList.ejs diff --git a/app/lib/ffmpeg.ts b/app/lib/ffmpeg.ts index d65c10f..45d70ae 100644 --- a/app/lib/ffmpeg.ts +++ b/app/lib/ffmpeg.ts @@ -5,6 +5,8 @@ import ffmpegInstaller from "@ffmpeg-installer/ffmpeg"; import ffprobeInstaller from "@ffprobe-installer/ffprobe"; import which from "which"; +import fs from "fs"; + /** * Enum to represent different types of video encoding methods. * @@ -52,7 +54,7 @@ export const setEncodingType = (type: EncodingType) => { const getExecutablePath = ( envVar: string, executable: string, - installer: { path: string }, + installer: { path: string } ) => { if (process.env[envVar]) { return process.env[envVar]; @@ -68,12 +70,12 @@ const getExecutablePath = ( const ffmpegPath = getExecutablePath( "EB_FFMPEG_PATH", "ffmpeg", - ffmpegInstaller, + ffmpegInstaller ); const ffprobePath = getExecutablePath( "EB_FFPROBE_PATH", "ffprobe", - ffprobeInstaller, + ffprobeInstaller ); console.log(`Using ffmpeg from path: ${ffmpegPath}`); @@ -87,14 +89,14 @@ const checkEnvForEncoder = () => { if (envEncoder && Object.keys(EncodingType).includes(envEncoder)) { setEncodingType( - EncodingType[envEncoder as keyof typeof EncodingType] as EncodingType, + EncodingType[envEncoder as keyof typeof EncodingType] as EncodingType ); console.log( - `Setting encoding type to ${envEncoder} based on environment variable.`, + `Setting encoding type to ${envEncoder} based on environment variable.` ); } else if (envEncoder) { console.warn( - `Invalid encoder value "${envEncoder}" in environment variable, defaulting to CPU.`, + `Invalid encoder value "${envEncoder}" in environment variable, defaulting to CPU.` ); } }; @@ -119,7 +121,7 @@ checkEnvForEncoder(); export const ffmpegDownscale = ( path: string, filename: string, - extension: string, + extension: string ) => { const startTime = Date.now(); const outputOptions = [ @@ -138,15 +140,32 @@ export const ffmpegDownscale = ( .input(path) .outputOptions(outputOptions) .output(`uploads/720p-${filename}${extension}`) + .on("start", () => { + // Create the .processing file + fs.closeSync( + fs.openSync(`uploads/720p-${filename}${extension}.processing`, "w") + ); + }) .on("end", () => { console.log( `720p copy complete using ${currentEncoding}, took ${ Date.now() - startTime - }ms to complete`, + }ms to complete` ); + + // Delete the .processing file + fs.unlinkSync(`uploads/720p-${filename}${extension}.processing`); + resolve(); }) - .on("error", (e) => reject(new Error(e))) + .on("error", (e) => { + // Ensure to delete the .processing file even on error + if (fs.existsSync(`uploads/720p-${filename}${extension}.processing`)) { + fs.unlinkSync(`uploads/720p-${filename}${extension}.processing`); + } + + reject(new Error(e)); + }) .run(); }); }; @@ -169,7 +188,7 @@ export const ffmpegDownscale = ( export const ffmpegConvert = ( path: string, filename: string, - extension: string, + extension: string ) => { const startTime = Date.now(); const outputOptions = [ @@ -206,7 +225,7 @@ export const ffmpegConvert = ( .output(`uploads/${filename}${outputFormat}`) .on("end", function () { console.log( - `Conversion complete, took ${Date.now() - startTime} to complete`, + `Conversion complete, took ${Date.now() - startTime} to complete` ); console.log(`uploads/${filename}${outputFormat}`); resolve(); @@ -219,7 +238,7 @@ export const ffmpegConvert = ( export const ffProbe = async ( path: string, filename: string, - extension: string, + extension: string ) => { return new Promise((resolve, reject) => { ffprobe(path, (err, data) => { diff --git a/app/routes/index.ts b/app/routes/index.ts index 7190464..00a74db 100644 --- a/app/routes/index.ts +++ b/app/routes/index.ts @@ -4,6 +4,7 @@ import type { Response, NextFunction, } from "express"; + import multer from "multer"; import express from "express"; import imageProbe from "probe-image-size"; @@ -65,6 +66,10 @@ router.get( }, ); +/*router.get("/media-list", fetchMedia, (req: Request, res: Response) => { + res.render("partials/_fileList"); // Render only the file list partial +});*/ + router.get( "/gifv/:file", async (req: Request, res: Response, next: NextFunction) => { @@ -166,4 +171,3 @@ router.post( ); export default router; - diff --git a/app/views/index.ejs b/app/views/index.ejs index 17e0dda..ab7aaba 100644 --- a/app/views/index.ejs +++ b/app/views/index.ejs @@ -11,14 +11,7 @@ - <% - function extension(string) { - return string.slice((string.lastIndexOf(".") - 2 >>> 0) + 2); - } - - const videoExtensions = ['.mp4', '.mov', '.avi', '.flv', '.mkv', '.wmv', '.webm']; - const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.tiff', '.webp']; - %> +
@@ -65,68 +58,13 @@

Click the file to copy the url

- <% if (Count > 0) { %>
    - <% files.forEach(function(file) { %> -
  • -
    -
    - <% if (videoExtensions.includes(extension(file.path))) { %> -
    - -
    - <% if(user.username == "admin" && file.username != "admin") { %> - <%= file.username %> -
    - <% } %> - Copy as GIFv -
    -
    - <% } else if (extension(file.path) == ".gif") { %> -
    - -
    - <% if(user.username == "admin" && file.username != "admin") { %> - <%= file.username %> -
    - <% } %> - Copy as GIFv -
    -
    - <% } else if (imageExtensions.includes(extension(file.path))) { %> -
    - - <% if(user.username == "admin" && file.username != "admin") { %> -
    - <%= file.username %> -
    - <% } %> -
    - <% } else {%> -
    -

    <%=extension(file.path)%> file

    - <% if(user.username == "admin" && file.username != "admin") { %> -
    - <%= file.username %> -
    - <% } %> -
    - <% } %> - - - -
    -
    -
    -
    -
  • - <% }); %> + <% if (files && files.length > 0) { %> + <%- include('partials/_fileList.ejs',) %> + <% } %>
- <% } %>