Spaces:
Running
Running
File size: 14,115 Bytes
85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 85774d3 5921f34 |
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
document.addEventListener('DOMContentLoaded', () => {
// UI Elements
const inputText = document.getElementById('input-text');
const outputText = document.getElementById('output-text');
const processBtn = document.getElementById('process-btn');
const processBtnMobile = document.getElementById('process-btn-mobile');
const copyBtn = document.getElementById('copy-btn');
const clearBtn = document.getElementById('clear-btn');
const blockCount = document.getElementById('block-count');
const downloadSrtBtn = document.getElementById('download-srt-btn');
// Mode Toggle Elements
const modeTextBtn = document.getElementById('mode-text');
const modeAudioBtn = document.getElementById('mode-audio');
const audioSection = document.getElementById('audio-section');
const inputSection = document.querySelector('section'); // First section is input
// Audio Elements
const audioFileInput = document.getElementById('audio-file');
const apiKeyInput = document.getElementById('api-key');
const transcribeBtn = document.getElementById('transcribe-btn');
const fileLabel = document.getElementById('file-label');
const transcribingStatus = document.getElementById('transcribing-status');
// State
let currentSrtData = null;
let currentTranscriptWords = []; // Stores {word, start, end}
// --- CONSTANTS ---
const MAX_CHARS = 11;
// Tabu words (Articles, Prepositions, Pronouns, Conjunctions)
const TABU_WORDS = new Set([
// PT
'de', 'do', 'da', 'em', 'no', 'na', 'por', 'para', 'com', 'ao', 'a', 'os', 'as',
'e', 'ou', 'mas', 'que', 'se', 'como', 'porque', 'quando', 'então', 'o', 'um', 'uma',
'nos', 'nas', 'me', 'te', 'não', 'só', 'já', 'também', 'tipo', 'nosso', 'nossa',
// EN
'a', 'an', 'the', 'of', 'in', 'on', 'at', 'to', 'for', 'with', 'from', 'by', 'and', 'or', 'but',
// ES
'el', 'la', 'los', 'las', 'un', 'una', 'de', 'en', 'a', 'por', 'para', 'con', 'y', 'o', 'pero', 'que'
]);
const CONNECTIVES = new Set(['e', 'é', 'que', 'and', 'that', 'y']); // Must start new line
// --- MODE SWITCHING ---
modeTextBtn.addEventListener('click', () => {
modeTextBtn.classList.add('mode-active');
modeAudioBtn.classList.remove('mode-active');
audioSection.classList.add('hidden');
inputSection.classList.remove('opacity-50', 'pointer-events-none');
downloadSrtBtn.classList.add('hidden');
outputText.value = '';
blockCount.textContent = '0';
currentSrtData = null;
});
modeAudioBtn.addEventListener('click', () => {
modeAudioBtn.classList.add('mode-active');
modeTextBtn.classList.remove('mode-active');
audioSection.classList.remove('hidden');
// Optional: Disable manual text input when in audio mode to avoid confusion
inputSection.classList.add('opacity-50', 'pointer-events-none');
});
audioFileInput.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
fileLabel.textContent = e.target.files[0].name;
} else {
fileLabel.textContent = 'Click or drag audio file here';
}
});
// --- AUDIO TRANSCRIPTION LOGIC ---
async function handleTranscription() {
const file = audioFileInput.files[0];
const apiKey = apiKeyInput.value.trim();
if (!file) {
showToast("Please select an audio or video file.", "error");
return;
}
if (!apiKey) {
showToast("Please enter your OpenAI API Key.", "error");
return;
}
// UI Loading State
transcribeBtn.disabled = true;
transcribingStatus.classList.remove('hidden');
try {
const formData = new FormData();
formData.append('file', file);
formData.append('model', 'whisper-1');
formData.append('response_format', 'verbose_json');
formData.append('timestamp_granularities', 'word');
const response = await fetch('https://api.openai.com/v1/audio/transcriptions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`
},
body: formData
});
if (!response.ok) {
const errData = await response.json();
throw new Error(errData.error?.message || 'Transcription failed');
}
const data = await response.json();
// Extract words with timestamps
// Whisper returns words array when timestamp_granularities is set
if (!data.words) {
throw new Error("No word-level timestamps returned. Check API plan.");
}
currentTranscriptWords = data.words.map(w => ({
word: w.word,
start: w.start,
end: w.end
}));
// Get full text
const fullText = data.text;
// Process the text with existing logic
const processedBlocks = getProcessedBlocks(fullText);
// Map timestamps to processed blocks
const srtContent = generateSRT(processedBlocks, currentTranscriptWords);
// Display Results
outputText.value = srtContent; // Show SRT format in textarea or just text? Let's show SRT content so they can see timing
blockCount.textContent = processedBlocks.length;
currentSrtData = srtContent;
downloadSrtBtn.classList.remove('hidden');
showToast("Transcription & Alignment complete!");
} catch (error) {
console.error(error);
showToast(error.message, "error");
} finally {
transcribeBtn.disabled = false;
transcribingStatus.classList.add('hidden');
}
}
transcribeBtn.addEventListener('click', handleTranscription);
// --- SRT GENERATION ---
function formatSRTTime(seconds) {
const date = new Date(0);
date.setMilliseconds(seconds * 1000);
const isoString = date.toISOString();
// Extract HH:MM:SS,ms
return isoString.substr(11, 8) + ',' + isoString.substr(20, 3);
}
function generateSRT(blocks, words) {
let srtOutput = "";
let wordIndex = 0;
let blockIndex = 1;
// Normalize punctuation in blocks to match Whisper words (roughly)
// Whisper usually returns words without punctuation attached, or with basic punctuation
// Our script adds "!" for commas.
for (const block of blocks) {
// Split block into words (removing our added punctuation for matching)
// We need to reconstruct the text for display but match based on content
// Simple approach: Count words in the block
// Calculate how many whisper words correspond to this block text
const blockWords = block.replace(/[!?.,]/g, '').trim().split(/\s+/).filter(w => w.length > 0);
const numWords = blockWords.length;
if (numWords === 0) continue;
if (wordIndex >= words.length) break;
// Determine start and end time
// Start is the start of the first word in this chunk
const startTime = words[wordIndex].start;
// End is the end of the last word in this chunk
// Look ahead 'numWords - 1'
let endIndex = wordIndex + numWords - 1;
if (endIndex >= words.length) endIndex = words.length - 1;
const endTime = words[endIndex].end;
// Format Entry
srtOutput += `${blockIndex}\n`;
srtOutput += `${formatSRTTime(startTime)} --> ${formatSRTTime(endTime)}\n`;
srtOutput += `${block}\n\n`;
wordIndex += numWords;
blockIndex++;
}
return srtOutput;
}
downloadSrtBtn.addEventListener('click', () => {
if (!currentSrtData) return;
const blob = new Blob([currentSrtData], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'capcut_aligned.srt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showToast("SRT file downloaded!");
});
// --- UTILITIES ---
function showToast(message, type = 'success') {
const toast = document.createElement('div');
const bgColor = type === 'success' ? 'bg-green-500' : 'bg-red-500';
toast.className = `fixed bottom-5 right-5 ${bgColor} text-white px-6 py-3 rounded-lg shadow-lg flex items-center gap-2 z-50 toast`;
toast.innerHTML = `<i data-feather="${type === 'success' ? 'check-circle' : 'alert-circle'}"></i> ${message}`;
document.body.appendChild(toast);
feather.replace();
setTimeout(() => {
toast.classList.add('hiding');
toast.addEventListener('animationend', () => toast.remove());
}, 3000);
}
function cleanText(text) {
// 1. Remove duplicate spaces
let cleaned = text.replace(/\s+/g, ' ').trim();
// 2. Comma Transformation: Replace , with ! attached to previous word
cleaned = cleaned.replace(/,/g, '!');
// 3. Normalize multiple exclamation marks
cleaned = cleaned.replace(/!+/g, '!');
return cleaned;
}
function isWeakEnding(word) {
if (!word) return false;
// Remove trailing punctuation for the check, but usually in this flow
// the word carries the punctuation from the split logic.
// We check the alphanumeric part length.
const cleanWord = word.replace(/[!?.,]/g, '').toLowerCase();
// 2-3 Letter Ban
if (cleanWord.length <= 3) return true;
// Tabu Words
if (TABU_WORDS.has(cleanWord)) return true;
return false;
}
function countCharsNoSpaces(block) {
return block.replace(/\s/g, '').length;
}
// Separate the text processing logic to be reusable by both text and audio modes
function getProcessedBlocks(raw) {
const cleanedText = cleanText(raw);
const words = cleanedText.split(' ');
let lines = [];
let currentLine = [];
let currentLength = 0;
for (let i = 0; i < words.length; i++) {
let word = words[i];
let nextWord = words[i + 1] || '';
const endsWithPunctuation = /[!?]|(\.\.)/.test(word.slice(-1));
const isConnective = CONNECTIVES.has(word.toLowerCase().replace(/[!?.,]/g, ''));
let startNewLine = false;
if (isConnective && currentLine.length > 0) {
startNewLine = true;
}
const proposedLineStr = [...currentLine, word].join(' ');
const proposedLen = countCharsNoSpaces(proposedLineStr);
if (currentLine.length > 0 && proposedLen > MAX_CHARS) {
startNewLine = true;
}
if (startNewLine) {
lines.push(currentLine.join(' '));
currentLine = [word];
} else {
currentLine.push(word);
}
if (endsWithPunctuation) {
lines.push(currentLine.join(' '));
currentLine = [];
}
}
if (currentLine.length > 0) {
lines.push(currentLine.join(' '));
}
// Anti-Weakening
let changed = true;
let iterations = 0;
while (changed && iterations < 100) {
changed = false;
iterations++;
for (let i = 0; i < lines.length - 1; i++) {
const lineWords = lines[i].split(' ');
const lastWord = lineWords[lineWords.length - 1];
if (isWeakEnding(lastWord)) {
const remainingWords = lineWords.slice(0, lineWords.length - 1);
if (remainingWords.length === 0) {
lines[i] = lines[i+1];
} else {
lines[i] = remainingWords.join(' ');
}
const nextLineWords = lines[i+1].split(' ');
lines[i+1] = [lastWord, ...nextLineWords].join(' ');
if (lines[i].trim() === '') {
lines.splice(i, 1);
i--;
}
changed = true;
}
}
}
return lines.filter(l => l.trim().length > 0);
}
function processScript() {
const raw = inputText.value;
if (!raw.trim()) {
showToast("Please enter text to process.", "error");
return;
}
const lines = getProcessedBlocks(raw);
blockCount.textContent = lines.length;
showToast("Script processed successfully!");
}
// --- Event Listeners ---
const handleProcess = (e) => {
if(e) e.preventDefault();
processScript();
};
processBtn.addEventListener('click', handleProcess);
processBtnMobile.addEventListener('click', handleProcess);
copyBtn.addEventListener('click', () => {
if (!outputText.value) return;
navigator.clipboard.writeText(outputText.value).then(() => {
showToast("Copied to clipboard!");
});
});
clearBtn.addEventListener('click', () => {
inputText.value = '';
outputText.value = '';
blockCount.textContent = '0';
inputText.focus();
});
// Feather icons init backup
setTimeout(() => feather.replace(), 100);
}); |