-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (63 loc) · 2.53 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 app = express();
const port = 9999;
const fs = require('fs');
const path = require('path');
app.use(express.json());
app.post('/chunk', async (req, res) => {
try{
console.log("req",req.body)
let response = { status: false, message: 'Failed to open video file' };
const videoPath = path.join(__dirname, 'public');
// console.log("videoPath",videoPath)
const mediaId = req.body.media_id
let media_path =req.body.media_url
let chunk_size = req.body.chenk_size
// let total_size =parseInt(event.body.total_size)
// let media_path = videoPath + '/a5475c64-e848-4adb-ac33-25a779f2303e.mp4'
// event.body.media_url
//__dirname + '/a5475c64-e848-4adb-ac33-25a779f2303e.mp4'
console.log("media_path",media_path)
if (!fs.existsSync(videoPath)) {
fs.mkdirSync(videoPath, { recursive: true });
}
const videoMediaPath = path.join(__dirname, 'public',mediaId);
if (!fs.existsSync(videoMediaPath)) {
fs.mkdirSync(videoMediaPath, { recursive: true });
}else{
// Clean directory
fs.readdirSync(videoMediaPath).forEach((file) => {
const filePath = path.join(videoMediaPath, file);
fs.unlinkSync(filePath);
});
}
const chunkSize = parseInt(chunk_size) * 1024 * 1024; // 30MB
const sourceFile = fs.openSync(media_path, 'r');
if (sourceFile) {
let i = 1;
let chunkFileList = [];
const buffer = Buffer.alloc(chunkSize);
let bytesRead;
while ((bytesRead = fs.readSync(sourceFile, buffer, 0, chunkSize, null)) > 0) {
const chunkFileName = path.join(videoMediaPath, `chunk_${i}.mp4`);
fs.writeFileSync(chunkFileName, buffer.slice(0, bytesRead));
chunkFileList.push(chunkFileName);
i++;
}
fs.closeSync(sourceFile);
// Sort chunk files by creation time
chunkFileList.sort((a, b) => {
return fs.statSync(a).ctimeMs - fs.statSync(b).ctimeMs;
});
console.log("chunkFileList",chunkFileList)
console.log('video splitting =>', media_path, 'media_upload', 'response');
response = { status: true, message: 'Media splitting successfully', files: chunkFileList };
res.send(response)
}
}catch(err){
throw new Error(err)
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});