add encoding choice
This commit is contained in:
parent
e580b8fd76
commit
f322b45ee1
2 changed files with 57 additions and 2 deletions
|
@ -38,7 +38,7 @@ import {db, MediaParams, insertToDB} from "./db";
|
||||||
enum EncodingType {
|
enum EncodingType {
|
||||||
CPU = 'libx264',
|
CPU = 'libx264',
|
||||||
NVIDIA = 'h264_nvenc',
|
NVIDIA = 'h264_nvenc',
|
||||||
AMD = 'h264_vmf',
|
AMD = 'h264_amf',
|
||||||
INTEL = 'h264_qsv',
|
INTEL = 'h264_qsv',
|
||||||
APPLE = 'h264_videotoolbox'
|
APPLE = 'h264_videotoolbox'
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,20 @@ export const setEncodingType = (type: EncodingType) => {
|
||||||
currentEncoding = type;
|
currentEncoding = type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export 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.`);
|
||||||
|
} else if (envEncoder) {
|
||||||
|
//I finally understand DHH
|
||||||
|
console.warn(`Invalid encoder value "${envEncoder}" in environment variable, defaulting to ${Object.keys(EncodingType).find(key => EncodingType[key as keyof typeof EncodingType] === currentEncoding)}.`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
checkEnvForEncoder();
|
||||||
|
|
||||||
export const checkAuth: Middleware = (req, res, next) => {
|
export const checkAuth: Middleware = (req, res, next) => {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
return res.status(401);
|
return res.status(401);
|
||||||
|
@ -172,6 +186,26 @@ export const convertTo720p: Middleware = (req, res, next) => {
|
||||||
'-c:v', currentEncoding,
|
'-c:v', currentEncoding,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Adjust output options based on encoder for maximum quality
|
||||||
|
switch(currentEncoding) {
|
||||||
|
case EncodingType.CPU:
|
||||||
|
outputOptions.push('-crf', '0');
|
||||||
|
break;
|
||||||
|
case EncodingType.NVIDIA:
|
||||||
|
outputOptions.push('-rc', 'cqp', '-qp', '0');
|
||||||
|
break;
|
||||||
|
case EncodingType.AMD:
|
||||||
|
outputOptions.push('-qp_i', '0', '-qp_p', '0', '-qp_b', '0');
|
||||||
|
break;
|
||||||
|
case EncodingType.INTEL:
|
||||||
|
outputOptions.push('-global_quality', '1'); // Intel QSV specific setting for high quality
|
||||||
|
break;
|
||||||
|
case EncodingType.APPLE:
|
||||||
|
outputOptions.push('-global_quality', '1');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
||||||
.inputFormat('mp4')
|
.inputFormat('mp4')
|
||||||
|
|
|
@ -27,7 +27,7 @@ ffmpeg.setFfprobePath(ffprobePath!);
|
||||||
export enum EncodingType {
|
export enum EncodingType {
|
||||||
CPU = 'libx264',
|
CPU = 'libx264',
|
||||||
NVIDIA = 'h264_nvenc',
|
NVIDIA = 'h264_nvenc',
|
||||||
AMD = 'h264_vmf',
|
AMD = 'h264_amf',
|
||||||
INTEL = 'h264_qsv',
|
INTEL = 'h264_qsv',
|
||||||
APPLE = 'h264_videotoolbox',
|
APPLE = 'h264_videotoolbox',
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,29 @@ export const generateTestVideo = async (encodingType: EncodingType): Promise<voi
|
||||||
const outputOptions = [
|
const outputOptions = [
|
||||||
'-vf', 'scale=-2:720',
|
'-vf', 'scale=-2:720',
|
||||||
'-c:v', encodingType,
|
'-c:v', encodingType,
|
||||||
|
'-c:a', 'copy', // Copying the original audio stream
|
||||||
|
'-b:v', '8000k'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Adjust output options based on encoder for maximum quality
|
||||||
|
switch(encodingType) {
|
||||||
|
case EncodingType.CPU:
|
||||||
|
outputOptions.push('-crf', '0');
|
||||||
|
break;
|
||||||
|
case EncodingType.NVIDIA:
|
||||||
|
outputOptions.push('-rc', 'cqp', '-qp', '0');
|
||||||
|
break;
|
||||||
|
case EncodingType.AMD:
|
||||||
|
outputOptions.push('-qp_i', '0', '-qp_p', '0', '-qp_b', '0');
|
||||||
|
break;
|
||||||
|
case EncodingType.INTEL:
|
||||||
|
outputOptions.push('-global_quality', '1'); // Intel QSV specific setting for high quality
|
||||||
|
break;
|
||||||
|
case EncodingType.APPLE:
|
||||||
|
outputOptions.push('-global_quality', '1');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
.input('test.mp4')
|
.input('test.mp4')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue