Spaces:
Running
Running
| from moviepy.editor import VideoFileClip | |
| import whisper | |
| import torch | |
| # Path to your video | |
| video_path = "C:/Users/path/Downloads/movie.mp4" | |
| clip = VideoFileClip(video_path) | |
| duration = clip.duration | |
| chunk_length = 60 # 1 minute chunks | |
| # Load whisper model | |
| model = whisper.load_model("tiny.en") | |
| model = model.to(torch.device("cpu")) # force CPU | |
| # Store all segments here | |
| all_segments = [] | |
| for i, start in enumerate(range(0, int(duration), chunk_length)): | |
| end = min(start + chunk_length, duration) | |
| print(f"Processing chunk {i+1}: {start}-{end} seconds") | |
| # Create subclip | |
| subclip = clip.subclip(start, end) | |
| subclip_path = f"chunk_{i}.mp4" | |
| subclip.write_videofile(subclip_path, audio_codec="aac", verbose=False, logger=None) | |
| # Transcribe subclip | |
| result = model.transcribe(subclip_path, language="English") | |
| # Add segments to main list | |
| all_segments.extend(result["segments"]) | |
| # Save all segments to a single SRT file | |
| with open("output.srt", "w", encoding="utf-8") as f: | |
| for i, segment in enumerate(all_segments, start=1): | |
| start = segment["start"] | |
| end = segment["end"] | |
| text = segment["text"].strip() | |
| # Format SRT timestamp | |
| def srt_time(seconds): | |
| ms = int((seconds % 1) * 1000) | |
| h = int(seconds // 3600) | |
| m = int((seconds % 3600) // 60) | |
| s = int(seconds % 60) | |
| return f"{h:02}:{m:02}:{s:02},{ms:03}" | |
| f.write(f"{i}\n{srt_time(start)} --> {srt_time(end)}\n{text}\n\n") | |
| print("✅ SRT file generated: output.srt") | |