From 9e5c6349427556dfc7cf83b7f8c82fcdc395ee3c Mon Sep 17 00:00:00 2001 From: Atanner Date: Mon, 14 Apr 2025 22:30:20 -0600 Subject: [PATCH] Enhance file conversion endpoint with error handling and progress logging --- index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 86db9aa..93cab02 100644 --- a/index.js +++ b/index.js @@ -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 }); }); -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"));