-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsub.mjs
141 lines (124 loc) · 3.52 KB
/
sub.mjs
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
import { execSync } from "node:child_process";
import {
existsSync,
rmSync,
writeFileSync,
lstatSync,
mkdirSync,
readdirSync,
} from "node:fs";
import path from "path";
import {
WHISPER_LANG,
WHISPER_MODEL,
WHISPER_PATH,
WHISPER_VERSION,
} from "./whisper-config.mjs";
import {
downloadWhisperModel,
installWhisperCpp,
transcribe,
toCaptions,
} from "@remotion/install-whisper-cpp";
const extractToTempAudioFile = (fileToTranscribe, tempOutFile) => {
// Extracting audio from mp4 and save it as 16khz wav file
execSync(
`npx remotion ffmpeg -i "${fileToTranscribe}" -ar 16000 "${tempOutFile}" -y`,
{ stdio: ["ignore", "inherit"] },
);
};
const subFile = async (filePath, fileName, folder) => {
const outPath = path.join(
process.cwd(),
"public",
folder,
fileName.replace(".wav", ".json"),
);
const whisperCppOutput = await transcribe({
inputPath: filePath,
model: WHISPER_MODEL,
tokenLevelTimestamps: true,
whisperPath: WHISPER_PATH,
printOutput: false,
translateToEnglish: false,
language: WHISPER_LANG,
splitOnWord: true,
});
const { captions } = toCaptions({
whisperCppOutput,
});
writeFileSync(
outPath.replace("webcam", "subs"),
JSON.stringify(captions, null, 2),
);
};
const processVideo = async (fullPath, entry, directory) => {
if (
!fullPath.endsWith(".mp4") &&
!fullPath.endsWith(".webm") &&
!fullPath.endsWith(".mkv") &&
!fullPath.endsWith(".mov")
) {
return;
}
const isTranscribed = existsSync(
fullPath
.replace(/.mp4$/, ".json")
.replace(/.mkv$/, ".json")
.replace(/.mov$/, ".json")
.replace(/.webm$/, ".json")
.replace("webcam", "subs"),
);
if (isTranscribed) {
return;
}
let shouldRemoveTempDirectory = false;
if (!existsSync(path.join(process.cwd(), "temp"))) {
mkdirSync(`temp`);
shouldRemoveTempDirectory = true;
}
console.log("Extracting audio from file", entry);
const tempWavFileName = entry.split(".")[0] + ".wav";
const tempOutFilePath = path.join(process.cwd(), `temp/${tempWavFileName}`);
extractToTempAudioFile(fullPath, tempOutFilePath);
await subFile(
tempOutFilePath,
tempWavFileName,
path.relative("public", directory),
);
if (shouldRemoveTempDirectory) {
rmSync(path.join(process.cwd(), "temp"), { recursive: true });
}
};
const processDirectory = async (directory) => {
const entries = readdirSync(directory).filter((f) => f !== ".DS_Store");
for (const entry of entries) {
const fullPath = path.join(directory, entry);
const stat = lstatSync(fullPath);
if (stat.isDirectory()) {
await processDirectory(fullPath); // Recurse into subdirectories
} else {
await processVideo(fullPath, entry, directory);
}
}
};
await installWhisperCpp({ to: WHISPER_PATH, version: WHISPER_VERSION });
await downloadWhisperModel({ folder: WHISPER_PATH, model: WHISPER_MODEL });
// Read arguments for filename if given else process all files in the directory
const hasArgs = process.argv.length > 2;
if (!hasArgs) {
await processDirectory(path.join(process.cwd(), "public"));
process.exit(0);
}
for (const arg of process.argv.slice(2)) {
const fullPath = path.join(process.cwd(), arg);
const stat = lstatSync(fullPath);
if (stat.isDirectory()) {
await processDirectory(fullPath);
continue;
}
console.log(`Processing file ${fullPath}`);
const directory = path.dirname(fullPath);
const fileName = path.basename(fullPath);
await processVideo(fullPath, fileName, directory);
}