Skip to content

Instantly share code, notes, and snippets.

@zviryatko
Last active November 15, 2023 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zviryatko/b9e74988ad5956eb8a7f756cb1277da9 to your computer and use it in GitHub Desktop.
Save zviryatko/b9e74988ad5956eb8a7f756cb1277da9 to your computer and use it in GitHub Desktop.
Extract subtitles from video and translate them
import moviepy.editor as mp
clip = mp.VideoFileClip(r"video.mp4")
clip.audio.write_audiofile(r"converted.wav")
certifi==2023.7.22
charset-normalizer==3.2.0
decorator==4.4.2
idna==3.4
imageio==2.31.1
imageio-ffmpeg==0.4.8
moviepy==1.0.3
numpy==1.25.1
Pillow==10.0.0
proglog==0.1.10
requests==2.31.0
SpeechRecognition==3.10.0
tqdm==4.65.0
urllib3==2.0.4

Put video file with name video.mp4

# create virtual env
python -m venv .venv
source .venv/bin/activate
# install dependencies for extracting audio file from video
pip install -r requirements.txt
python extract_audio.py
# install OpenAi Whisper to extract text and translate on the fly
pip install openai-whisper
whisper converted.wav --language German --task translate

It'll create subtitles files converted.*, just use it in video player.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment