-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (73 loc) · 2.28 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
83
84
85
86
87
88
89
90
91
92
93
94
//importing required modules
const express = require("express");
const app = express();
const cors = require("cors");
const path = require("path");
const shortId = require("shortid");
const Queue = require("bull");
const { exec } = require("child_process");
const { promisify } = require("util");
app.use(cors());
app.use(express.json());
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`server running on port ${port}`);
});
//setting up bull with redis server and creating a new Queue
const videoQueue = new Queue("VideoQueue", {
redis: {
port: "6379",
host: "127.0.0.1",
},
});
app.post("/videoUrl/", async (req, res) => {
const videoUrl = req.body;
console.log(videoUrl.url);
const jobId = shortId.generate();
await videoQueue.add(videoUrl, {
jobId: jobId,
});
return res.json({ jobId });
});
app.get("/job/:jobId", async (req, res) => {
const { jobId } = req.params;
const job = await videoQueue.getJob(jobId);
if (!job) {
return res.status(404).json({ message: "Job not Found" });
}
const jobStatus = await job.getState();
if (jobStatus === "completed") {
return res.json({ status: "completed" });
} else if (jobStatus === "failed") {
return res.json({ status: "failed" });
} else {
return res.json({ status: "in-progress" });
}
});
app.get("/download/:downloadId", (req, res) => {
const downloadId = req.params.downloadId;
console.log(downloadId);
const videoFilePath = `./${downloadId}.mp4`;
const file = path.basename(videoFilePath);
const filePath = path.resolve(videoFilePath);
res.setHeader("Content-Disposition", `attachment; filename=${file}`);
res.setHeader("Content-Type", "video/mp4");
res.sendFile(filePath);
});
const execAsync = promisify(exec);
videoQueue.process(async (job) => {
console.log(job.data.url, job.opts.jobId);
const command = `ffmpeg -i "${job.data.url}" ${job.opts.jobId}.mp4`;
try {
const { stdout, stderr } = await execAsync(command);
if (stderr) {
console.error(`FFmpeg stderr: ${stderr}`);
}
console.log("FFmpeg command executed successfully");
console.log(stdout);
return job.moveToCompleted();
} catch (error) {
job.moveToFailed(new Error("Job failed"));
console.error(`An error occurred: ${error.message}`);
}
});