🎞️ Automatically generating subtitles for video files using Whisper ASR model in Python
MIT License
This repository provides a Python script to generate subtitle files (.srt
) for a given video file. The script extracts audio from the video, transcribes the audio using OpenAI's Whisper model, and generates an .srt
file with the transcription and timestamps.
SRT (.srt) files are the most common type of closed caption file format. SRT stands for “SubRip Subtitle” file.
An SRT file includes:
ffmpeg
..srt
subtitle files.Python 3.7+
ffmpeg
torch
openai-whisper
Install ffmpeg
:
Make sure ffmpeg
is installed on your system. You can download it from ffmpeg.org and follow the installation instructions for your operating system.
Clone the repository:
git clone https://github.com/yourusername/subtitle-generator.git
cd subtitle-generator
Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install the required Python packages:
pip install -r requirements.txt
Provide the path to the video file:
Ensure you have the video file path ready. The script will process this file to generate subtitles.
Run the script:
Update all file paths (.srt, .wav, .mp4) in the script
python generate_subtitles.py
Output:
The script will create a .srt
file with the same name as the video file in the same directory.
generate_subtitles.py
This is the main script that performs the following steps:
Extract audio from the video file:
Uses ffmpeg
to extract audio from the provided video file and save it as a .wav
file.
Transcribe the audio file:
Utilizes OpenAI's Whisper model to transcribe the audio into text with timestamps.
Create the .srt
file:
Generates an .srt
file with the transcribed text and timestamps.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.