-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestssssssss.js
More file actions
68 lines (59 loc) · 1.93 KB
/
Copy pathtestssssssss.js
File metadata and controls
68 lines (59 loc) · 1.93 KB
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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const app = express();
const port = 3000;
// Konfigurasi multer untuk upload file
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
// Endpoint untuk konversi WebP ke MP4
app.post('/convert/webp-to-mp4', upload.single('file'), (req, res) => {
const inputPath = req.file.path;
const outputPath = `uploads/${path.basename(req.file.originalname, path.extname(req.file.originalname))}.mp4`;
ffmpeg(inputPath)
.toFormat('mp4')
.on('end', () => {
fs.unlinkSync(inputPath); // Hapus file setelah konversi
res.download(outputPath, () => {
fs.unlinkSync(outputPath); // Hapus file setelah diunduh
});
})
.on('error', (err) => {
res.status(500).send('Error: ' + err.message);
})
.save(outputPath);
});
// Endpoint untuk konversi GIF ke MP4
app.post('/convert/gif-to-mp4', upload.single('file'), (req, res) => {
const inputPath = req.file.path;
const outputPath = `uploads/${path.basename(req.file.originalname, path.extname(req.file.originalname))}.mp4`;
ffmpeg(inputPath)
.toFormat('mp4')
.on('end', () => {
fs.unlinkSync(inputPath); // Hapus file setelah konversi
res.download(outputPath, () => {
fs.unlinkSync(outputPath); // Hapus file setelah diunduh
});
})
.on('error', (err) => {
res.status(500).send('Error: ' + err.message);
})
.save(outputPath);
});
// Pastikan direktori upload ada
if (!fs.existsSync('uploads')) {
fs.mkdirSync('uploads');
}
// Mulai server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});