Iboytech's picture
Downloading and copying transcription doesn't work
614d53c verified
Raw History Blame Contribute Delete
11.5 kB
// Main application functionality
class TranscriptionApp {
constructor() {
this.init();
}
init() {
this.setupEventListeners();
this.setupBackToTop();
}
setupEventListeners() {
// Transcription form submission
const form = document.getElementById('transcription-form');
if (form) {
form.addEventListener('submit', (e) => this.handleTranscription(e));
}
// Copy button
const copyBtn = document.getElementById('copy-btn');
if (copyBtn) {
copyBtn.addEventListener('click', () => this.copyToClipboard());
}
// Download button
const downloadBtn = document.getElementById('download-btn');
if (downloadBtn) {
downloadBtn.addEventListener('click', () => this.downloadTranscription());
}
}
setupBackToTop() {
const backToTop = document.createElement('button');
backToTop.className = 'back-to-top';
backToTop.innerHTML = '<i data-feather="arrow-up"></i>';
document.body.appendChild(backToTop);
// Show/hide back to top button
window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
backToTop.classList.add('visible');
} else {
backToTop.classList.remove('visible');
}
});
// Scroll to top when clicked
backToTop.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
// Update icons after adding new ones
feather.replace();
}
async handleTranscription(e) {
e.preventDefault();
const videoUrl = document.getElementById('video-url').value;
const language = document.getElementById('language').value;
const format = document.getElementById('format').value;
// Show loading state
this.showLoading();
try {
// Simulate API call (replace with actual API integration)
const transcription = await this.simulateTranscription(videoUrl, language, format);
// Display results
this.displayResults(transcription);
} catch (error) {
this.showError('Failed to transcribe video. Please check the URL and try again.');
}
}
async simulateTranscription(videoUrl, language, format) {
// Validate video URL
if (!videoUrl || !videoUrl.includes('http')) {
throw new Error('Please enter a valid video URL');
}
// Simulate API processing time
await new Promise(resolve => setTimeout(resolve, 2000));
// Mock transcription data based on format
const mockTranscriptions = {
txt: `Video Transcription Sample\n\n[00:00:00] Welcome to this demonstration of our transcription service.\n[00:00:05] This is a sample transcription showing how our AI converts video content into text.\n[00:00:15] You can use this text for subtitles, content creation, or accessibility purposes.\n[00:00:25] Thank you for using VidTranscribe Magic Wand!`,
srt: `1\n00:00:00,000 --> 00:00:05,000\nWelcome to this demonstration of our transcription service.\n\n2\n00:00:05,000 --> 00:00:15,000\nThis is a sample transcription showing how our AI converts video content into text.\n\n3\n00:00:15,000 --> 00:00:25,000\nYou can use this text for subtitles, content creation, or accessibility purposes.\n\n4\n00:00:25,000 --> 00:00:30,000\nThank you for using VidTranscribe Magic Wand!`,
vtt: `WEBVTT\n\n00:00:00.000 --> 00:00:05.000\nWelcome to this demonstration of our transcription service.\n\n00:00:05.000 --> 00:00:15.000\nThis is a sample transcription showing how our AI converts video content into text.\n\n00:00:15.000 --> 00:00:25.000\nYou can use this text for subtitles, content creation, or accessibility purposes.\n\n00:00:25.000 --> 00:00:30.000\nThank you for using VidTranscribe Magic Wand!`,
json: JSON.stringify({
videoUrl: videoUrl,
language: language,
duration: "30 seconds",
transcription: [
{
start: "00:00:00",
end: "00:00:05",
text: "Welcome to this demonstration of our transcription service."
},
{
start: "00:00:05",
end: "00:00:15",
text: "This is a sample transcription showing how our AI converts video content into text."
},
{
start: "00:00:15",
end: "00:00:25",
text: "You can use this text for subtitles, content creation, or accessibility purposes."
},
{
start: "00:00:25",
end: "00:00:30",
text: "Thank you for using VidTranscribe Magic Wand!"
}
]
}, null, 2)
};
return mockTranscriptions[format] || mockTranscriptions.txt;
}
showLoading() {
const resultsSection = document.getElementById('results');
const outputDiv = document.getElementById('transcription-output');
resultsSection.classList.remove('hidden');
outputDiv.innerHTML = `
<div class="flex flex-col items-center justify-center py-8">
<div class="w-16 h-16 border-4 border-primary-500 border-t-transparent rounded-full loading-pulse mb-4"></div>
<p class="text-gray-300">Transcribing your video...</p>
<p class="text-sm text-gray-400 mt-2">This may take a few moments</p>
</div>
`;
}
displayResults(transcription) {
const outputDiv = document.getElementById('transcription-output');
const format = document.getElementById('format').value;
if (format === 'json') {
outputDiv.innerHTML = `<pre class="text-green-400 whitespace-pre-wrap">${transcription}</pre>`;
} else {
outputDiv.innerHTML = `<div class="text-gray-200 whitespace-pre-wrap leading-relaxed">${transcription}</div>`;
}
// Store transcription in global variable for access
window.currentTranscription = transcription;
// Scroll to results
document.getElementById('results').scrollIntoView({ behavior: 'smooth' });
}
showError(message) {
const outputDiv = document.getElementById('transcription-output');
outputDiv.innerHTML = `
<div class="text-center py-8">
<i data-feather="alert-circle" class="w-12 h-12 text-red-500 mx-auto mb-4"></i>
<p class="text-red-400 text-lg">${message}</p>
</div>
`;
feather.replace();
}
copyToClipboard() {
const outputDiv = document.getElementById('transcription-output');
if (!outputDiv) return;
let text;
if (outputDiv.querySelector('pre')) {
text = outputDiv.querySelector('pre').textContent;
} else {
text = outputDiv.textContent || outputDiv.innerText;
}
// Fallback for older browsers
if (!navigator.clipboard) {
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
this.showToast('Transcription copied to clipboard!', 'success');
} catch (err) {
this.showToast('Failed to copy transcription', 'error');
}
document.body.removeChild(textArea);
return;
}
navigator.clipboard.writeText(text).then(() => {
this.showToast('Transcription copied to clipboard!', 'success');
}).catch(() => {
this.showToast('Failed to copy transcription', 'error');
});
}
downloadTranscription() {
const outputDiv = document.getElementById('transcription-output');
if (!outputDiv) return;
let text;
if (outputDiv.querySelector('pre')) {
text = outputDiv.querySelector('pre').textContent;
} else {
text = outputDiv.textContent || outputDiv.innerText;
}
const format = document.getElementById('format').value;
const filename = `transcription.${format}`;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
this.showToast('Transcription downloaded!', 'success');
}
showToast(message, type = 'info') {
const toast = document.createElement('div');
const bgColor = type === 'success' ? 'bg-primary-500' : 'bg-red-500';
toast.className = `fixed top-4 right-4 ${bgColor} text-white px-6 py-3 rounded-lg shadow-lg transform transition-all duration-300 translate-x-full z-50`;
toast.textContent = message;
document.body.appendChild(toast);
// Animate in
requestAnimationFrame(() => {
requestAnimationFrame(() => {
toast.classList.remove('translate-x-full');
});
});
// Remove after 3 seconds
setTimeout(() => {
toast.classList.add('translate-x-full');
setTimeout(() => {
if (document.body.contains(toast)) {
document.body.removeChild(toast);
}
}, 300);
}, 3000);
}
}
// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new TranscriptionApp();
});
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Handle feather icons in dynamically added content
document.addEventListener('DOMContentLoaded', function() {
// Replace icons initially
if (typeof feather !== 'undefined') {
feather.replace();
}
// Watch for DOM changes to replace icons in new content
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length > 0) {
setTimeout(() => {
if (typeof feather !== 'undefined') {
feather.replace();
}
}, 100);
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});