{time_range}
{text}
import gradio as gr import os import subprocess import whisper import librosa import matplotlib.pyplot as plt import numpy as np import uuid import base64 import torch import shutil from docx import Document # DOCX export # ---------------------------------------------------------- # Auto-select GPU if available for Whisper # ---------------------------------------------------------- device = "cuda" if torch.cuda.is_available() else "cpu" model = whisper.load_model("base", device=device) # ---------------------------------------------------------- # Utility: Convert seconds → WebVTT timestamp format # ---------------------------------------------------------- def format_timestamp(seconds): """ Convert time in seconds to WebVTT format HH:MM:SS.MS """ h = int(seconds // 3600) m = int((seconds % 3600) // 60) s = int(seconds % 60) ms = int((seconds - int(seconds)) * 1000) return f"{h:02d}:{m:02d}:{s:02d}.{ms:03d}" # ---------------------------------------------------------- # Write segments to a .vtt subtitle file # ---------------------------------------------------------- def write_vtt(segments, filepath): """ Save Whisper segments to a .vtt (WebVTT subtitle) file. """ with open(filepath, "w", encoding="utf-8") as f: f.write("WEBVTT\n\n") for i, seg in enumerate(segments, start=1): start = format_timestamp(seg['start']) end = format_timestamp(seg['end']) text = seg['text'].strip() f.write(f"{i}\n{start} --> {end}\n{text}\n\n") # ---------------------------------------------------------- # Export transcript to DOCX # ---------------------------------------------------------- def write_docx(entries, filepath): """ Export transcript text into a single DOCX document. """ doc = Document() doc.add_heading("Transcript", level=1) full_text = " ".join([text for _, text in entries]) doc.add_paragraph(full_text) doc.save(filepath) return filepath # ---------------------------------------------------------- # Read a .vtt file and return list of (timerange, text) # ---------------------------------------------------------- def parse_vtt(filepath): """ Basic VTT parser: returns a list of (timestamp, text) """ entries = [] with open(filepath, "r", encoding="utf-8") as f: lines = f.readlines() idx = 0 while idx < len(lines): line = lines[idx].strip() if "-->" in line: time_range = line idx += 1 text_lines = [] while idx < len(lines) and lines[idx].strip() != '': text_lines.append(lines[idx].strip()) idx += 1 entries.append((time_range, ' '.join(text_lines))) else: idx += 1 return entries # ---------------------------------------------------------- # Parse a VTT timestamp "HH:MM:SS.MS" # ---------------------------------------------------------- def parse_timestamp(ts_str): """ Convert WebVTT timestamp to seconds. """ h, m, rest = ts_str.split(":") s, ms = rest.split(".") return int(h)*3600 + int(m)*60 + int(s) + int(ms)/1000 # ---------------------------------------------------------- # Capture screenshot using ffmpeg # ---------------------------------------------------------- def capture_screenshot(video_path, time_sec, out_path): """ Extract a frame at a specific time using ffmpeg. """ cmd = [ "ffmpeg", "-ss", str(time_sec), "-i", video_path, "-frames:v", "1", "-q:v", "2", out_path, "-y" ] subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # ---------------------------------------------------------- # Save a voice intensity plot around the timestamp # ---------------------------------------------------------- def save_voice_plot(times, db, start_sec, out_path): """ Plot voice-band intensity (300–3000 Hz) and mark the timestamp. """ plt.figure(figsize=(8, 3)) plt.plot(times, db, color="purple") plt.axvline(x=start_sec, color="red", linestyle="--") interp_val = np.interp(start_sec, times, db) plt.scatter([start_sec], [interp_val], color="red") plt.xlabel("Time (s)") plt.ylabel("Voice band dB") plt.tight_layout() plt.savefig(out_path) plt.close() # ---------------------------------------------------------- # Convert image → base64 to embed in HTML # ---------------------------------------------------------- def file_to_base64(filepath): """ Convert a file to a base64 string for HTML embedding. """ with open(filepath, "rb") as f: data = f.read() ext = os.path.splitext(filepath)[1].lower().replace('.', '') mime = f"image/{'jpeg' if ext=='jpg' else ext}" b64 = base64.b64encode(data).decode('utf-8') return f"data:{mime};base64,{b64}" # ---------------------------------------------------------- # Extract audio track from video # ---------------------------------------------------------- def extract_audio(video_path, output_dir): """ Extract audio as MP3 using ffmpeg. """ audio_path = os.path.join(output_dir, "audio.mp3") subprocess.run([ "ffmpeg", "-y", "-i", video_path, "-vn", "-acodec", "libmp3lame", audio_path ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) return audio_path # ---------------------------------------------------------- # Generate the annotated HTML transcript # ---------------------------------------------------------- def generate_html(entries, video_id, video_path, screenshot_dir, plot_dir, output_html_path): """ Create a complete HTML page showing: - text - screenshot - voice plot for each segment. """ html = f"""
Uploaded video file: {os.path.basename(video_path)}
""" for time_range, text in entries: start = time_range.split(" --> ")[0] start_sec = int(parse_timestamp(start)) screenshot_path = os.path.join(screenshot_dir, f"{video_id}_{start_sec}.jpg") plot_path = os.path.join(plot_dir, f"{video_id}_{start_sec}_sound.png") screenshot_b64 = file_to_base64(screenshot_path) if os.path.exists(screenshot_path) else "" plot_b64 = file_to_base64(plot_path) if os.path.exists(plot_path) else "" html += f"""{text}