File size: 5,688 Bytes
a5c4cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d4ed3
a5c4cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let currentSessionId = null;
let statusCheckInterval = null;

// File input handler
document.getElementById('videoInput').addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        document.getElementById('fileName').textContent = `Selected: ${file.name} (${formatFileSize(file.size)})`;
        document.getElementById('optionsBox').style.display = 'block';
    }
});

function formatFileSize(bytes) {
    if (bytes === 0) return '0 Bytes';
    const k = 1024;
    const sizes = ['Bytes', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}

async function processVideo() {
    const fileInput = document.getElementById('videoInput');
    const file = fileInput.files[0];
    
    if (!file) {
        alert('Please select a video file first');
        return;
    }
    
    // Show progress section
    document.getElementById('progressSection').style.display = 'block';
    document.getElementById('uploadSection')?.style.setProperty('display', 'none');
    document.getElementById('resultsSection').style.display = 'none';
    
    // Create form data
    const formData = new FormData();
    formData.append('video', file);
    formData.append('whisper_model', document.getElementById('whisperModel').value);
    
    try {
        // Upload file
        const response = await fetch('/api/upload', {
            method: 'POST',
            body: formData
        });
        
        if (!response.ok) {
            const error = await response.json();
            throw new Error(error.error || 'Upload failed');
        }
        
        const data = await response.json();
        currentSessionId = data.session_id;
        
        // Start checking status
        checkStatus();
        
    } catch (error) {
        alert('Error: ' + error.message);
        resetForm();
    }
}

function checkStatus() {
    if (!currentSessionId) return;
    
    statusCheckInterval = setInterval(async () => {
        try {
            const response = await fetch(`/api/status/${currentSessionId}`);
            if (!response.ok) {
                throw new Error('Status check failed');
            }
            
            const status = await response.json();
            updateProgress(status);
            
            if (status.status === 'completed' || status.status === 'error') {
                clearInterval(statusCheckInterval);
                
                if (status.status === 'completed') {
                    showResults(status);
                } else {
                    alert('Error: ' + status.message);
                    resetForm();
                }
            }
        } catch (error) {
            console.error('Status check error:', error);
            clearInterval(statusCheckInterval);
            alert('Error checking status');
            resetForm();
        }
    }, 2000); // Check every 2 seconds
}

function updateProgress(status) {
    const progressFill = document.getElementById('progressFill');
    const progressText = document.getElementById('progressText');
    const progressDetails = document.getElementById('progressDetails');
    
    progressFill.style.width = status.progress + '%';
    progressText.textContent = status.message;
    
    if (status.output) {
        // Extract useful info from output
        const lines = status.output.split('\n');
        const lastLines = lines.slice(-5).join('\n');
        progressDetails.textContent = lastLines;
    }
}

function showResults(status) {
    document.getElementById('progressSection').style.display = 'none';
    document.getElementById('resultsSection').style.display = 'block';
    
    // Store file paths in data attributes
    const videoBtn = document.querySelector('[onclick="downloadFile(\'video\')"]');
    const subtitleBtn = document.querySelector('[onclick="downloadFile(\'subtitle\')"]');
    
    if (status.video_path) {
        videoBtn.disabled = false;
        videoBtn.dataset.path = status.video_path;
    } else {
        videoBtn.disabled = true;
    }
    
    if (status.subtitle_path) {
        subtitleBtn.disabled = false;
        subtitleBtn.dataset.path = status.subtitle_path;
    } else {
        subtitleBtn.disabled = true;
    }
}

async function downloadFile(fileType) {
    if (!currentSessionId) return;
    
    try {
        const response = await fetch(`/api/download/${currentSessionId}/${fileType}`);
        if (!response.ok) {
            throw new Error('Download failed');
        }
        
        const blob = await response.blob();
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileType === 'video' ? 'cleaned_video.mp4' : 'cleaned_subtitles.srt';
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
        document.body.removeChild(a);
        
        // Clean up after download
        await fetch(`/api/cleanup/${currentSessionId}`, { method: 'POST' });
        
    } catch (error) {
        alert('Error downloading file: ' + error.message);
    }
}

function resetForm() {
    document.getElementById('videoInput').value = '';
    document.getElementById('fileName').textContent = '';
    document.getElementById('optionsBox').style.display = 'none';
    document.getElementById('progressSection').style.display = 'none';
    document.getElementById('resultsSection').style.display = 'none';
    
    if (statusCheckInterval) {
        clearInterval(statusCheckInterval);
    }
    
    currentSessionId = null;
}