Button Phone Conveter Test Code
const express = require("express");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const { execFile } = require("child_process");
const app = express();
const PORT = process.env.PORT || 10000;
const UPLOAD_DIR = "/tmp/uploads";
const OUTPUT_DIR = "/tmp/outputs";
fs.mkdirSync(UPLOAD_DIR, { recursive: true });
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const upload = multer({
dest: UPLOAD_DIR,
limits: {
fileSize: 500 * 1024 * 1024
}
});
app.get("/health", (req, res) => {
res.status(200).json({
success: true,
status: "ready"
});
});
app.get("/startup-test", (req, res) => {
const testOutput =
path.join(
OUTPUT_DIR,
"startup-test.mp4"
);
const testArgs = [
"-y",
"-f",
"lavfi",
"-i",
"color=c=black:s=256x144:r=15",
"-t",
"1",
"-c:v",
"mpeg4",
"-b:v",
"180k",
"-an",
testOutput
];
execFile(
"ffmpeg",
testArgs,
{
maxBuffer:
10 * 1024 * 1024
},
(error, stdout, stderr) => {
if (error) {
console.error(
"Startup test failed:",
stderr
);
return res
.status(500)
.send(
"Startup test failed."
);
}
if (
!fs.existsSync(
testOutput
)
) {
return res
.status(500)
.send(
"Startup test output was not created."
);
}
try {
fs.unlinkSync(
testOutput
);
}
catch (_) {}
return res.json({
success: true,
status: "ready"
});
}
);
});
app.get("/", (req, res) => {
res.send(`
Button Phone Video Converter
`);
});
// --------------------------------------------------
// Video conversion
// --------------------------------------------------
app.post(
"/convert",
upload.single("video"),
async (req, res) => {
if (!req.file) {
return res
.status(400)
.send(
"No video file received."
);
}
const inputFile =
req.file.path;
const originalName =
path.parse(
req.file.originalname
).name;
const safeName =
originalName
.replace(
/[^a-zA-Z0-9_-]/g,
"_"
)
.slice(0, 100);
const outputName =
safeName +
"_144p_MPEG4.mp4";
const outputFile =
path.join(
OUTPUT_DIR,
outputName
);
const ffmpegArgs = [
"-y",
"-i",
inputFile,
"-vf",
"scale=256:144:force_original_aspect_ratio=decrease,pad=256:144:(ow-iw)/2:(oh-ih)/2",
"-r",
"15",
"-c:v",
"mpeg4",
"-b:v",
"180k",
"-c:a",
"aac",
"-ac",
"1",
"-b:a",
"32k",
"-ar",
"44100",
"-movflags",
"+faststart",
outputFile
];
console.log(
"Starting FFmpeg conversion..."
);
console.log(
"Input:",
req.file.originalname
);
try {
await new Promise(
(resolve, reject) => {
execFile(
"ffmpeg",
ffmpegArgs,
{
maxBuffer:
10 * 1024 * 1024
},
(
error,
stdout,
stderr
) => {
if (error) {
console.error(
"FFmpeg error:"
);
console.error(
stderr
);
reject(
new Error(
"FFmpeg conversion failed.\\n\\n" +
stderr.slice(-3000)
)
);
return;
}
resolve();
}
);
}
);
if (
!fs.existsSync(
outputFile
)
) {
throw new Error(
"FFmpeg finished but output file was not created."
);
}
console.log(
"Conversion completed:",
outputName
);
return res.json({
success: true,
filename:
outputName,
downloadUrl:
"/download/" +
encodeURIComponent(
outputName
)
});
}
catch (error) {
console.error(
error
);
return res
.status(500)
.send(
error.message
);
}
finally {
try {
if (
fs.existsSync(
inputFile
)
) {
fs.unlinkSync(
inputFile
);
}
}
catch (_) {}
}
}
);
// --------------------------------------------------
// Download converted video
// --------------------------------------------------
app.get(
"/download/:filename",
(req, res) => {
const filename =
path.basename(
decodeURIComponent(
req.params.filename
)
);
const filePath =
path.join(
OUTPUT_DIR,
filename
);
if (
!fs.existsSync(
filePath
)
) {
return res
.status(404)
.send(
"Converted file not found."
);
}
res.download(
filePath,
filename
);
}
);
// --------------------------------------------------
// Start server
// --------------------------------------------------
app.listen(
PORT,
"0.0.0.0",
() => {
console.log(
"Cloud Button Phone Video Converter running on port " +
PORT
);
}
);
// add start now 1
Ready for Start Now test.
// add start now 1 end
Cloud Button Phone Video Converter
Clean Local Testing Version
Target: 144p • MPEG-4 Part 2 • MP4 • 15 FPS • AAC mono 32 kbps
Ready For Convert V1
Select a video to begin.
Comments
Post a Comment