Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
WaveringAna committed Nov 18, 2023
1 parent 807e503 commit 832189a
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 310 deletions.
52 changes: 17 additions & 35 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
module.exports = {
"env": {
"browser": true,
"es2021": true,
"node": true
env: {
browser: true,
es2021: true,
node: true,
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
overrides: [],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
indent: ["error", 2, { SwitchCase: 1 }],
"linebreak-style": ["error", "unix"],
quotes: ["error", "double"],
semi: ["error", "always"],
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
]
}
};
143 changes: 94 additions & 49 deletions app/lib/ffmpeg.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { extension, videoExtensions, imageExtensions } from "./lib";

import ffmpeg, { FfprobeData, ffprobe } from 'fluent-ffmpeg';
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
import ffprobeInstaller from '@ffprobe-installer/ffprobe';
import which from 'which';
import ffmpeg, { FfprobeData, ffprobe } from "fluent-ffmpeg";
import ffmpegInstaller from "@ffmpeg-installer/ffmpeg";
import ffprobeInstaller from "@ffprobe-installer/ffprobe";
import which from "which";

/**
* Enum to represent different types of video encoding methods.
*
*
* @enum {string}
* @property {string} CPU - Uses the libx264 codec for CPU-based encoding
* @property {string} NVIDIA - Uses the h264_nvenc codec for NVIDIA GPU-based encoding
Expand All @@ -16,11 +16,11 @@ import which from 'which';
* @property {string} APPLE - Uses the h264_videotoolbox codec for Apple GPU/MediaEngine-based encoding
*/
export enum EncodingType {
CPU = 'libx264',
NVIDIA = 'h264_nvenc',
AMD = 'h264_amf',
INTEL = 'h264_qsv',
APPLE = 'h264_videotoolbox'
CPU = "libx264",
NVIDIA = "h264_nvenc",
AMD = "h264_amf",
INTEL = "h264_qsv",
APPLE = "h264_videotoolbox",
}

/**
Expand All @@ -31,7 +31,7 @@ export let currentEncoding: EncodingType = EncodingType.CPU;

/**
* Sets the current encoding type.
*
*
* @param {EncodingType} type - The encoding type to set.
*/
export const setEncodingType = (type: EncodingType) => {
Expand All @@ -40,16 +40,20 @@ export const setEncodingType = (type: EncodingType) => {

/**
* Returns the path to an executable by checking environment variables, the system path, or a default installer.
*
*
* @param {string} envVar - The environment variable to check for the executable's path.
* @param {string} executable - The name of the executable to search for in the system path.
* @param {Object} installer - An object containing the default installer path.
* @param {string} installer.path - The default path to use if the executable is not found in the environment or system path.
*
*
* @returns {string} - The path to the executable.
* @throws Will throw an error if the executable is not found and the installer path is not available.
*/
const getExecutablePath = (envVar: string, executable: string, installer: { path: string }) => {
const getExecutablePath = (
envVar: string,
executable: string,
installer: { path: string },
) => {
if (process.env[envVar]) {
return process.env[envVar];
}
Expand All @@ -61,8 +65,16 @@ const getExecutablePath = (envVar: string, executable: string, installer: { path
}
};

const ffmpegPath = getExecutablePath('EB_FFMPEG_PATH', 'ffmpeg', ffmpegInstaller);
const ffprobePath = getExecutablePath('EB_FFPROBE_PATH', 'ffprobe', ffprobeInstaller);
const ffmpegPath = getExecutablePath(
"EB_FFMPEG_PATH",
"ffmpeg",
ffmpegInstaller,
);
const ffprobePath = getExecutablePath(
"EB_FFPROBE_PATH",
"ffprobe",
ffprobeInstaller,
);

console.log(`Using ffmpeg from path: ${ffmpegPath}`);
console.log(`Using ffprobe from path: ${ffprobePath}`);
Expand All @@ -74,84 +86,111 @@ const checkEnvForEncoder = () => {
const envEncoder = process.env.EB_ENCODER?.toUpperCase();

if (envEncoder && Object.keys(EncodingType).includes(envEncoder)) {
setEncodingType(EncodingType[envEncoder as keyof typeof EncodingType] as EncodingType);
console.log(`Setting encoding type to ${envEncoder} based on environment variable.`);
setEncodingType(
EncodingType[envEncoder as keyof typeof EncodingType] as EncodingType,
);
console.log(
`Setting encoding type to ${envEncoder} based on environment variable.`,
);
} else if (envEncoder) {
console.warn(`Invalid encoder value "${envEncoder}" in environment variable, defaulting to CPU.`);
console.warn(
`Invalid encoder value "${envEncoder}" in environment variable, defaulting to CPU.`,
);
}
};

checkEnvForEncoder();

/**
* Downscale a video using ffmpeg with various encoding options.
*
*
* @param {string} path - The input video file path.
* @param {string} filename - The name of the file.
* @param {string} extension - The file extension of the file
* @returns {Promise<void>} - A promise that resolves when the downscaling is complete, and rejects on error.
*
*
* @example
* ffmpegDownscale('input.mp4').then(() => {
* console.log('Downscaling complete.');
* }).catch((error) => {
* console.log(`Error: ${error}`);
* });
*/
export const ffmpegDownscale = (path: string, filename: string, extension: string) => {
export const ffmpegDownscale = (
path: string,
filename: string,
extension: string,
) => {
const startTime = Date.now();
const outputOptions = [
'-vf', 'scale=-2:720',
'-c:v', currentEncoding,
'-c:a', 'copy',
"-pix_fmt", "yuv420p",
"-vf",
"scale=-2:720",
"-c:v",
currentEncoding,
"-c:a",
"copy",
"-pix_fmt",
"yuv420p",
];

return new Promise<void>((resolve, reject) => {
ffmpeg()
.input(path)
.outputOptions(outputOptions)
.output(`uploads/720p-${filename}${extension}`)
.on('end', () => {
console.log(`720p copy complete using ${currentEncoding}, took ${Date.now() - startTime}ms to complete`);
.on("end", () => {
console.log(
`720p copy complete using ${currentEncoding}, took ${
Date.now() - startTime
}ms to complete`,
);
resolve();
})
.on('error', (e) => reject(new Error(e)))
.on("error", (e) => reject(new Error(e)))
.run();
});
}
};

/**
* Convert a video to a gif or vice versa using ffmpeg with various encoding options.
*
*
* @param {string} path - The input video file path.
* @param {string} filename - The name of the file.
* @param {string} extension - The file extension of the file
* @returns {Promise<void>} - A promise that resolves when the conversion is complete, and rejects on error.
*
*
* @example
* ffmpegConvert('input.mp4').then(() => {
* console.log('Conversion complete.');
* }).catch((error) => {
* console.log(`Error: ${error}`);
* });
*/
export const ffmpegConvert = (path: string, filename: string, extension: string) => {
export const ffmpegConvert = (
path: string,
filename: string,
extension: string,
) => {
const startTime = Date.now();
const outputOptions = [
'-vf', 'scale=-2:720',
'-c:v', currentEncoding,
'-c:a', 'copy',
"-movflags", "+faststart",
"-pix_fmt", "yuv420p",
]
"-vf",
"scale=-2:720",
"-c:v",
currentEncoding,
"-c:a",
"copy",
"-movflags",
"+faststart",
"-pix_fmt",
"yuv420p",
];

let outputFormat: string;

if (videoExtensions.includes(extension)) {
outputFormat = '.gif';
} else if (extension == '.gif') {
outputFormat = '.mp4';
outputFormat = ".gif";
} else if (extension == ".gif") {
outputFormat = ".mp4";
} else {
return new Promise<void>((resolve, reject) => {
reject(`Submitted file is neither a video nor a gif: ${path}`);
Expand All @@ -162,24 +201,30 @@ export const ffmpegConvert = (path: string, filename: string, extension: string)
ffmpeg()
.input(path)
.outputOptions(outputOptions)
.output(`uploads/`)
.output("uploads/")
.outputFormat(outputFormat)
.output(`uploads/${filename}${outputFormat}`)
.on("end", function() {
console.log(`Conversion complete, took ${Date.now() - startTime} to complete`);
.on("end", function () {
console.log(
`Conversion complete, took ${Date.now() - startTime} to complete`,
);
console.log(`uploads/${filename}${outputFormat}`);
resolve();
})
.on("error", (e) => reject(e))
.run();
});
}
};

export const ffProbe = (path: string, filename: string, extension: string) => {
export const ffProbe = async (
path: string,
filename: string,
extension: string,
) => {
return new Promise<FfprobeData>((resolve, reject) => {
ffprobe(path, (err, data) => {
if (err) reject (err);
if (err) reject(err);
resolve(data);
});
});
}
};
44 changes: 32 additions & 12 deletions app/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,45 @@
declare global {
namespace Express {
interface User {
id? : number | string,
username: string,
hashed_password?: any,
salt?: any
id?: number | string;
username: string;
hashed_password?: any;
salt?: any;
}
}
}
/**Splits a file name into its name and then its extension */
export function extension(str: string){
export function extension(str: string) {
const file = str.split("/").pop();
return [file.substr(0,file.lastIndexOf(".")),file.substr(file.lastIndexOf("."),file.length).toLowerCase()];
return [
file.substr(0, file.lastIndexOf(".")),
file.substr(file.lastIndexOf("."), file.length).toLowerCase(),
];
}
/**Type for user data */
export interface User {
id? : number | string,
username: string,
hashed_password?: any,
salt?: any
id?: number | string;
username: string;
hashed_password?: any;
salt?: any;
}

export const videoExtensions = [".mp4", ".mov", ".avi", ".flv", ".mkv", ".wmv", ".webm"];
export const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".tiff", ".webp"];
export const videoExtensions = [
".mp4",
".mov",
".avi",
".flv",
".mkv",
".wmv",
".webm",
];
export const imageExtensions = [
".jpg",
".jpeg",
".png",
".gif",
".bmp",
".svg",
".tiff",
".webp",
];
Loading

0 comments on commit 832189a

Please sign in to comment.