Browse Source

Enhance file conversion endpoint with error handling and progress logging

master
Atanner 8 months ago
parent
commit
9e5c634942
  1. 54
      index.js

54
index.js

@ -1,24 +1,60 @@ @@ -1,24 +1,60 @@
const express = require("express");
const multer = require("multer");
const { exec } = require("child_process");
const path = require("path");
const { spawn } = require("child_process");
const fs = require("fs");
const app = express();
const upload = multer({ dest: "uploads/" });
app.post("/convert", upload.fields([{ name: "audio" }, { name: "image" }]), (req, res) => {
if (!req.files || !req.files.audio || !req.files.image) {
console.error("❌ Missing file(s) in request");
return res.status(400).send("Missing audio or image file.");
}
const audioPath = req.files.audio[0].path;
const imagePath = req.files.image[0].path;
const output = `output-${Date.now()}.mp4`;
const cmd = `ffmpeg -loop 1 -i ${imagePath} -i ${audioPath} -c:v libx264 -preset fast -tune stillimage -crf 18 -c:a aac -shortest -pix_fmt yuv420p ${output}`;
const args = [
"-loop", "1",
"-i", imagePath,
"-i", audioPath,
"-c:v", "libx264",
"-preset", "fast",
"-tune", "stillimage",
"-crf", "18",
"-c:a", "aac",
"-shortest",
"-pix_fmt", "yuv420p",
output
];
console.log(`🚀 Starting FFmpeg conversion: ${output}`);
const ffmpeg = spawn("ffmpeg", args);
let lastLog = Date.now();
ffmpeg.stderr.on("data", (data) => {
const now = Date.now();
const text = data.toString();
// Log a progress update every 3 seconds max
if (text.includes("frame=") && now - lastLog > 3000) {
console.log(`[FFmpeg Progress] ${text.trim()}`);
lastLog = now;
}
// If you want verbose output uncomment this:
// console.log(`[FFmpeg Raw] ${text.trim()}`);
});
exec(cmd, (err) => {
if (err) {
console.error(err);
ffmpeg.on("exit", (code) => {
if (code !== 0) {
console.error(`❌ FFmpeg failed with code ${code}`);
res.status(500).send("Conversion failed");
} else {
console.log("✅ FFmpeg finished successfully. Sending file...");
res.download(output, () => {
fs.unlinkSync(audioPath);
fs.unlinkSync(imagePath);
@ -28,4 +64,8 @@ app.post("/convert", upload.fields([{ name: "audio" }, { name: "image" }]), (req @@ -28,4 +64,8 @@ app.post("/convert", upload.fields([{ name: "audio" }, { name: "image" }]), (req
});
});
app.listen(3000, () => console.log("FFmpeg API running on port 3000"));
app.get("/", (req, res) => {
res.send("🎧 FileConvert API is up.");
});
app.listen(3000, () => console.log("✅ FileConvert API running on http://0.0.0.0:3000"));

Loading…
Cancel
Save