Skip to content

Commit

Permalink
Added Video Transcription sample code. (#30)
Browse files Browse the repository at this point in the history
* Added Video Transcription sample code.

* Code cleanup after running prettier.

* Removed the ^
  • Loading branch information
happyhuman authored Apr 9, 2018
1 parent 3ce6787 commit 54b9c21
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
60 changes: 60 additions & 0 deletions video-intelligence/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,59 @@ function analyzeSafeSearch(gcsUri) {
// [END analyze_safe_search]
}

function analyzeVideoTranscription(gcsUri) {
// [START video_speech_transcription]
// Imports the Google Cloud Video Intelligence library
const videoIntelligence = require('@google-cloud/video-intelligence')
.v1p1beta1;

// Creates a client
const client = new videoIntelligence.VideoIntelligenceServiceClient();

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const gcsUri = 'GCS URI of video to analyze, e.g. gs://my-bucket/my-video.mp4';

const videoContext = {
speechTranscriptionConfig: {
languageCode: 'en-US',
},
};

const request = {
inputUri: gcsUri,
features: ['SPEECH_TRANSCRIPTION'],
videoContext: videoContext,
};

client
.annotateVideo(request)
.then(results => {
const operation = results[0];
console.log('Waiting for operation to complete...');
return operation.promise();
})
.then(results => {
console.log('Word level information:');
const alternative =
results[0].annotationResults[0].speechTranscriptions[0].alternatives[0];
alternative.words.forEach(wordInfo => {
let start_time =
wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9;
let end_time = wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9;
console.log(
'\t' + start_time + 's - ' + end_time + 's: ' + wordInfo.word
);
});
console.log('Transcription: ' + alternative.transcript);
})
.catch(err => {
console.error('ERROR:', err);
});
// [END video_speech_transcription]
}

require(`yargs`)
.demand(1)
.command(
Expand Down Expand Up @@ -387,11 +440,18 @@ require(`yargs`)
{},
opts => analyzeSafeSearch(opts.gcsUri)
)
.command(
`transcription <gcsUri>`,
`Extract the video transcription using the Cloud Video Intelligence API.`,
{},
opts => analyzeVideoTranscription(opts.gcsUri)
)
.example(`node $0 faces gs://demomaker/larry_sergey_ice_bucket_short.mp4`)
.example(`node $0 shots gs://demomaker/sushi.mp4`)
.example(`node $0 labels-gcs gs://demomaker/tomatoes.mp4`)
.example(`node $0 labels-file cat.mp4`)
.example(`node $0 safe-search gs://demomaker/tomatoes.mp4`)
.example(`node $0 transcription gs://demomaker/tomatoes.mp4`)
.wrap(120)
.recommendCommands()
.epilogue(
Expand Down
12 changes: 12 additions & 0 deletions video-intelligence/system-test/analyze.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ test.serial(`should analyze safe search results in a GCS file`, async t => {
t.regex(output, /Time: \d+\.\d+s/);
t.regex(output, /Explicit annotation results:/);
});

// analyze_video_transcription
test.serial(
`should analyze video transcription results in a GCS file`,
async t => {
const output = await tools.runAsync(
`${cmd} transcription ${shortUrl}`,
cwd
);
t.regex(output, /over the past/);
}
);

0 comments on commit 54b9c21

Please sign in to comment.