-
Notifications
You must be signed in to change notification settings - Fork 4
/
ffprobe.ts
86 lines (79 loc) · 1.6 KB
/
ffprobe.ts
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
import {
FFprobeBinaryNotFound,
FFprobeBinaryPermissionDenied,
FFprobeCommandFailed,
} from "./errors.ts";
import type { MediaInfo } from "./media_info.ts";
export interface FFprobeOptions {
cwd?: string;
binary?: string;
args?: Array<string>;
}
export async function ffprobe(
input: string,
{ cwd, binary, args }: FFprobeOptions,
): Promise<MediaInfo> {
if (!binary) {
binary = "ffprobe";
}
if (!args) {
args = [];
}
const cmd = [
binary,
"-hide_banner",
"-print_format",
"json",
"-show_format",
"-show_streams",
...args,
input,
];
let process: Deno.Process;
try {
process = Deno.run({
cmd,
cwd,
stdout: "piped",
stderr: "piped",
});
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new FFprobeBinaryNotFound({
binary,
cwd,
inputFile: input,
cmd,
previous: error,
});
} else if (error instanceof Deno.errors.PermissionDenied) {
throw new FFprobeBinaryPermissionDenied({
binary,
cwd,
inputFile: input,
cmd,
previous: error,
});
}
throw error;
}
const status = await process.status();
if (!status.success) {
process.stdout?.close();
process.close();
throw new FFprobeCommandFailed({
binary,
cwd,
inputFile: input,
cmd,
status,
stderrOutput: await process.stderrOutput(),
});
}
const output = await process.output();
process.stderr?.close();
process.close();
return JSON.parse(
new TextDecoder().decode(output),
);
}