This Cloud Function automatically creates a LINEAR16 converted copy of files uploaded to Google Cloud Storage. Intended to be compatible for usage with Google Cloud Speech-to-Text API.
The audio is converted to the s16le
format and codec, with mono-channel audio @ 16000hz.
This function was based on the ffmpeg-convert-audio function sample from firebase/functions-samples.
See file functions/index.js for the audio conversion code.
The audio is first downloaded locally from the Cloud Storage bucket to the tmp/
folder using the Node.js Client. Then the audio is converted using FFmpeg and uploaded back to the bucket.
The function should be set to trigger on upload of any file to your Firebase project's default Cloud Storage bucket.
To deploy and test the function:
- Create a Firebase or Google Cloud project and open the bucket which the function will subscribe to.
- Deploy the code by creating the function via Cloud Console or via the
gcloud
command line tool, see below. - Upload a file to the bucket, monitor your function logs and after the processing is over the bucket should have the converted file with a
_converted.pcm
suffix.
- Go to the Create function page and choose your settings.
- Copy and paste the code from functions/index.js and functions/package.json into their respective files.
- Follow the instructions to deploy the function and monitor/test it.
Deploying via gcloud
command line tool
Be sure to have the tool configured to target the correct project.
- Get the code, for instance using
git clone
. - Enter the correct directory
cd gfc-ffmpeg-linear16/
. - Deploy the function via the
gcloud functions deploy
command with your settings.
- Take into account that the audio files produced should not exceed the size of the memory of your function.
- The audio conversion could take a certain amount of time, increase the timeout of your function using the cloud functions webgui so the function can run for a longer time.
- When deploying, be sure to select the correct bucket, trigger, event type and a service account that has enough permission.
- Consider making your function idempotent to prevent wasting resources on repeated conversions.
© Google, 2015-2020. Modifications © NewGo Tecnologia, 2020. Licensed under an Apache-2 license.