How to create a voice over in more than 30 languages with the Google Cloud Platform Text To Speech API
I start this article, with the holy grail for content creators. What if there was a way to automatically translate your videos and create a voice- over in different languages?
It turns out that Google is getting us closer to that dream. In this article I will show you how you can use the Google Cloud Platform Speech and Translation APIs to create a voice over in over 30 languages. And you will be able to see the result in my latest youtube video.
I have recently started creating youtube videos and I have come to realise that there is a lot of work that goes into creating and editing an youtube video.
Delivering a smooth performance in front of the camera is part of the challenge. But good video editing and preparing content is one of the major obstacles when providing good content.
It is no wonder, then that internationalisation is down in the list of major concerns. Most of the world speaks English after all. Or so we are led to believe…..
In reality as much as 60% of Youtube users don’t speak english and even a higher proportion feel more comfortable listening to content in their own language.
As a small Youtube creator as things stand it is too expensive to create content for all these languages. But what if there was an easy way to translate your videos automatically and create a voice over in different languages?
There is indeed a way to translate all your content automatically using a handful of GCP ML/AI apis.
As part of a small experiment, I wrote a small script to put all those APIs together and in that way, translate a video to any language supported by GCP(33 languages as of now), with voice-over.
It first transcribes the video using the Speech to Text API and then uses the Google Translation API to translate the text to any language. Finally we call the Google Text to speech API to create the voice over for the video.
Note that I decided not to use the captions generated by Youtube, as I wanted to see what I can get with the captions provided by the Text to Speech API.
Lets get started!
Create a project in your GCP Account
In order to get started you need to create a project in GCP.
Create a Service Account
Navigate to IAM&Admin-Service Accounts.
Click on “Create a Service Account”
Type the name of the service account and click Create.
In the next screen add the roles Project -> Owner and Storage Admin
Click Continue and then click Done on the third optional screen.
you will download a JSON file. Keep it in a safe location as we will need it later.
Enabling the required APIs
Now you should enable the three APIs that we will need for this exercise.
Go to API’s and services. Click on Cloud Speech To Text API
Repeat for the Cloud Translation API and Cloud Text To Speech API.
The end result is far from perfect. But the potential future of the technology is there to see. With a bit more coding and using some AI to do lip syncing I could have created a perfect voice-over for my video.
Perfect. Now we have enabled the APIs. Let’s get to the fun part.
Development Environment Setup
RUN yum update -y && yum install -y python3 && yum install -y python3-pip && pip3 install --upgrade pip
RUN echo "export LANG=en_GB.UTF-8" >> /root/.bashrc
COPY python/requirements.txt /tmp/requirements.txt
RUN pip3 install -r /tmp/requirements.txt
entrypoint: ["tail", "-f", "/dev/null"]
We set the entrypoint to tail -f /dev/null to allow the container to stay up and in this way we are able to call our python script.
Now open a terminal window, navigate to the directory with your docker files and start the docker container with:
$ docker-compose up -dCreating network "translate-video_default" with the default driverBuilding translationStep 1/5 : FROM jrottenberg/ffmpeg:3.2.15-centos7---> 58b29b349418Step 2/5 : RUN yum update -y && yum install -y python3 && yum install -y python3-pip && pip3 install --upgrade pip---> Using cache---> 6c379e1a2638Step 3/5 : RUN echo "export LANG=en_GB.UTF-8" >> /root/.bashrc---> Using cache---> 6da1d83e6945Step 4/5 : COPY python/requirements.txt /tmp/requirements.txt---> 4ac6765a420eStep 5/5 : RUN pip3 install -r /tmp/requirements.txt---> Running in 429aa9a64dbf
Our translate python script will take an input video in mp4 format and an output language and return a new mp4 video with a new audio track with the voice over in the language as requested in the parameter. The script assumes that the input video is in english.
Extracting the Audio from the video
To extract the audio from the video, we use ffmpeg.
filePathOutput = os.path.splitext(filePath) + '.mp3'
subprocess.call(['ffmpeg', '-i', filePath, filePathOutput])
The resulting audio is in mp3 format
Uploading the Audio to Google Cloud Storage
To extract the text we need to use the Google Text To Speech API. But before we can do that, we need to upload the video to Google Cloud Storage.
For that we need to create a bucket in Google Cloud. In the GCP Dashboard, go to Storage->Browser. Click in Create Bucket
Specify an unique bucket name and click create. You will need this bucket in the code to upload the audio file.
Calling the Google Speech to Text API
Now that we have the code to upload the audio file, we can call the Google Text To Speech Cloud API.
The transcript is returned as a list of results and each result has 1 or more alternatives. Alternatives are provided when the speech engine is not sure of was actually said, so it gives you a few possibilities to select. To simplify and given this is just a proof of concept, I am always selecting the first alternative available.
Calling the Google Translation API
After we have a copy in text of what was actually said in the video, we are ready to translate it into a different language.
Calling the Google Text To Speech API
Now that we have the translated text, we can use Google’s Text to Speech API to convert it to an MP3 file.
There are a few parameters here that I just want to briefly explain. You can set the speed at which the speaker talks. In my video demo I went with 0.8 to keep up with my own speaking rate. If you are not sure in your case, leave it at 1.0.
You can also set a pitch. The pitch will set the tone of the voice. If you are not sure what exact voice to use, set the gender of the voice. And most important of all, you need to specify the languageCode of the voice you are looking for. The library will then select the best available voice within a list taking into account the parameters given to VoiceSelectionParams.
Putting everything together
The last piece in our puzzle is to replace the original audio in the video that we passed as input and replace it with the new audio voice over. Thankfully with ffmpeg that is quite easy to do.
And that’s it!
Now the next step is to record a small introductory video and try our script.
Disclaimers, you are not going to get production level voice-overs at this stage, but hopefully this demo will give you a taste of the potential with this unprecedented Google technology.
For a detailed step by step instruction, do watch my latest video: