Notebooks
G
Groq
Subtitler Tutorial

Subtitler Tutorial

managed-by-terraformtutorialsinstagram-reel-subtitler06-multimodalgroq-api-cookbook

Groq Whisper Instagram Reel Subtitler

This guide will walk you through creating an automated subtitle generator for Instagram Reels using Groq Whisper. The script extracts audio from a video, transcribes it using Groq's Whisper API, and overlays word by word subtitles onto the video.

Example video output: example_video_output.mp4

Technologies Used

  • Groq Whisper Large V3 Turbo: AI-powered speech-to-text transcription with word level time stamps.
  • MoviePy: Handles video and subtitle overlaying.
  • Python OS Module: Manages file paths.

Step 1: Install Dependencies

Ensure you have the necessary Python packages installed:

pip install moviepy groq python-dotenv

Note: MoviePy requries FFmpeg, an open source program that handles audio and video. You can download it here: https://ffmpeg.org/download.html or if you have Homebrew installed on Mac, run this command in your terminal: brew install ffmpeg

Step 2: Setup API Key

Create a GroqCloud account and get your API key:

Sign up at GroqCloud. Navigate to API Keys and click on Generate API Key

Store the key securely in an .env file:

GROQ_API_KEY=your_groq_api_key

Then in the captioner.py file, import the packages and load the API key.

[ ]

Step 3: Convert MP4 to MP3

Before transcribing, we must extract audio from the video.

[ ]

Step 4: Transcribe Audio Using Groq Whisper

Now that we have the mp3 file from the above function, We send the extracted MP3 audio to Whisper hosted on Groq for lightning-fast transcription.

We use the verbose_json mode on Whisper to get back timestamped word segments so we know when to place each word on the video.

[ ]

Step 5: Overlay Subtitle Clips

From the previous function, we'll recieve a JSON that contains timestamped segments of words. With these word segments, we'll loop through them and create TextClips to be put into the video at the correct time.

Example of the JSON you would recieve that we'll iterate through:

[
    {'word': 'This', 'start': 0.1, 'end': 0.28},
    {'word': 'month', 'start': 0.28, 'end': 0.56},
    {'word': 'I', 'start': 0.56, 'end': 0.78},
    {'word': 'traveled', 'start': 0.78, 'end': 1.12},
    {'word': 'to', 'start': 1.12, 'end': 1.38}
...
[ ]

Step 6: Call the functions

Now that we've defined the functions, we need to create the appropriate variables and call the functions in the correct order.

[ ]

Step 6: Run the python script

(replace captioner.py with your python file's name if it is not called captioner.py)

python3 captioner.py

Troubleshooting errors:

  • On MacOS, playing audio within VSCode versus opening up the video in Finder uses different audio encoding outputs. Adding audio_codec="aac" to the output line final_clip.write_videofile("final.mp4", codec="libx264", audio_codec="aac") will allow you to hear audio on playback in MacOS Finder. But without it, you will only be able to hear the audio file from within VSCode and not from the Finder.