-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
87 lines (74 loc) · 1.92 KB
/
main.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
import { Flags, parseFlags } from "./parseFlags.ts";
import { getVideoData, trim } from "./ffwrapper.ts";
import { FFTrimError } from "./error.ts";
import { parseWeightsArray } from "./weights.ts";
let flags: Flags;
try {
flags = parseFlags();
} catch (err) {
if (err instanceof FFTrimError) {
console.log(
`usage: ddmpeg -i <input> -o <output> [-t <[start]:[end]>] [-s <size>] [-m <w1>[,w2,w3...]]`,
);
console.error("caused by: " + err);
Deno.exit(1);
} else {
throw err;
}
}
let {
start,
end,
size,
inputFile,
outputFile,
mergeWeights,
} = flags;
const videoData = await getVideoData(inputFile);
const audioStreams = videoData.streams.filter((stream) => stream.type === "audio");
// set defaults for start/end
if (start === undefined) {
start = 0;
}
if (end === undefined) {
end = videoData.durationSeconds;
}
// parse audio weights
if (mergeWeights) {
for (let i = mergeWeights.length; i < audioStreams.length; i++) {
mergeWeights[i] = 1;
}
}
const audioWeights = parseWeightsArray(mergeWeights);
const duration = end - start;
const copyAudio = audioWeights.canCopyAudio;
// trim the video (async)
const progress = trim({
start,
end,
bitrate: size ? size / duration : undefined,
inputFile,
outputFile,
audioWeights,
copyAudio,
});
// print out progress on the trimming
const encoder = new TextEncoder();
for await (const secondsDone of progress) {
const percent = secondsDone / duration;
Deno.stdout.writeSync(
encoder.encode(
`\r${progressBar(30, percent)} (${(percent * 100).toFixed(1)}%)`,
),
);
}
// write a newline
Deno.stdout.writeSync(new Uint8Array([10]));
// ======== UTILITY FUNCTIONS =========
// simple progress bar
function progressBar(width: number, percent: number) {
percent = Math.max(0, Math.min(percent, 1));
const equalsSigns = Math.floor((width - 2) * percent);
const spaces = Math.ceil((width - 2) * (1 - percent));
return `[${"=".repeat(equalsSigns)}${" ".repeat(spaces)}]`;
}