Spaces:
Sleeping
Sleeping
File size: 11,495 Bytes
cfd31a2 |
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 |
document.addEventListener('DOMContentLoaded', function() {
const loader = document.getElementById('loader');
// Debug URL parameters
const urlParams = new URLSearchParams(window.location.search);
console.log("URL parameters:", Object.fromEntries(urlParams));
const folderId = urlParams.get('folder_id');
if (folderId) {
//console.log("Folder ID:", folderId);
//console.log("Expected video path:", `/static/uploads/${folderId}/video.mp4`);
//console.log("Expected subtitle path:", `/static/uploads/${folderId}/titulky.srt`);
}
const player = videojs('video-player', {
controls: true,
fluid: true,
autoplay: false,
preload: 'auto',
playbackRates: [0.5, 1, 1.5, 2],
html5: {
vhs: {
enableLowInitialPlaylist: true
}
}
});
console.log("Video.js player initialized");
player.ready(() => {
console.log("Video.js player is ready");
// Funkce pro kontrolu a načítání titulků
function checkAndLoadSubtitles() {
//console.log("Hledám titulky v elementu 'initial-subtitles' v HTML...");
const initialSubtitles = document.getElementById('initial-subtitles');
if (initialSubtitles) {
console.log("Nalezen element s titulky v HTML šabloně");
const content = initialSubtitles.textContent;
console.log("Obsah titulků:", content ? `Nalezen (délka: ${content.length} znaků)` : "Prázdný");
if (content && content.trim()) {
console.log("Načítám nalezené titulky do editoru");
try {
SubtitleManager.loadSubtitles(content.trim());
console.log("Titulky úspěšně načteny do editoru");
// Skryj loader, pokud video a titulky jsou připravené
if (loader) {
loader.classList.add('loader-hidden');
}
// Zastav interval, protože titulky byly úspěšně načteny
clearInterval(subtitleCheckInterval);
} catch (error) {
console.error("Chyba při načítání titulků:", error);
}
} else {
console.warn("Element s titulky byl nalezen, ale je prázdný");
}
} else {
//console.log("Titulky nebyly nalezeny v HTML elementu 'initial-subtitles'. Cesta v šabloně: {{ subtitle_content | safe }}");
}
}
// Opakovaná kontrola titulků každých 2 sekundy
const subtitleCheckInterval = setInterval(checkAndLoadSubtitles, 2000);
// Kontrola i při načtení videa
checkAndLoadSubtitles();
// Inicializace Timeline Manageru
TimelineManager.init(player);
// Skrytí loaderu, pokud je video načteno a titulky jsou přítomné
const videoSource = player.currentSrc();
if (loader && videoSource) {
console.log("Loader hidden as video source is loaded");
}
});
const videoUpload = document.getElementById('video-upload');
const subtitleUpload = document.getElementById('subtitle-upload');
const exportVttBtn = document.getElementById('export-vtt');
const exportSrtBtn = document.getElementById('export-srt');
videoUpload.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const formData = new FormData();
formData.append('video', file);
fetch('/upload_video', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
player.src({
type: 'video/mp4',
src: data.filepath
});
player.load();
}
})
.catch(error => console.error('Error:', error));
}
});
subtitleUpload.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const formData = new FormData();
formData.append('subtitle', file);
fetch('/upload_subtitle', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
SubtitleManager.loadSubtitles(data.content);
TimelineManager.render();
}
})
.catch(error => console.error('Error:', error));
}
});
exportVttBtn.addEventListener('click', function() {
const vtt = SubtitleManager.exportVTT();
downloadFile(vtt, 'subtitles.vtt', 'text/vtt');
});
exportSrtBtn.addEventListener('click', function() {
const srt = SubtitleManager.exportSRT();
downloadFile(srt, 'subtitles.srt', 'text/plain');
});
document.getElementById('generate-subtitles').addEventListener('click', function() {
const videoFile = videoUpload.files[0];
if (!videoFile) {
alert('Please upload a video file first');
return;
}
const formData = new FormData();
formData.append('video', videoFile);
this.disabled = true;
this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating...';
fetch('/generate_subtitles', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
SubtitleManager.subtitles = [];
data.subtitles.forEach(subtitle => {
SubtitleManager.addSubtitle(subtitle.start, subtitle.end, subtitle.text);
});
TimelineManager.render();
} else {
alert('Error generating subtitles: ' + data.error);
}
})
.catch(error => {
console.error('Error:', error);
alert('Error generating subtitles');
})
.finally(() => {
this.disabled = false;
this.innerHTML = '<i class="fas fa-magic"></i> Generate Subtitles';
});
});
function downloadFile(content, filename, type) {
const blob = new Blob([content], { type: type });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}
// Add subtitle button functionality
document.getElementById('add-subtitle').addEventListener('click', function() {
const currentTime = player.currentTime();
SubtitleManager.addSubtitle(currentTime, currentTime + 2, 'New subtitle');
TimelineManager.render();
});
// Save subtitle button functionality
document.getElementById('saveSubtitle').addEventListener('click', function() {
SubtitleManager.updateParentEditor();
});
// Export translation button functionality
document.getElementById('exportTranslation').addEventListener('click', function() {
const srt = SubtitleManager.exportSRT();
// Aktualizace session storage
window.parent.sessionStorage.setItem('subtitles', srt);
console.log('Exportovaný překlad (SRT formát):');
console.log('----------------------------------------');
console.log(srt);
console.log('----------------------------------------');
console.log('Session storage "subtitles" byl aktualizován');
console.log("Všechny session:");
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
const value = sessionStorage.getItem(key);
console.log(`${key}: ${value}`);
}
// Odeslání zprávy o změně titulků do rodičovské stránky
window.parent.postMessage({
type: 'subtitlesChanged',
data: srt
}, '*');
// Notifikace uživateli
if (window.parent.showToast) {
window.parent.showToast('Překlad byl úspěšně aktualizován', false);
}
});
// Funkce pro odeslání zprávy o změně titulků do rodičovské stránky
function notifySubtitlesChange(subtitlesContent) {
window.parent.postMessage({
type: 'subtitlesChanged',
data: subtitlesContent
}, '*');
}
// Handler pro tlačítko vytvoření dabingu
document.getElementById('createDubbing').addEventListener('click', async function() {
try {
// Získat titulky ze session storage
const subtitles = SubtitleManager.getSubtitles();
if (!subtitles || subtitles.length === 0) {
alert('Nejprve musíte upravit titulky!');
return;
}
// Převést titulky na JSON ve správném formátu pro backend
const formattedSubtitles = subtitles.map((sub, index) => ({
id: index + 1,
start: sub.start,
end: sub.end,
text: sub.text,
translation: sub.text // Text je už přeložený
}));
const subtitlesJson = JSON.stringify(formattedSubtitles);
// Získat formulář z hlavní stránky
const mainForm = window.parent.document.getElementById('translateForm');
const formData = new FormData(mainForm);
// Přidat titulky jako JSON
formData.append('edited_subtitles', subtitlesJson);
// Přidat cestu k videu ze session
const videoPath = sessionStorage.getItem('video_path');
if (videoPath) {
formData.append('downloaded_video_path', videoPath);
}
// Odeslat požadavek na vytvoření dabingu
const response = await fetch('/translate', {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.success) {
// Přehrát výsledné video
if (result.video) {
const videoPlayer = document.getElementById('video-player');
if (videoPlayer) {
videoPlayer.src = result.video;
videoPlayer.load();
}
}
alert('Dabing byl úspěšně vytvořen!');
} else {
alert('Chyba při vytváření dabingu: ' + result.error);
}
} catch (error) {
console.error('Error:', error);
alert('Chyba při vytváření dabingu: ' + error.message);
}
});
// Update timeline when video metadata is loaded
player.on('loadedmetadata', function() {
console.log("Video metadata loaded, duration:", player.duration());
TimelineManager.render();
});
});
|