This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg.js
180 lines (168 loc) · 4.23 KB
/
ffmpeg.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const path = require('path');
const process = require('process');
const child_process = require('child_process');
const os = require('os');
const fs = require('fs');
function parse_duration(str) {
// Duration is in the form HH:MM:SS.fraction
let parts = str.split(":");
for (let idx = 0; idx < parts.length; idx++) {
parts[idx] = parseFloat(parts[idx]);
}
return ((parts[0] * 60) + parts[1]) * 60 + parts[2];
}
class ffmpeg {
constructor(ffpath) {
this.bin = {};
this.bin.ffmpeg = "ffmpeg";
this.bin.ffprobe = "ffprobe";
if (os.platform() == "win32") { // Windows has special behavior - system installed FFmpeg is rare.
let ffmpeg_local = path.join((ffpath ? ffpath : process.cwd()), "bin/ffmpeg.exe");
if (fs.existsSync(ffmpeg_local)) {
this.bin.ffmpeg = ffmpeg_local;
}
let ffprobe_local = path.join((ffpath ? ffpath : process.cwd()), "bin/ffprobe.exe");
if (fs.existsSync(ffmpeg_local)) {
this.bin.ffmpeg = ffprobe_local;
}
}
}
_probeParse(buffer) {
let parsed = JSON.parse(buffer);
if (parsed.streams && (parsed.streams.length > 0)) {
for (let idx = 0; idx < parsed.streams.length; idx++) {
let stream = parsed.streams[idx];
if (stream.tags && stream.tags["DURATION"]) {
stream.duration = parse_duration(stream.tags["DURATION"]);
}
}
}
return parsed;
}
probeSync(file) {
let result = child_process.spawnSync(this.bin.ffprobe,
[
"-hide_banner",
"-v", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
"-i", file
]
);
return this._probeParse(result.stdout);
}
probe(file) {
return new Promise((resolve, reject) => {
let proc = child_process.spawn(this.bin.ffprobe,
[
"-hide_banner",
"-v", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
"-i", file
],
{stdio: ['pipe', 'pipe', 'pipe']}
);
let buffer = "";
proc.stdout.on('data', (data) => {
buffer += data.toString();
});
proc.on('close', (code, signal) => {
if (code == 0) {
try {
resolve(this._probeParse(buffer));
} catch (ex) {
reject(ex);
}
} else {
if (code == null) {
reject(signal);
} else {
reject(code);
}
}
});
proc.on('error', (error) => {
reject(error);
})
});
}
get_encoder_caps(encoder) {
let result = {
hardware: false,
formats: [],
devices: []
};
// Output is a list of options.
let temp = child_process.execFileSync(this.bin.ffmpeg,
[
"-hide_banner",
"-v", "quiet",
"-h", `encoder=${encoder}`
]
).toString();
let lines = temp.split('\r\n');
for (let line of lines) {
if (line.includes("General capabilities:")) {
result.hardware = (line.includes("hardware"));
} else if (line.includes("Supported hardware devices:")) {
let data = line.substr(line.indexOf(':') + 2);
result.devices = data.split(' ');
} else if (line.includes("Supported pixel formats:")) {
let data = line.substr(line.indexOf(':') + 2);
result.formats = data.split(' ');
}
}
return result;
}
ffmpegSync(params) {
return child_process.spawnSync(this.bin.ffmpeg, params);
}
ffmpeg(params) {
return new Promise((resolve, reject) => {
let proc = child_process.spawn(this.bin.ffmpeg, params);
let buf_stdout = "";
let buf_stderr = "";
proc.stdout.on('data', (data) => {
buf_stdout += data.toString();
});
proc.stderr.on('data', (data) => {
buf_stderr += data.toString();
});
proc.on('close', (code, signal) => {
if (code == 0) {
try {
resolve([code, buf_stdout, buf_stderr]);
} catch (ex) {
reject([ex, buf_stdout, buf_stderr]);
}
} else {
if (code == null) {
reject([signal, buf_stdout, buf_stderr]);
} else {
reject([code, buf_stdout, buf_stderr]);
}
}
});
proc.on('error', (error) => {
reject([error, buf_stdout, buf_stderr]);
})
});
}
consolify(_file) {
//_file = path.resolve(_file);
if (os.platform() == 'win32') {
// Need to turn:
// C:\myfiles\model.pkl
// into:
// C\\:/myfiles/model.pkl
_file = _file.replace(/\\/g, '/').replace(':', '\\\\:');
} else {
// No further work needs to be done?
}
return _file;
}
}
module.exports = ffmpeg;