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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 2 |
+
const loader = document.getElementById('loader');
|
| 3 |
+
// Debug URL parameters
|
| 4 |
+
const urlParams = new URLSearchParams(window.location.search);
|
| 5 |
+
console.log("URL parameters:", Object.fromEntries(urlParams));
|
| 6 |
+
|
| 7 |
+
const folderId = urlParams.get('folder_id');
|
| 8 |
+
if (folderId) {
|
| 9 |
+
//console.log("Folder ID:", folderId);
|
| 10 |
+
//console.log("Expected video path:", `/static/uploads/${folderId}/video.mp4`);
|
| 11 |
+
//console.log("Expected subtitle path:", `/static/uploads/${folderId}/titulky.srt`);
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
const player = videojs('video-player', {
|
| 15 |
+
controls: true,
|
| 16 |
+
fluid: true,
|
| 17 |
+
autoplay: false,
|
| 18 |
+
preload: 'auto',
|
| 19 |
+
playbackRates: [0.5, 1, 1.5, 2],
|
| 20 |
+
html5: {
|
| 21 |
+
vhs: {
|
| 22 |
+
enableLowInitialPlaylist: true
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
});
|
| 26 |
+
console.log("Video.js player initialized");
|
| 27 |
+
|
| 28 |
+
player.ready(() => {
|
| 29 |
+
console.log("Video.js player is ready");
|
| 30 |
+
|
| 31 |
+
// Funkce pro kontrolu a načítání titulků
|
| 32 |
+
function checkAndLoadSubtitles() {
|
| 33 |
+
//console.log("Hledám titulky v elementu 'initial-subtitles' v HTML...");
|
| 34 |
+
const initialSubtitles = document.getElementById('initial-subtitles');
|
| 35 |
+
if (initialSubtitles) {
|
| 36 |
+
console.log("Nalezen element s titulky v HTML šabloně");
|
| 37 |
+
const content = initialSubtitles.textContent;
|
| 38 |
+
console.log("Obsah titulků:", content ? `Nalezen (délka: ${content.length} znaků)` : "Prázdný");
|
| 39 |
+
|
| 40 |
+
if (content && content.trim()) {
|
| 41 |
+
console.log("Načítám nalezené titulky do editoru");
|
| 42 |
+
try {
|
| 43 |
+
SubtitleManager.loadSubtitles(content.trim());
|
| 44 |
+
console.log("Titulky úspěšně načteny do editoru");
|
| 45 |
+
|
| 46 |
+
// Skryj loader, pokud video a titulky jsou připravené
|
| 47 |
+
if (loader) {
|
| 48 |
+
loader.classList.add('loader-hidden');
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// Zastav interval, protože titulky byly úspěšně načteny
|
| 52 |
+
clearInterval(subtitleCheckInterval);
|
| 53 |
+
} catch (error) {
|
| 54 |
+
console.error("Chyba při načítání titulků:", error);
|
| 55 |
+
}
|
| 56 |
+
} else {
|
| 57 |
+
console.warn("Element s titulky byl nalezen, ale je prázdný");
|
| 58 |
+
}
|
| 59 |
+
} else {
|
| 60 |
+
//console.log("Titulky nebyly nalezeny v HTML elementu 'initial-subtitles'. Cesta v šabloně: {{ subtitle_content | safe }}");
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// Opakovaná kontrola titulků každých 2 sekundy
|
| 65 |
+
const subtitleCheckInterval = setInterval(checkAndLoadSubtitles, 2000);
|
| 66 |
+
|
| 67 |
+
// Kontrola i při načtení videa
|
| 68 |
+
checkAndLoadSubtitles();
|
| 69 |
+
|
| 70 |
+
// Inicializace Timeline Manageru
|
| 71 |
+
TimelineManager.init(player);
|
| 72 |
+
|
| 73 |
+
// Skrytí loaderu, pokud je video načteno a titulky jsou přítomné
|
| 74 |
+
const videoSource = player.currentSrc();
|
| 75 |
+
if (loader && videoSource) {
|
| 76 |
+
console.log("Loader hidden as video source is loaded");
|
| 77 |
+
}
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
const videoUpload = document.getElementById('video-upload');
|
| 82 |
+
const subtitleUpload = document.getElementById('subtitle-upload');
|
| 83 |
+
const exportVttBtn = document.getElementById('export-vtt');
|
| 84 |
+
const exportSrtBtn = document.getElementById('export-srt');
|
| 85 |
+
|
| 86 |
+
videoUpload.addEventListener('change', function(e) {
|
| 87 |
+
const file = e.target.files[0];
|
| 88 |
+
if (file) {
|
| 89 |
+
const formData = new FormData();
|
| 90 |
+
formData.append('video', file);
|
| 91 |
+
|
| 92 |
+
fetch('/upload_video', {
|
| 93 |
+
method: 'POST',
|
| 94 |
+
body: formData
|
| 95 |
+
})
|
| 96 |
+
.then(response => response.json())
|
| 97 |
+
.then(data => {
|
| 98 |
+
if (data.success) {
|
| 99 |
+
player.src({
|
| 100 |
+
type: 'video/mp4',
|
| 101 |
+
src: data.filepath
|
| 102 |
+
});
|
| 103 |
+
player.load();
|
| 104 |
+
}
|
| 105 |
+
})
|
| 106 |
+
.catch(error => console.error('Error:', error));
|
| 107 |
+
}
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
subtitleUpload.addEventListener('change', function(e) {
|
| 111 |
+
const file = e.target.files[0];
|
| 112 |
+
if (file) {
|
| 113 |
+
const formData = new FormData();
|
| 114 |
+
formData.append('subtitle', file);
|
| 115 |
+
|
| 116 |
+
fetch('/upload_subtitle', {
|
| 117 |
+
method: 'POST',
|
| 118 |
+
body: formData
|
| 119 |
+
})
|
| 120 |
+
.then(response => response.json())
|
| 121 |
+
.then(data => {
|
| 122 |
+
if (data.success) {
|
| 123 |
+
SubtitleManager.loadSubtitles(data.content);
|
| 124 |
+
TimelineManager.render();
|
| 125 |
+
}
|
| 126 |
+
})
|
| 127 |
+
.catch(error => console.error('Error:', error));
|
| 128 |
+
}
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
exportVttBtn.addEventListener('click', function() {
|
| 134 |
+
const vtt = SubtitleManager.exportVTT();
|
| 135 |
+
downloadFile(vtt, 'subtitles.vtt', 'text/vtt');
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
exportSrtBtn.addEventListener('click', function() {
|
| 139 |
+
const srt = SubtitleManager.exportSRT();
|
| 140 |
+
downloadFile(srt, 'subtitles.srt', 'text/plain');
|
| 141 |
+
});
|
| 142 |
+
|
| 143 |
+
document.getElementById('generate-subtitles').addEventListener('click', function() {
|
| 144 |
+
const videoFile = videoUpload.files[0];
|
| 145 |
+
if (!videoFile) {
|
| 146 |
+
alert('Please upload a video file first');
|
| 147 |
+
return;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
const formData = new FormData();
|
| 151 |
+
formData.append('video', videoFile);
|
| 152 |
+
|
| 153 |
+
this.disabled = true;
|
| 154 |
+
this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating...';
|
| 155 |
+
|
| 156 |
+
fetch('/generate_subtitles', {
|
| 157 |
+
method: 'POST',
|
| 158 |
+
body: formData
|
| 159 |
+
})
|
| 160 |
+
.then(response => response.json())
|
| 161 |
+
.then(data => {
|
| 162 |
+
if (data.success) {
|
| 163 |
+
SubtitleManager.subtitles = [];
|
| 164 |
+
data.subtitles.forEach(subtitle => {
|
| 165 |
+
SubtitleManager.addSubtitle(subtitle.start, subtitle.end, subtitle.text);
|
| 166 |
+
});
|
| 167 |
+
TimelineManager.render();
|
| 168 |
+
} else {
|
| 169 |
+
alert('Error generating subtitles: ' + data.error);
|
| 170 |
+
}
|
| 171 |
+
})
|
| 172 |
+
.catch(error => {
|
| 173 |
+
console.error('Error:', error);
|
| 174 |
+
alert('Error generating subtitles');
|
| 175 |
+
})
|
| 176 |
+
.finally(() => {
|
| 177 |
+
this.disabled = false;
|
| 178 |
+
this.innerHTML = '<i class="fas fa-magic"></i> Generate Subtitles';
|
| 179 |
+
});
|
| 180 |
+
});
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
function downloadFile(content, filename, type) {
|
| 185 |
+
const blob = new Blob([content], { type: type });
|
| 186 |
+
const url = window.URL.createObjectURL(blob);
|
| 187 |
+
const a = document.createElement('a');
|
| 188 |
+
a.href = url;
|
| 189 |
+
a.download = filename;
|
| 190 |
+
a.click();
|
| 191 |
+
window.URL.revokeObjectURL(url);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// Add subtitle button functionality
|
| 195 |
+
document.getElementById('add-subtitle').addEventListener('click', function() {
|
| 196 |
+
const currentTime = player.currentTime();
|
| 197 |
+
SubtitleManager.addSubtitle(currentTime, currentTime + 2, 'New subtitle');
|
| 198 |
+
TimelineManager.render();
|
| 199 |
+
});
|
| 200 |
+
|
| 201 |
+
// Save subtitle button functionality
|
| 202 |
+
document.getElementById('saveSubtitle').addEventListener('click', function() {
|
| 203 |
+
SubtitleManager.updateParentEditor();
|
| 204 |
+
});
|
| 205 |
+
|
| 206 |
+
// Export translation button functionality
|
| 207 |
+
document.getElementById('exportTranslation').addEventListener('click', function() {
|
| 208 |
+
const srt = SubtitleManager.exportSRT();
|
| 209 |
+
|
| 210 |
+
// Aktualizace session storage
|
| 211 |
+
window.parent.sessionStorage.setItem('subtitles', srt);
|
| 212 |
+
|
| 213 |
+
console.log('Exportovaný překlad (SRT formát):');
|
| 214 |
+
console.log('----------------------------------------');
|
| 215 |
+
console.log(srt);
|
| 216 |
+
console.log('----------------------------------------');
|
| 217 |
+
console.log('Session storage "subtitles" byl aktualizován');
|
| 218 |
+
console.log("Všechny session:");
|
| 219 |
+
for (let i = 0; i < sessionStorage.length; i++) {
|
| 220 |
+
const key = sessionStorage.key(i);
|
| 221 |
+
const value = sessionStorage.getItem(key);
|
| 222 |
+
console.log(`${key}: ${value}`);
|
| 223 |
+
}
|
| 224 |
+
// Odeslání zprávy o změně titulků do rodičovské stránky
|
| 225 |
+
window.parent.postMessage({
|
| 226 |
+
type: 'subtitlesChanged',
|
| 227 |
+
data: srt
|
| 228 |
+
}, '*');
|
| 229 |
+
// Notifikace uživateli
|
| 230 |
+
if (window.parent.showToast) {
|
| 231 |
+
window.parent.showToast('Překlad byl úspěšně aktualizován', false);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
});
|
| 235 |
+
// Funkce pro odeslání zprávy o změně titulků do rodičovské stránky
|
| 236 |
+
function notifySubtitlesChange(subtitlesContent) {
|
| 237 |
+
window.parent.postMessage({
|
| 238 |
+
type: 'subtitlesChanged',
|
| 239 |
+
data: subtitlesContent
|
| 240 |
+
}, '*');
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
// Handler pro tlačítko vytvoření dabingu
|
| 244 |
+
document.getElementById('createDubbing').addEventListener('click', async function() {
|
| 245 |
+
try {
|
| 246 |
+
// Získat titulky ze session storage
|
| 247 |
+
const subtitles = SubtitleManager.getSubtitles();
|
| 248 |
+
if (!subtitles || subtitles.length === 0) {
|
| 249 |
+
alert('Nejprve musíte upravit titulky!');
|
| 250 |
+
return;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
// Převést titulky na JSON ve správném formátu pro backend
|
| 254 |
+
const formattedSubtitles = subtitles.map((sub, index) => ({
|
| 255 |
+
id: index + 1,
|
| 256 |
+
start: sub.start,
|
| 257 |
+
end: sub.end,
|
| 258 |
+
text: sub.text,
|
| 259 |
+
translation: sub.text // Text je už přeložený
|
| 260 |
+
}));
|
| 261 |
+
const subtitlesJson = JSON.stringify(formattedSubtitles);
|
| 262 |
+
|
| 263 |
+
// Získat formulář z hlavní stránky
|
| 264 |
+
const mainForm = window.parent.document.getElementById('translateForm');
|
| 265 |
+
const formData = new FormData(mainForm);
|
| 266 |
+
|
| 267 |
+
// Přidat titulky jako JSON
|
| 268 |
+
formData.append('edited_subtitles', subtitlesJson);
|
| 269 |
+
|
| 270 |
+
// Přidat cestu k videu ze session
|
| 271 |
+
const videoPath = sessionStorage.getItem('video_path');
|
| 272 |
+
if (videoPath) {
|
| 273 |
+
formData.append('downloaded_video_path', videoPath);
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
// Odeslat požadavek na vytvoření dabingu
|
| 277 |
+
const response = await fetch('/translate', {
|
| 278 |
+
method: 'POST',
|
| 279 |
+
body: formData
|
| 280 |
+
});
|
| 281 |
+
|
| 282 |
+
const result = await response.json();
|
| 283 |
+
|
| 284 |
+
if (result.success) {
|
| 285 |
+
// Přehrát výsledné video
|
| 286 |
+
if (result.video) {
|
| 287 |
+
const videoPlayer = document.getElementById('video-player');
|
| 288 |
+
if (videoPlayer) {
|
| 289 |
+
videoPlayer.src = result.video;
|
| 290 |
+
videoPlayer.load();
|
| 291 |
+
}
|
| 292 |
+
}
|
| 293 |
+
alert('Dabing byl úspěšně vytvořen!');
|
| 294 |
+
} else {
|
| 295 |
+
alert('Chyba při vytváření dabingu: ' + result.error);
|
| 296 |
+
}
|
| 297 |
+
} catch (error) {
|
| 298 |
+
console.error('Error:', error);
|
| 299 |
+
alert('Chyba při vytváření dabingu: ' + error.message);
|
| 300 |
+
}
|
| 301 |
+
});
|
| 302 |
+
|
| 303 |
+
// Update timeline when video metadata is loaded
|
| 304 |
+
player.on('loadedmetadata', function() {
|
| 305 |
+
console.log("Video metadata loaded, duration:", player.duration());
|
| 306 |
+
TimelineManager.render();
|
| 307 |
+
});
|
| 308 |
+
});
|