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:
- The number of the closed caption frame in sequence
- Beginning and end timecodes for when the closed caption frame should appear
- The closed caption itself
- A blank link to indicate the start of a new closed caption sequence
- Extracts audio from video files using
ffmpeg. - Transcribes audio to text with timestamps using OpenAI's Whisper model.
- Generates
.srtsubtitle files.
Python 3.7+ffmpegtorchopenai-whisper
-
Install
ffmpeg:Make sure
ffmpegis 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
.srtfile with the same name as the video file in the same directory.
This is the main script that performs the following steps:
-
Extract audio from the video file:
Uses
ffmpegto extract audio from the provided video file and save it as a.wavfile. -
Transcribe the audio file:
Utilizes OpenAI's Whisper model to transcribe the audio into text with timestamps.
-
Create the
.srtfile:Generates an
.srtfile 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.
- OpenAI Whisper for the transcription model.
- ffmpeg for audio extraction.
