-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
82 lines (71 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require("express");
const multer = require("multer");
const hbjs = require("handbrake-js");
const path = require("path");
const fs = require("fs");
const app = express();
const port = 3000;
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads");
},
filename: function (req, file, cb) {
const uniquePrefix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, uniquePrefix + "-" + file.originalname);
},
});
const upload = multer({ storage: storage });
// set the view engine to ejs
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("pages/index");
});
app.post("/upload", upload.single("uploaded_file"), (req, res) => {
const outputFileType = ".mp4";
if (!req.file) {
return res.status(400).send("No file uploaded.");
}
console.log("File uploaded:", req.file);
console.log("File path:", req.file.path);
const outputFileName = `transcoded-${req.file.originalname}`
.split(".")
.slice(0, -1)
.join(".")
.concat(outputFileType);
const outputPath = path.join(__dirname, "public", outputFileName);
hbjs
.spawn({ input: req.file.path, output: outputPath })
.on("begin", () => {
console.log("begin");
})
.on("error", (err) => {
console.error("Error processing video:", err);
// res.status(500).send("Error processing video.");
})
.on("progress", (progress) => {
console.log(
"Percent complete: %s, ETA: %s",
progress.percentComplete,
progress.eta
);
})
// .on("output", (output) => {
// // Accumulate output in a variable if needed
// // For simplicity, let's ignore output events for now
// })
.on("end", () => {
console.log("Video processing completed successfully.");
// Serve the converted video
const videoURL = `/videos/${outputFileName}`;
res
.status(200)
.send(
`Video processing completed. You can download your video <a href="${videoURL}">here</a>.`
);
});
});
// Serve static files from the public directory
app.use("/videos", express.static(path.join(__dirname, "public")));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});