forked from qwertyforce/4chan_tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebm_to_mp4.js
43 lines (39 loc) · 1.3 KB
/
webm_to_mp4.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
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
const axios = require('axios');
const stream = require('stream');
ffmpeg.setFfmpegPath(ffmpegPath);
const fs=require('fs')
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for ( let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
async function convert(video_link) {
const bufferStream = new stream.PassThrough();
return new Promise((resolve, reject) => {
axios.get(video_link, { responseType: 'arraybuffer' }).then((data) => {
let random_name = makeid(10)
let output = `./${random_name}.mp4`
bufferStream.end(data.data)
ffmpeg(bufferStream)
.size('960x540').audioCodec('aac').videoCodec('libx264').on('end', () => {
fs.readFile(output, (err, data) => {
if (err) throw err;
fs.unlink(output, (err) => {
if (err) {
console.error(err)
return
}
resolve(data)
})
})
}).save(output)
});
})
}
module.exports = {convert};