Skip to content

A Cloud Functions example to translate subtitle (SRT) files

License

Notifications You must be signed in to change notification settings

pbavinck/Translate-SRT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Disclaimer: This is not an officially supported Google product. Written code can be used as a baseline, it's not meant for production usage.

Translate-SRT

Translate-SRT is a demonstration of using Google Cloud Functions. The demo leverages the Google Translate API to translate a subtitle text file in SubRip format from one language to another once it is placed in a Google Cloud Storage bucket. Current code specifies from en (english) to nl (Dutch), but this is easily altered.

The Cloud Function is triggered when a new subtitle is stored in a predfined Cloud Storage bucket. After finishing the translation, a new subtitle file with the same timings is stored in a second predefined Cloud Storage bucket.

Getting the sample code

Get the latest sample code from GitHub using Git or download the repository as a ZIP file. (Download)

git clone https://github.com/pbavinck/Translate-SRT.git

Before you begin

  1. Download and install the Google Cloud SDK, which includes the gcloud command-line tool

  2. Create a new Google Cloud Platform project from the Cloud Console or use an existing one

  3. Initialize gcloud, set your project ID and authorize the Cloud SDK

    gcloud init
    
  4. Enable the Google Translate API in Cloud Console

  5. Create three buckets on Google Cloud Storage. One for staging the Cloud Function code (staging bucket). One for the to be translated subtitle files, which is the bucket that triggers the Cloud Function (source bucket). And one for storing the translated subtitle files (target bucket).

Run Locally

We assume you already have nodeJS and npm installed. Before you can run the code locally, which will still use the remote Google Cloud buckets and the remote Google Translate API, you need to do the following:

  1. From within the Translate-SRT folder, install the required node dependencies:

    npm install
    
  2. Copy the file .env_sample to a new file with the name .env

  3. Enter your source and target bucket names in the environment variables TEST_BUCKET and TARGET_BUCKET as well

  4. Add the location of your service account credentials to the GOOGLE_APPLICATION_CREDENTIALS environment variable. This enables the client libraries to use the application default credentials. In VSCode this can be done by adding the following env line to your launch.json.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/index.js",
            "env": {
                "GOOGLE_APPLICATION_CREDENTIALS": "/the-path-to-my/service-account-credentials.json"
            }
        }
    ]
}
  1. Put a sample text file in SubRip format in your source bucket (with extension .txt as opposed to .srt)

  2. Run the test using the command:

    node test.js
    

If all goes well you should see output similar to:

Processing new text file: sample-en.txt (text/plain)
Lines read: 7954
2510 lines of text need translation
Batch size: 128
Performing 20 translation requests
Translations done.
20 segments returned
Writing translated file locally to /tmp/sample-nl.txt.
Uploading local file sample-nl.txt to bucket <<target bucket>
/tmp/sample-nl.txt uploaded to <<target bucket>
Deleting local file /tmp/sample-nl.txt
Finished.

Deploying to Cloud Functions

  1. Use the following command to deploy the code to Google Cloud Function.

    gcloud functions deploy translateSRTFiles --runtime nodejs8 --region=<<region>> --stage-bucket <<staging bucket name>> --trigger-resource <<source bucket name>> --trigger-event google.storage.object.finalize
    
  2. Congratulations! Your Cloud Function is now live and receives events from your source bucket.

Licensing

About

A Cloud Functions example to translate subtitle (SRT) files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published