-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffwrapper.ts
205 lines (179 loc) · 4.36 KB
/
ffwrapper.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { readStringDelim } from "https://deno.land/std/io/mod.ts";
import { readAll } from "https://deno.land/std/streams/mod.ts";
import { clockToSeconds } from "./timeFormats.ts";
import { Weights } from "./weights.ts";
export type VideoData = {
durationSeconds: number;
streams: Stream[];
};
export type Stream = {
index: number;
type: "video" | "audio";
codec: string;
};
export type TrimArgs = {
start: number | undefined;
end: number | undefined;
bitrate: number | undefined;
inputFile: string;
outputFile: string;
audioWeights?: Weights;
copyAudio: boolean;
};
export async function getVideoData(file: string): Promise<VideoData> {
// deno-fmt-ignore
const p = Deno.run({
cmd: [
"ffprobe",
file,
"-v", "error",
"-print_format", "json",
"-show_streams", "-show_format"
],
stdout: "piped",
stderr: "null",
});
const output = JSON.parse(new TextDecoder().decode(await readAll(p.stdout)));
const videoData = {
durationSeconds: Number(output.format.duration),
streams: output.streams.map((stream: any) => ({
index: stream.index,
type: stream.codec_type,
codec: stream.codec_name,
})),
};
if (!validateVideoData(videoData)) {
throw new Error(`invalid video data: ${JSON.stringify(videoData)}`);
}
return videoData;
}
export async function* trim(options: TrimArgs): AsyncGenerator<number> {
const {
start,
end,
bitrate,
inputFile,
audioWeights,
outputFile,
copyAudio,
} = options;
const startArgs = start ? ["-ss", start.toString(10)] : [];
const endArgs = end ? ["-to", end.toString(10)] : [];
const bitrateArgs = bitrate ? ["-b:v", bitrate.toString(10)] : [];
const audioWeightsArgs = audioWeights ? parseAudioWeightsArgs(audioWeights) : [];
const copyAudioArgs = copyAudio ? ["-c:a", "copy"] : [];
const cmd = [
"ffmpeg",
"-i",
inputFile,
...startArgs,
...endArgs,
...bitrateArgs,
...audioWeightsArgs,
...copyAudioArgs,
"-loglevel",
"level+info",
"-y",
outputFile,
];
console.log(cmd.map((s) => `"${s}"`).join(" "));
const p = Deno.run({
cmd,
stdout: "null",
stderr: "piped",
});
yield* readProgressUpdates(p.stderr);
}
// returns an async generator that yields the number of seconds that the encoder has currently encoded.
async function* readProgressUpdates(
stderr: Deno.Reader,
): AsyncGenerator<number> {
const progressRegex = /time=([0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]{2,}))/;
let startedReading = false;
for await (const progressUpdateLine of readStringDelim(stderr, "\r")) {
const groups = progressRegex.exec(progressUpdateLine);
const result = groups?.[1];
if (startedReading && !result) {
break;
}
if (result) {
startedReading = true;
yield clockToSeconds(result);
}
}
}
// audioTracks:
// represents which weights go to which tracks
function parseAudioWeightsArgs(audioWeights: Weights): string[] {
// if empty, we remove all audio tracks
if (audioWeights.type === "none") {
return [
"-map",
"0",
"-map",
"0:a",
];
}
if (audioWeights.type === "single") {
return [
"-map",
"0:v:0",
"-map",
`0:a:${audioWeights.weights.keys().next().value}`,
];
}
if (audioWeights.type === "multi") {
// deno-fmt-ignore
return [
"-filter_complex", amix(audioWeights.weights, false),
"-map", "0:V:0",
"-map", "[aout]",
];
}
// if (audioWeights.type === 'weighted') ...
// deno-fmt-ignore
return [
"-filter_complex", amix(audioWeights.weights, true),
"-map", "0:V:0",
"-map", "[aout]",
];
}
// takes weights, returns amix filter
function amix(weights: Map<string, number>, useWeights: boolean): string {
let amix = "";
for (const track of weights.keys()) {
amix += `[0:a:${track}]`;
}
amix += `amix=inputs=${weights.size}:duration=longest`;
if (useWeights) {
amix += `:weights=${[...weights.values()].join(" ")}`;
}
amix += "[aout]";
return amix;
}
function validateVideoData(data: any): data is VideoData {
if (!data || typeof data !== "object") {
return false;
}
if (typeof data.durationSeconds !== "number") {
return false;
}
if (isNaN(data.durationSeconds)) {
return false;
}
for (const stream of data?.streams) {
if (!stream || typeof stream !== "object") {
return false;
}
if (typeof stream.index !== "number") {
return false;
}
if (stream.type !== "video" && stream.type !== "audio") {
return false;
}
if (typeof stream.codec !== "string") {
return false;
}
}
return true;
}