TL;DR: You can prepare a video for dubbing using three terminal commands. ffmpeg cuts the audio into the form whisper expects, whisper-cli writes an English SRT, and demucs separates the score from the speech. By the end you have four files: the wav, the SRT, the vocals stem, and the music bed. In the second part of this two-part presentation (in next issue), the SRT will be translated, spoken with Piper, and muxed back into the video.
Dubbing is the job for the GUI tools, such as Premiere and Final Cut, or some SaaS products. But an alternative option is a pipeline that does it from the terminal, and that's what I am proposing to do here.
This issue focuses on the preparation part: turn a video into the files a dub needs. Next, we will present the second part, the actual dubbing steps.
Dependencies and setup
The setup for the pipeline requires your package manager (homebrew, apt or other) and a Python virtual environment.
On macOS, install ffmpeg and whisper.cpp via Homebrew, then create a Python virtualenv and add the demucs package to it:
brew install ffmpeg whisper.cpp
...
python3 -m venv .venv && source .venv/bin/activate && pip install demucsOn Debian / Ubuntu, install ffmpeg via apt, then create a Python virtualenv and add the whisper-cpp and demucs packages to it:
sudo apt install ffmpeg
...
python3 -m venv .venv && source .venv/bin/activate
pip install whisper-cpp
pip install demucsEach tool has a specific job in this pipeline:
ffmpegextracts the audio into the form whisper needs (Step 1).whisper-cliis the CLI program for whisper.cpp, which provides high-performance inference of OpenAI's Whisper ASR model on the CPU; it transcribes the wav file into an SRT (Step 2). It needs a base English model that can be downloaded (a 140 MB file).demucs, which runs in PyTorch, is the source separator that lifts the music out of the mix (Step 3).
After installing the tools, download and store the base English model file that whisper needs:
mkdir -p models
curl -L -o models/ggml-base.en.bin "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin"What this part of the pipeline does
There are three steps needed in the pipeline:
Extract the audio with
ffmpeginto a 16 kHz mono WAV file (whisper needs that format).Transcribe the English cues with
whisper-cli, which gives us an SRT file.Separate the music from the speech in the audio, with
demucs, which gives us two wav files,vocals.wavandno_vocals.wav.
We want the pipeline to be scriptable, so each step writes a file that can be manipulated, and you can re-run the step or change its parameters without touching the other steps.
Worked example: Sintel, the Blender Foundation movie
The worked example uses Sintel, the Blender Foundation short film, downloadable under CC BY from the Internet Archive. Pick your own neutral, license-clear clip, and the pipeline runs the same way on it.
curl -L -o sintel.mp4 "https://archive.org/download/Sintel/sintel-2048-stereo_512kb.mp4"
ffmpeg -y -v error -i sintel.mp4 -vn -ar 16000 -ac 1 -t 240 -c:a pcm_s16le sintel_head.wavThat cuts the first four minutes of the film into sintel_head.wav, the form whisper expects.
Step 1: get English subtitles with whisper-cli
whisper-cli -m models/ggml-base.en.bin -f sintel_head.wav -l en -osrt -of sintel_head-l en declares the spoken language, -osrt writes the cues as SRT, and -of sets the filename prefix for the SRT output. The first dialogue cue in the head is:
00:01:47,000 --> 00:01:49,480
- This blade has a dark past.Whisper also writes (dramatic music) and (grunting) cues from the base model; remember to delete those before translating since they are non-speech.
Step 2: lift the music out with Demucs
A dub replaces the speech, not the music score, so we need to separate the speech and the rest. Demucs is a source separator; give it the wav and it returns what are called stems, single-track audio files split by source, one for the speech and one for everything else.
source .venv/bin/activate
demucs --two-stems=vocals -n htdemucs sintel_head.wav -o stemsCheck the result with ls stems/htdemucs/sintel_head:
no_vocals.wav vocals.wavThe no_vocals.wav stem is the music bed the dub will mix against in the second part of the project. The vocals.wav stem is what you could throw away; but keep it, in case you want to remaster the original mix without the score, at some point in the future.
What this leaves you
You now have four files saved on disk, ready for the next part of the work:
sintel_head.wav— the 240 s audio head, in whisper's preferred shape.sintel_head.srt— the English subtitles file, with non-speech cues stripped.stems/htdemucs/sintel_head/vocals.wav— the speech stem, kept for reference.stems/htdemucs/sintel_head/no_vocals.wav— the music bed, what the dub will sit on.
Edit the SRT by hand for any misheard cue (whisper's base model is small and gets names wrong), and re-run Demucs with a different -n if the separation has artifacts.
In next issue, you will: translate the SRT, run Piper on every cue, mix the new voice over no_vocals.wav, and mux the dub back into sintel.mp4.
Example footage: Sintel (© Blender Foundation), CC BY 3.0, via archive.org/details/Sintel.
