move ffprobe to ffmpeg.ts

This commit is contained in:
waveringana 2023-10-31 13:13:22 -04:00
parent 9fedc71337
commit 7a86ad95b4
2 changed files with 17 additions and 15 deletions

View file

@ -1,6 +1,6 @@
import { extension, videoExtensions, imageExtensions } from "./lib"; import { extension, videoExtensions, imageExtensions } from "./lib";
import ffmpeg from 'fluent-ffmpeg'; import ffmpeg, { FfprobeData, ffprobe } from 'fluent-ffmpeg';
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg'; import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
import ffprobeInstaller from '@ffprobe-installer/ffprobe'; import ffprobeInstaller from '@ffprobe-installer/ffprobe';
import which from 'which'; import which from 'which';
@ -174,3 +174,12 @@ export const ffmpegConvert = (path: string, filename: string, extension: string)
.run(); .run();
}); });
} }
export const ffProbe = (path: string, filename: string, extension: string) => {
return new Promise<FfprobeData>((resolve, reject) => {
ffprobe(path, (err, data) => {
if (err) reject (err);
resolve(data);
});
});
}

View file

@ -1,13 +1,9 @@
import type {RequestHandler as Middleware, Request, Response, NextFunction} from "express"; import type {RequestHandler as Middleware, Request, Response, NextFunction} from "express";
import multer from "multer"; import multer from "multer";
import express from "express"; import express from "express";
import ffmpeg from "fluent-ffmpeg";
import imageProbe from "probe-image-size"; import imageProbe from "probe-image-size";
import ffmpegpath from "@ffmpeg-installer/ffmpeg";
import ffprobepath from "@ffprobe-installer/ffprobe";
ffmpeg.setFfmpegPath(ffmpegpath.path); import {ffProbe} from "../lib/ffmpeg";
ffmpeg.setFfprobePath(ffprobepath.path);
import fs from "fs"; import fs from "fs";
@ -57,15 +53,12 @@ router.get("/gifv/:file", async (req: Request, res: Response, next: NextFunction
const nameAndExtension = extension(`uploads/${req.params.file}`); const nameAndExtension = extension(`uploads/${req.params.file}`);
if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".mov" || nameAndExtension[1] == ".webm" || nameAndExtension[1] == ".gif") { if (nameAndExtension[1] == ".mp4" || nameAndExtension[1] == ".mov" || nameAndExtension[1] == ".webm" || nameAndExtension[1] == ".gif") {
ffmpeg() let imageData = ffProbe(`uploads/${req.params.file}`, nameAndExtension[0], nameAndExtension[1]);
.input(`uploads/${req.params.file}`)
.inputFormat(nameAndExtension[1].substring(1)) width = (await imageData).streams[0].width;
.ffprobe((err: Error, data: ffmpeg.FfprobeData) => { height = (await imageData).streams[0].height;
if (err) return next(err);
width = data.streams[0].width;
height = data.streams[0].height;
return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: width, height: height }); return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: width, height: height });
});
} else { } else {
const imageData = await imageProbe(fs.createReadStream(`uploads/${req.params.file}`)); const imageData = await imageProbe(fs.createReadStream(`uploads/${req.params.file}`));
return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: imageData.width, height: imageData.height }); return res.render("gifv", { url: url, host: `${req.protocol}://${req.get("host")}`, width: imageData.width, height: imageData.height });