-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_video_convert.js
More file actions
260 lines (213 loc) · 9.62 KB
/
script_video_convert.js
File metadata and controls
260 lines (213 loc) · 9.62 KB
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
document.addEventListener('DOMContentLoaded', () => {
const videoInput = document.getElementById('videoInput');
const dropZone = document.querySelector('.drop-zone');
const convertBtn = document.getElementById('convertBtn');
const downloadBtn = document.getElementById('downloadBtn');
const formatSelect = document.getElementById('audioFormat');
const progress = document.getElementById('conversionProgress');
const videoPreview = document.getElementById('videoPreview');
let mediaRecorder;
let recordedChunks = [];
let videoFile = null;
let audioContext;
let progressInterval;
const supportedFormats = ['video/mp4', 'video/webm', 'video/quicktime'];
// Handle file selection
function handleVideoSelect(file) {
if (!file.type.startsWith('video/') || !supportedFormats.includes(file.type)) {
showAlert('Please select a valid video file (MP4, WebM or MOV)!', 'danger');
return;
}
videoFile = file;
const videoURL = URL.createObjectURL(file);
// Show video preview
videoPreview.src = videoURL;
videoPreview.style.display = 'block';
videoPreview.muted = false;
videoPreview.controls = true;
downloadBtn.classList.add('d-none');
convertBtn.disabled = false;
showAlert('Video loaded successfully!', 'success');
}
// Start conversion process
function startConversion() {
if (!videoFile) {
showAlert('Please select a video first!', 'danger');
return;
}
const videoElement = document.createElement('video');
videoElement.src = URL.createObjectURL(videoFile);
videoElement.muted = true; // Required for autoplay
videoElement.playsInline = true;
videoElement.onloadedmetadata = () => {
videoElement.play().catch(e => {
showAlert('Error playing video: ' + e.message, 'danger');
progress.classList.add('d-none');
});
startRecording(videoElement);
};
videoElement.onerror = () => {
showAlert('Error playing video!', 'danger');
progress.classList.add('d-none');
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
}
};
}
// Start media recording
function startRecording(videoElement) {
// Reset progress UI
const progressBar = progress.querySelector('.progress-bar');
progressBar.style.width = '0%';
progressBar.setAttribute('aria-valuenow', '0');
progress.querySelector('.progress-text').textContent = '0%';
document.getElementById('progressPercent').textContent = '0';
document.getElementById('timeElapsed').textContent = '00:00';
document.getElementById('timeRemaining').textContent = '00:00';
recordedChunks = [];
progress.classList.remove('d-none');
try {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
const destination = audioContext.createMediaStreamDestination();
const source = audioContext.createMediaElementSource(videoElement);
// Connect audio nodes properly
source.connect(audioContext.destination); // For playback
source.connect(destination); // For recording
let mimeType = formatSelect.value;
// Handle MP3 format support
if (mimeType === 'audio/mp3' && !MediaRecorder.isTypeSupported('audio/mpeg')) {
showAlert('MP3 format not supported in this browser. Using WAV instead.', 'warning');
mimeType = 'audio/wav';
}
mediaRecorder = new MediaRecorder(destination.stream, {
mimeType: mimeType,
audioBitsPerSecond: 128000
});
mediaRecorder.ondataavailable = event => {
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
mediaRecorder.onstop = () => {
clearInterval(progressInterval);
const audioBlob = new Blob(recordedChunks, { type: mimeType });
const audioUrl = URL.createObjectURL(audioBlob);
downloadBtn.href = audioUrl;
downloadBtn.download = `audio-${Date.now()}${getFileExtension(mimeType)}`;
downloadBtn.classList.remove('d-none');
showAlert('Conversion completed successfully!', 'success');
progress.classList.add('d-none');
// Clean up
if (audioContext) {
audioContext.close().catch(e => console.error('Error closing audio context:', e));
}
};
mediaRecorder.start();
// Update progress
progressInterval = updateProgress(videoElement);
// Set timeout as fallback
const duration = videoElement.duration * 1000 || 60000;
setTimeout(() => {
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
}
}, duration);
} catch (e) {
console.error('Error:', e);
showAlert('Conversion error: ' + e.message, 'danger');
progress.classList.add('d-none');
}
}
// Update progress tracking
function updateProgress(videoElement) {
const progressBar = progress.querySelector('.progress-bar');
const progressText = progress.querySelector('.progress-text');
const conversionDetails = document.getElementById('conversionDetails');
const progressPercent = document.getElementById('progressPercent');
const timeElapsed = document.getElementById('timeElapsed');
const timeRemaining = document.getElementById('timeRemaining');
// Show details container
conversionDetails.classList.remove('d-none');
const interval = setInterval(() => {
if (!videoElement.duration || isNaN(videoElement.currentTime)) return;
const currentTime = videoElement.currentTime;
const duration = videoElement.duration;
const percent = Math.min(100, (currentTime / duration) * 100);
// Update progress bar
progressBar.style.width = `${percent}%`;
progressBar.setAttribute('aria-valuenow', percent.toFixed(1));
progressText.textContent = `${percent.toFixed(1)}%`;
// Update detailed progress
progressPercent.textContent = percent.toFixed(1);
// Calculate time elapsed and remaining
const elapsedSeconds = currentTime;
const remainingSeconds = Math.max(0, duration - currentTime);
timeElapsed.textContent = formatTime(elapsedSeconds);
timeRemaining.textContent = formatTime(remainingSeconds);
// Complete if done
if (percent >= 100) {
clearInterval(interval);
}
}, 200);
return interval;
// Helper function to format time as MM:SS
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
}
// Get file extension from MIME type
function getFileExtension(mimeType) {
switch(mimeType) {
case 'audio/mp3': return '.mp3';
case 'audio/wav': return '.wav';
case 'audio/ogg': return '.ogg';
case 'audio/webm': return '.webm';
default: return '.mp3';
}
}
// Event listeners
dropZone.addEventListener('click', () => videoInput.click());
videoInput.addEventListener('change', (e) => {
if (e.target.files.length) handleVideoSelect(e.target.files[0]);
});
// Drag and drop handlers
['dragover', 'dragenter'].forEach(event => {
dropZone.addEventListener(event, (e) => {
e.preventDefault();
dropZone.classList.add('active-drop');
});
});
['dragleave', 'dragend', 'drop'].forEach(event => {
dropZone.addEventListener(event, (e) => {
e.preventDefault();
dropZone.classList.remove('active-drop');
});
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
if (e.dataTransfer.files.length) handleVideoSelect(e.dataTransfer.files[0]);
});
convertBtn.addEventListener('click', () => {
showAlert('Conversion started...', 'info');
startConversion();
});
// Initialize with convert button disabled
convertBtn.disabled = true;
// Show alert function
function showAlert(message, type) {
const alertDiv = document.createElement('div');
alertDiv.className = `alert alert-${type} alert-dismissible fade show position-fixed top-0 start-50 translate-middle-x mt-3`;
alertDiv.setAttribute('role', 'alert');
alertDiv.innerHTML = `
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
`;
document.body.appendChild(alertDiv);
setTimeout(() => {
alertDiv.classList.remove('show');
setTimeout(() => alertDiv.remove(), 150);
}, 3000);
}
});