feat: video-to-audio converter implemented without logic

This commit is contained in:
Domenik 2025-02-22 14:25:05 +01:00
parent 91125204b5
commit 324156fd03
7 changed files with 318 additions and 2 deletions

View file

@ -17,7 +17,8 @@
"fastify-multipart": "^5.3.1",
"libreoffice-convert": "^1.6.0",
"luxon": "^3.5.0",
"qrcode": "^1.5.4"
"qrcode": "^1.5.4",
"video-to-audio": "^1.0.1"
},
"devDependencies": {
"@types/luxon": "^3.4.2",
@ -1317,6 +1318,12 @@
"node": ">= 0.8"
}
},
"node_modules/video-to-audio": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/video-to-audio/-/video-to-audio-1.0.1.tgz",
"integrity": "sha512-6+h79x2kSO60PYTT2cXcgXIsqw/3X8a5qshsZanxH0cxbGUVxVllwQdNOWr/8ywDNcgGgecSmk8Q6TJ79gtFjQ==",
"license": "MIT"
},
"node_modules/which-module": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.1.tgz",

View file

@ -20,7 +20,8 @@
"fastify-multipart": "^5.3.1",
"libreoffice-convert": "^1.6.0",
"luxon": "^3.5.0",
"qrcode": "^1.5.4"
"qrcode": "^1.5.4",
"video-to-audio": "^1.0.1"
},
"devDependencies": {
"@types/luxon": "^3.4.2",

View file

@ -8,6 +8,7 @@ import { regexTest } from "./src/routes/regextest.route";
import { tmzConvert } from "./src/routes/tmzconvert.route";
import { generateQRCode } from "./src/routes/generateqrcode.route";
import { wordCounter } from "./src/routes/wordcounter.route";
import { videoConvert } from "./src/routes/videoconvert.route";
const app = Fastify({ logger: true });
@ -25,6 +26,7 @@ app.register(regexTest);
app.register(tmzConvert);
app.register(generateQRCode);
app.register(wordCounter);
app.register(videoConvert);
const PORT = process.env.PORT || 4000;
app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => {

View file

@ -0,0 +1,70 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import * as libre from "libreoffice-convert";
import { promisify } from "util";
import { MultipartValue } from "@fastify/multipart";
const libreConvertAsync = promisify(libre.convert);
const mimeTypes: { [key: string]: string } = {
mp4: "video/mp4",
avi: "video/x-msvideo",
mkv: "video/x-matroska",
};
export async function videoConvert(app: FastifyInstance) {
app.post(
"/api/video-convert",
async (request: FastifyRequest, reply: FastifyReply) => {
try {
const parts = request.parts();
let fileBuffer: Buffer | null = null;
let outputFileExt = "";
for await (const part of parts) {
if (part.type === "file") {
fileBuffer = await part.toBuffer();
} else if (
part.fieldname === "outputFormat" &&
part.type === "field"
) {
outputFileExt = (part as MultipartValue<string>).value;
}
}
if (!fileBuffer) {
return reply.status(400).send({ error: "No file uploaded!" });
}
if (!outputFileExt) {
return reply
.status(400)
.send({ error: "No output format provided!" });
}
if (!outputFileExt.startsWith(".")) {
outputFileExt = "." + outputFileExt;
}
const format = outputFileExt.substring(1);
const mimeType = mimeTypes[format] || "application/octet-stream";
const convertedBuffer = await libreConvertAsync(
fileBuffer,
outputFileExt,
undefined,
);
reply
.header("Content-Type", mimeType)
.header(
"Content-Disposition",
`attachment; filename="converted${outputFileExt}"`,
)
.status(200)
.send(convertedBuffer);
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
},
);
}