Skip to content

Commit c51d7c5

Browse files
committed
everything works. Let's deploy.
1 parent d9984da commit c51d7c5

File tree

4 files changed

+139
-29
lines changed

4 files changed

+139
-29
lines changed

index.js

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ const http = require("http");
33
const cors = require("cors");
44
const multer = require("multer");
55
const path = require("path");
6-
const { transcodeVideo } = require("./transcode");
6+
const { transcodeVideo, trimVideo } = require("./transcode");
77

88
const port = process.env.PORT || 3211;
99

1010
// VERY IMPORTANT: WHEN RUNNING IN NODE, USE THESE FLAGS:--experimental-wasm-threads --experimental-wasm-bulk-memory
1111

1212
const app = express();
13+
14+
const fileStorageEngine = multer.diskStorage({
15+
destination: (req, file, cb) => {
16+
cb(null, "./storage");
17+
},
18+
filename: (req, file, cb) => {
19+
cb(null, Date.now() + "--" + file.originalname);
20+
},
21+
});
22+
23+
const uploads = multer({ storage: fileStorageEngine });
24+
1325
app.use(cors());
1426
app.use(express.json());
1527
app.use((_, res, next) => {
@@ -24,19 +36,62 @@ app.get("/", (req, res) => {
2436
res.send({ message: "Mind your business" });
2537
});
2638

27-
app.post("/upload", async (req, res) => {
39+
app.post("/upload", uploads.single("newVideo"), async (req, res) => {
2840
try {
29-
if (req.file) {
41+
console.log(req.file);
42+
res.send({ filename: req.file.filename, filepath: req.file.path });
43+
} catch (e) {
44+
console.log(e);
45+
throw e;
46+
}
47+
});
48+
49+
app.post("/transcode", async (req, res) => {
50+
// res.send({ endpoint: "trim endpoint" });
51+
try {
52+
if (!req.body.filename) {
3053
console.log(req);
3154
throw new Error("no file data");
3255
}
33-
// await transcodeVideo(req.file.originalname, req.file.path);
34-
console.log(req.body);
35-
await transcodeVideo(req.body.filename, req.body.filepath);
36-
res.status(200).send();
37-
} catch (e) {}
56+
57+
console.log(req.body, "line 56");
58+
transcodeVideo(
59+
req.body.filename,
60+
req.body.filepath,
61+
req.body.filetype,
62+
res
63+
);
64+
} catch (e) {
65+
console.log(e);
66+
throw new Error(e);
67+
}
68+
});
69+
70+
app.post("/trim", async (req, res) => {
71+
try {
72+
if (!req.body.filename) {
73+
console.log(req);
74+
throw new Error("no file data");
75+
}
76+
77+
console.log(req.body, "line 56");
78+
trimVideo(
79+
req.body.filename,
80+
req.body.filepath,
81+
req.body.start,
82+
req.body.end,
83+
res
84+
);
85+
} catch (e) {
86+
console.log(e);
87+
throw new Error(e);
88+
}
3889
});
3990

4091
app.listen(port, () => {
41-
console.log(`Example app listening on port ${port}`);
92+
console.log(`Veditor app listening on port ${port}`);
93+
});
94+
95+
process.on("uncaughtException", function (err) {
96+
console.log("UNCAUGHT EXCEPTION - keeping process alive:", err); // err.message is "foobar"
4297
});

test.avi

-1.37 MB
Binary file not shown.

test.mp4

-1.19 MB
Binary file not shown.

transcode.js

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,81 @@ const { createFFmpeg, fetchFile } = require("@ffmpeg/ffmpeg");
33

44
const ffmpeg = createFFmpeg({ log: true });
55

6-
const transcodeVideo = async (filename, filepath) => {
7-
await ffmpeg.load();
8-
ffmpeg.FS(
9-
"writeFile",
10-
"test.mp4",
11-
await fetchFile("http://localhost:3000/assets/video/Real 4K HDR .mp4")
12-
);
13-
await ffmpeg.run(
14-
"-i",
15-
"test.mp4",
16-
"-t",
17-
"2.5",
18-
"-ss",
19-
"2.5",
20-
"-f",
21-
"gif",
22-
"out.gif"
23-
);
24-
await fs.promises.writeFile("./out.gif", ffmpeg.FS("readFile", "out.gif"));
25-
process.exit(0);
6+
const transcodeVideo = async (filename, filepath, filetype, res) => {
7+
try {
8+
if (!ffmpeg.isLoaded()) await ffmpeg.load();
9+
10+
ffmpeg.FS(
11+
"writeFile",
12+
`test.${filename.slice(-3)}`,
13+
await fetchFile(filepath)
14+
);
15+
16+
if (["wav", "m4a", "mp3"].includes(filetype))
17+
await ffmpeg.run(
18+
"-i",
19+
`test.${filename.slice(-3)}`,
20+
"-vn",
21+
"-acodec",
22+
"copy",
23+
`out.${filetype}`
24+
);
25+
else {
26+
await ffmpeg.run(
27+
"-i",
28+
`test.${filename.slice(-3)}`,
29+
`out.${filetype}`
30+
);
31+
}
32+
const newPath = `./transcoded/${Date.now()}-Transcoded----${filename}.${filetype}`;
33+
await fs.promises.writeFile(
34+
newPath,
35+
ffmpeg.FS("readFile", `out.${filetype}`)
36+
);
37+
await res.download(newPath);
38+
} catch (err) {
39+
console.log("Error:", err);
40+
throw err;
41+
}
42+
};
43+
44+
const trimVideo = async (filename, filepath, start, end, res) => {
45+
try {
46+
if (!ffmpeg.isLoaded()) await ffmpeg.load();
47+
48+
ffmpeg.FS(
49+
"writeFile",
50+
`test.${filename.slice(-3)}`,
51+
await fetchFile(filepath)
52+
);
53+
54+
await ffmpeg.run(
55+
"-i",
56+
`test.${filename.slice(-3)}`,
57+
"-ss",
58+
`${start}`,
59+
"-to",
60+
`${end}`,
61+
"-c:v",
62+
"copy",
63+
"-c:a",
64+
"copy",
65+
`out.${filename.slice(-3)}`
66+
);
67+
68+
const newPath = `./transcoded/${Date.now()}-Trimmed----${filename}.${filename.slice(
69+
-3
70+
)}`;
71+
await fs.promises.writeFile(
72+
newPath,
73+
ffmpeg.FS("readFile", `out.${filename.slice(-3)}`)
74+
);
75+
await res.download(newPath);
76+
} catch (err) {
77+
console.log("Error:", err);
78+
throw err;
79+
}
2680
};
2781

2882
module.exports.transcodeVideo = transcodeVideo;
83+
module.exports.trimVideo = trimVideo;

0 commit comments

Comments
 (0)