ffmpegConvert();
This commit is contained in:
parent
6d779e7811
commit
9fedc71337
1 changed files with 92 additions and 104 deletions
|
@ -13,7 +13,7 @@ import which from 'which';
|
||||||
* @property {string} NVIDIA - Uses the h264_nvenc codec for NVIDIA GPU-based encoding
|
* @property {string} NVIDIA - Uses the h264_nvenc codec for NVIDIA GPU-based encoding
|
||||||
* @property {string} AMD - Uses the h264_amf codec for AMD GPU-based encoding
|
* @property {string} AMD - Uses the h264_amf codec for AMD GPU-based encoding
|
||||||
* @property {string} INTEL - Uses the h264_qsv codec for Intel GPU-based encoding
|
* @property {string} INTEL - Uses the h264_qsv codec for Intel GPU-based encoding
|
||||||
* @property {string} APPLE - Uses the h264_videotoolbox codec for Apple GPU-based encoding
|
* @property {string} APPLE - Uses the h264_videotoolbox codec for Apple GPU/MediaEngine-based encoding
|
||||||
*/
|
*/
|
||||||
export enum EncodingType {
|
export enum EncodingType {
|
||||||
CPU = 'libx264',
|
CPU = 'libx264',
|
||||||
|
@ -24,7 +24,7 @@ export enum EncodingType {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current encoding type being used for video encoding.
|
* The current encoding type being used for video encoding. Default is CPU
|
||||||
* @type {EncodingType}
|
* @type {EncodingType}
|
||||||
*/
|
*/
|
||||||
export let currentEncoding: EncodingType = EncodingType.CPU;
|
export let currentEncoding: EncodingType = EncodingType.CPU;
|
||||||
|
@ -104,27 +104,9 @@ export const ffmpegDownscale = (path: string, filename: string, extension: strin
|
||||||
'-vf', 'scale=-2:720',
|
'-vf', 'scale=-2:720',
|
||||||
'-c:v', currentEncoding,
|
'-c:v', currentEncoding,
|
||||||
'-c:a', 'copy',
|
'-c:a', 'copy',
|
||||||
|
"-pix_fmt", "yuv420p",
|
||||||
];
|
];
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
.input(path)
|
.input(path)
|
||||||
|
@ -139,50 +121,56 @@ export const ffmpegDownscale = (path: string, filename: string, extension: strin
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts video to gif and vice versa using ffmpeg */
|
/**
|
||||||
/**export const convert: Middleware = (req, res, next) => {
|
* Convert a video to a gif or vice versa using ffmpeg with various encoding options.
|
||||||
const files = req.files as Express.Multer.File[];
|
*
|
||||||
|
* @param {string} path - The input video file path.
|
||||||
for (const file in files) {
|
* @param {string} filename - The name of the file.
|
||||||
const nameAndExtension = extension(files[file].originalname);
|
* @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.
|
||||||
if (videoExtensions.includes(nameAndExtension[1])) {
|
*
|
||||||
console.log("Converting " + nameAndExtension[0] + nameAndExtension[1] + " to gif");
|
* @example
|
||||||
console.log(`Using ${currentEncoding} as encoder`);
|
* ffmpegConvert('input.mp4').then(() => {
|
||||||
|
* console.log('Conversion complete.');
|
||||||
|
* }).catch((error) => {
|
||||||
|
* console.log(`Error: ${error}`);
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
export const ffmpegConvert = (path: string, filename: string, extension: string) => {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
const outputOptions = [
|
||||||
|
'-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';
|
||||||
|
} else {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
reject(`Submitted file is neither a video nor a gif: ${path}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
ffmpeg()
|
ffmpeg()
|
||||||
.input(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
.input(path)
|
||||||
.inputFormat(nameAndExtension[1].substring(1))
|
.outputOptions(outputOptions)
|
||||||
.outputOptions(`-c:v ${currentEncoding}`)
|
.output(`uploads/`)
|
||||||
.outputFormat("gif")
|
.outputFormat(outputFormat)
|
||||||
.output(`uploads/${nameAndExtension[0]}.gif`)
|
.output(`uploads/${filename}${outputFormat}`)
|
||||||
.on("end", function() {
|
.on("end", function() {
|
||||||
console.log(`Conversion complete, took ${Date.now() - startTime} to complete`);
|
console.log(`Conversion complete, took ${Date.now() - startTime} to complete`);
|
||||||
console.log(`Uploaded to uploads/${nameAndExtension[0]}.gif`);
|
console.log(`uploads/${filename}${outputFormat}`);
|
||||||
|
resolve();
|
||||||
})
|
})
|
||||||
.on("error", (e) => console.log(e))
|
.on("error", (e) => reject(e))
|
||||||
.run();
|
.run();
|
||||||
} else if (nameAndExtension[1] == ".gif") {
|
});
|
||||||
console.log(`Converting ${nameAndExtension[0]}${nameAndExtension[1]} to mp4`);
|
}
|
||||||
console.log(`Using ${currentEncoding} as encoder`);
|
|
||||||
|
|
||||||
const startTime = Date.now();
|
|
||||||
ffmpeg(`uploads/${nameAndExtension[0]}${nameAndExtension[1]}`)
|
|
||||||
.inputFormat("gif")
|
|
||||||
.outputFormat("mp4")
|
|
||||||
.outputOptions([
|
|
||||||
"-pix_fmt yuv420p",
|
|
||||||
`-c:v ${currentEncoding}`,
|
|
||||||
"-movflags +faststart"
|
|
||||||
])
|
|
||||||
.noAudio()
|
|
||||||
.output(`uploads/${nameAndExtension[0]}.mp4`)
|
|
||||||
.on("end", function() {
|
|
||||||
console.log(`Conversion complete, took ${Date.now() - startTime} to complete`);
|
|
||||||
console.log(`Uploaded to uploads/${nameAndExtension[0]}.mp4`);
|
|
||||||
next();
|
|
||||||
})
|
|
||||||
.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};**/
|
|
Loading…
Add table
Add a link
Reference in a new issue