-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (51 loc) · 1.63 KB
/
index.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
import 'dotenv/config'
import * as fs from 'node:fs';
import chalk from 'chalk';
import boxen from 'boxen';
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import WitApi from 'node-wit';
import { splitVideo, videoExists, getAudioFiles, generateSRT, cleanUpAudioFiles } from './videoHelper.js';
import { initProgressBar, updateProgressBar } from './progressBar.js';
const splitSize = 5;
const result = [];
console.log(chalk.blue(boxen('this command helps you generate subtitles for your videos', { padding: 1 })))
const args = yargs(hideBin(process.argv))
.option("i", {
alias: "Input video",
describe: "Input video to translate"
})
.help()
.argv;
try {
if(!args || !videoExists(args.i)) {
throw new Error("Video doesnt exist");
}
const witClient = new WitApi.Wit({
accessToken: process.env.WIT_ACCESS_TOKEN
});
console.log("Setting up the video");
await splitVideo(args.i, splitSize);
const files = getAudioFiles();
initProgressBar(files.length);
for(const file of files) {
const fileStream = await fs.createReadStream(file);
const response = await witClient.dictation('audio/mpeg3', fileStream);
await sleep(250);
await fileStream.close();
result.push(response);
updateProgressBar();
}
generateSRT(result, splitSize, args.i);
cleanUpAudioFiles();
} catch (error) {
console.log(chalk.red(`--- ${error.message} ---`));
}
function sleep(time) {
return new Promise((resolve) => {
setTimeout(() => {
/* timeout to not get blocked by the API */
resolve();
}, time);
});
}