import { AlignedSegment, combineVideoWithSubtitles } from '../services/transcriptionApi'; // Generate SRT subtitle format export const generateSRT = (segments: AlignedSegment[]): string => { let srt = ""; segments.forEach((segment, index) => { const startTime = formatTimeForSRT(segment.start); const endTime = formatTimeForSRT(segment.end); srt += `${index + 1}\n`; srt += `${startTime} --> ${endTime}\n`; srt += `${segment.text}\n\n`; }); return srt; }; // Generate WebVTT subtitle format export const generateWebVTT = (segments: AlignedSegment[]): string => { let vtt = "WEBVTT\n\n"; segments.forEach((segment, index) => { const startTime = formatTimeForVTT(segment.start); const endTime = formatTimeForVTT(segment.end); vtt += `${index + 1}\n`; vtt += `${startTime} --> ${endTime}\n`; vtt += `${segment.text}\n\n`; }); return vtt; }; // Format time for SRT (HH:MM:SS,mmm) const formatTimeForSRT = (seconds: number): string => { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const secs = Math.floor(seconds % 60); const milliseconds = Math.floor((seconds % 1) * 1000); return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")},${milliseconds.toString().padStart(3, "0")}`; }; // Format time for WebVTT (HH:MM:SS.mmm) const formatTimeForVTT = (seconds: number): string => { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const secs = Math.floor(seconds % 60); const milliseconds = Math.floor((seconds % 1) * 1000); return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}.${milliseconds.toString().padStart(3, "0")}`; }; // Download subtitle file export const downloadSubtitles = (content: string, filename: string) => { const blob = new Blob([content], { type: "text/plain;charset=utf-8" }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; // Helper function to parse VTT time format export const parseVTTTime = (timeStr: string): number => { const parts = timeStr.split(":"); const seconds = parts[parts.length - 1].split("."); return ( parseInt(parts[0]) * 3600 + // hours parseInt(parts[1]) * 60 + // minutes parseInt(seconds[0]) + // seconds parseInt(seconds[1] || "0") / 1000 // milliseconds ); }; // Download video with embedded subtitles using backend FFmpeg processing export const downloadVideoWithSubtitles = async ( videoFile: File, subtitleContent: string, filename: string, language: string = 'eng', format: 'srt' | 'webvtt' = 'srt', outputFormat: 'mp4' | 'mkv' = 'mp4' ) => { try { // Use the centralized API function const blob = await combineVideoWithSubtitles(videoFile, subtitleContent, language, format, outputFormat); // Create download link const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } catch (error) { console.error('Error combining video with subtitles:', error); throw new Error(`Failed to combine video with subtitles: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; // Format time helper function export const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins}:${secs.toString().padStart(2, "0")}`; };