-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
154 lines (138 loc) · 4.77 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
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
import { parseArgs } from "jsr:@std/cli@1.0.6/parse-args";
import { WebVTTParser, WebVTTSerializer } from "npm:webvtt-parser-esm";
// Types
interface Segment {
id: number;
start: number;
end: number;
text: string;
}
interface TranscriptionResponse {
task: string;
language: string;
duration: number;
text: string;
segments: Segment[];
}
// Utils
function formatTime(seconds: number): string {
const pad = (num: number): string => num.toString().padStart(2, '0');
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
const ms = Math.floor((seconds % 1) * 1000);
return `${pad(hours)}:${pad(minutes)}:${pad(secs)}.${ms.toString().padStart(3, '0')}`;
}
// Converter
function jsonToVTT(json: TranscriptionResponse): string {
let vtt = "WEBVTT\n\n";
json.segments.forEach((segment) => {
vtt += `${formatTime(segment.start)} --> ${formatTime(segment.end)}\n`;
vtt += `${segment.text.trim()}\n\n`;
});
return vtt;
}
// Validators and Serializer
function validateAndSerializeVTT(vttContent: string): { isValid: boolean; serializedVtt: string } {
const parser = new WebVTTParser();
const parsedVtt = parser.parse(vttContent);
const serializer = new WebVTTSerializer();
const serializedVtt = serializer.serialize(parsedVtt.cues);
return { isValid: parsedVtt.errors.length === 0, serializedVtt };
}
function validateJSON(jsonContent: string): boolean {
try {
const parsed = JSON.parse(jsonContent) as TranscriptionResponse;
return (
typeof parsed.task === 'string' &&
typeof parsed.language === 'string' &&
typeof parsed.duration === 'number' &&
typeof parsed.text === 'string' &&
Array.isArray(parsed.segments) &&
parsed.segments.every(segment =>
typeof segment.id === 'number' &&
typeof segment.start === 'number' &&
typeof segment.end === 'number' &&
typeof segment.text === 'string'
)
);
} catch {
return false;
}
}
// Main function
async function main() {
const args = parseArgs(Deno.args, {
boolean: ["validate", "silent", "help"],
string: ["input"],
alias: { input: "i", validate: "v", silent: "s", help: "h" },
});
if (args.help) {
console.log(`
Usage: deno run --allow-read --allow-write main.ts [OPTIONS]
Options:
-i, --input <file> Input file (JSON or VTT) (required)
-v, --validate Validate the input file without conversion
-s, --silent Suppress all console output
-h, --help Show this help message
`);
Deno.exit(0);
}
const inputFile = args.input;
const validateOnly = args.validate;
const silent = args.silent;
if (!inputFile) {
if (!silent) console.error("Please provide an input file using --input or -i.");
Deno.exit(1);
}
try {
const fileContent = await Deno.readTextFile(inputFile);
const isJson = inputFile.toLowerCase().endsWith('.json');
const isVtt = inputFile.toLowerCase().endsWith('.vtt');
if (!isJson && !isVtt) {
if (!silent) console.error("Input file must be either JSON or VTT.");
Deno.exit(1);
}
if (isJson) {
const isValidJson = validateJSON(fileContent);
if (validateOnly) {
if (!silent) console.log(isValidJson ? "JSON content is valid." : "JSON content is invalid.");
Deno.exit(isValidJson ? 0 : 1);
}
if (isValidJson) {
const transcriptionResponse = JSON.parse(fileContent) as TranscriptionResponse;
const vttContent = jsonToVTT(transcriptionResponse);
const { isValid, serializedVtt } = validateAndSerializeVTT(vttContent);
if (isValid) {
const outputFile = inputFile.replace(/\.json$/, '.vtt');
await Deno.writeTextFile(outputFile, serializedVtt);
if (!silent) console.log(`VTT file has been created: ${outputFile}`);
} else {
if (!silent) console.error("Generated VTT content is invalid. Conversion aborted.");
Deno.exit(1);
}
} else {
if (!silent) console.error("Invalid JSON content. Conversion aborted.");
Deno.exit(1);
}
} else { // isVtt
const { isValid, serializedVtt } = validateAndSerializeVTT(fileContent);
if (!silent) {
console.log(isValid ? "VTT content is valid." : "VTT content is invalid.");
}
if (!validateOnly && isValid) {
await Deno.writeTextFile(inputFile, serializedVtt); // Overwrite with serialized content
if (!silent) console.log(`VTT file has been serialized and saved: ${inputFile}`);
}
Deno.exit(isValid ? 0 : 1);
}
} catch (error: unknown) {
if (!silent) {
console.error("An error occurred:", error instanceof Error ? error.message : String(error));
}
Deno.exit(1);
}
}
if (import.meta.main) {
main();
}