-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
78 lines (69 loc) · 1.86 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
/**
* @description M3U8 to MP4 Converter
* @author Furkan Inanc
* @version 1.0.0
*/
let ffmpeg = require("fluent-ffmpeg");
/**
* A class to convert M3U8 to MP4
* @class
*/
class m3u8ToMp4Converter {
/**
* Sets the input file
* @param {String} filename M3U8 file path. You can use remote URL
* @returns {Function}
*/
setInputFile(filename) {
if (!filename) throw new Error("You must specify the M3U8 file address");
this.M3U8_FILE = filename;
return this;
}
/**
* Sets the output file
* @param {String} filename Output file path. Has to be local :)
* @returns {Function}
*/
setOutputFile(filename) {
if (!filename) throw new Error("You must specify the file path and name");
this.OUTPUT_FILE = filename;
return this;
}
/**
* Starts the process
*/
start(options = {}) {
// https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#setting-event-handlers
options = Object.assign({
onStart: () => { },
onEnd: () => { },
onError: error => {
reject(new Error(error));
},
onProgress: () => { },
onStderr: () => { },
onCodecData: () => { },
}, options);
return new Promise((resolve, reject) => {
if (!this.M3U8_FILE || !this.OUTPUT_FILE) {
reject(new Error("You must specify the input and the output files"));
return;
}
ffmpeg(this.M3U8_FILE)
.on('start', options.onStart)
.on('codecData', options.onCodecData)
.on('progress', options.onProgress)
.on("error", options.onError)
.on('stderr', options.onStderr)
.on("end", (...args) => {
resolve();
options.onEnd(...args);
})
.outputOptions("-c copy")
.outputOptions("-bsf:a aac_adtstoasc")
.output(this.OUTPUT_FILE)
.run();
});
}
}
module.exports = m3u8ToMp4Converter;