Coder Social home page Coder Social logo

benwiley4000 / vtt-translate Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 2.0 20 KB

๐Ÿ”‡ ๐Ÿ‰‘๏ธ Google Translate your .vtt subtitle files into other languages

License: MIT License

JavaScript 100.00%
webvtt subtitles captions translate

vtt-translate's Introduction

vtt-translate

Not exactly the best idea...

...but you can use this to Google Translate your .vtt subtitle files into other languages!

Requires Node.js 7.10.1 or later.

disclaimer

I really want to emphasize that this isn't a great way to produce a translation for your video/audio content. The best way is to hire a professional translator. If you have a budget for producing your content, and you need a translation, please allocate some of that budget for paying a translator.

If you're working on a volunteer-based project with no budget, or you just need some test data, this can work well enough for you.

Keep in mind that translations are done in fixed sized batches of a certain number of lines, so there probably will be occasional translation errors specifically due to context being cut off from the beginning or end of a batch of text.

command line

install

npm install --global vtt-translate

use

$ vtt-translate 
Input VTT filename: ./vtt/transcript_en.vtt
Source language code: en
Destination language code: fr
Google Translate API key (https://cloud.google.com/translate/): [Paste your API key here]
Translating...
Translation complete!
Destination VTT filename: ./vtt/transcript_fr.vtt
$

See the Node API description for explanation of the command line inputs.

Note that API limits apply... if you fill your daily quota, a restore file will be saved so you can continue the next day without losing your progress:

$ vtt-translate 
Input VTT filename: ./vtt/transcript_en.vtt
Source language code: en
Destination language code: fr
Google Translate API key (https://cloud.google.com/translate/): [Paste your API key here]
Translating...
{ code: 403,
  message: 'User Rate Limit Exceeded',
  errors: 
   [ { message: 'User Rate Limit Exceeded',
       domain: 'usageLimits',
       reason: 'userRateLimitExceeded' } ],
  translatedCues: 
   [ ... ] }

Saving temp file so we can resume later where we left off...
Progress: 2688 of 2767 cues (2688 newly translated)
$

Don't remove this temp file! Otherwise you'll have to start over from scratch later.

Once the download completes, the temp file will be deleted automatically.

Node API

install

npm install --save vtt-translate

use

This API example is functionally identical to the earlier command line example.

const { getTranslatedVttForPathname } = require('vtt-translate');
const fs = require('fs');

const pathname = './vtt/transcript_en.vtt';
const apiKey = '[Paste your API key here]';
const source = 'en';
const target = 'fr';

getTranslatedVttForPathname(pathname, apiKey, { source, target })
  .then(vttContent => {
    fs.writeFile('./vtt/transcript_fr.vtt', vttContent, err => {
      if (err) {
        console.error(err);
      }
    });
  })
  .catch(err => {
    console.error(err);
  });

API

getTranslatedVttForPathname(pathname, apiKey, params)

Arguments

  • pathname: The filesystem pathname for the source vtt file.
  • apiKey: A valid Google Cloud Translation API key
  • params.source: A language code supported by Google's Neural Machine Translation Model, corresponding to the language of the source text (e.g. 'en')
  • params.target A language code corresponding to the translation target language
  • params.restoredCuesPathname: A pathname pointing to a JSON file saved with a backup of previously translated cues (see error handling)

Returns

A Promise which resolves with the full text of the translated vtt file (as a string)

Error handling

If you .catch an error and the error has a translatedCues property, you can save that object to a json file and restore it later with params.restoredCuesPathname. This allows downloading a translation across multiple days if your API request quota is too small to download the translation in one day.

vtt-translate's People

Contributors

benwiley4000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.