| 427 | } |
| 428 | |
| 429 | async function analyzeText(path) { |
| 430 | //[START video_detect_text] |
| 431 | // Imports the Google Cloud Video Intelligence library + Node's fs library |
| 432 | const Video = require('@google-cloud/video-intelligence'); |
| 433 | const fs = require('fs'); |
| 434 | const util = require('util'); |
| 435 | // Creates a client |
| 436 | const video = new Video.VideoIntelligenceServiceClient(); |
| 437 | |
| 438 | /** |
| 439 | * TODO(developer): Uncomment the following line before running the sample. |
| 440 | */ |
| 441 | // const path = 'Local file to analyze, e.g. ./my-file.mp4'; |
| 442 | |
| 443 | // Reads a local video file and converts it to base64 |
| 444 | const file = await util.promisify(fs.readFile)(path); |
| 445 | const inputContent = file.toString('base64'); |
| 446 | |
| 447 | const request = { |
| 448 | inputContent: inputContent, |
| 449 | features: ['TEXT_DETECTION'], |
| 450 | }; |
| 451 | // Detects text in a video |
| 452 | const [operation] = await video.annotateVideo(request); |
| 453 | const results = await operation.promise(); |
| 454 | console.log('Waiting for operation to complete...'); |
| 455 | |
| 456 | // Gets annotations for video |
| 457 | const textAnnotations = results[0].annotationResults[0].textAnnotations; |
| 458 | textAnnotations.forEach(textAnnotation => { |
| 459 | console.log(`Text ${textAnnotation.text} occurs at:`); |
| 460 | textAnnotation.segments.forEach(segment => { |
| 461 | const time = segment.segment; |
| 462 | if (time.startTimeOffset.seconds === undefined) { |
| 463 | time.startTimeOffset.seconds = 0; |
| 464 | } |
| 465 | if (time.startTimeOffset.nanos === undefined) { |
| 466 | time.startTimeOffset.nanos = 0; |
| 467 | } |
| 468 | if (time.endTimeOffset.seconds === undefined) { |
| 469 | time.endTimeOffset.seconds = 0; |
| 470 | } |
| 471 | if (time.endTimeOffset.nanos === undefined) { |
| 472 | time.endTimeOffset.nanos = 0; |
| 473 | } |
| 474 | console.log( |
| 475 | `\tStart: ${time.startTimeOffset.seconds || 0}` + |
| 476 | `.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s` |
| 477 | ); |
| 478 | console.log( |
| 479 | `\tEnd: ${time.endTimeOffset.seconds || 0}.` + |
| 480 | `${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s` |
| 481 | ); |
| 482 | console.log(`\tConfidence: ${segment.confidence}`); |
| 483 | segment.frames.forEach(frame => { |
| 484 | const timeOffset = frame.timeOffset; |
| 485 | console.log( |
| 486 | `Time offset for the frame: ${timeOffset.seconds || 0}` + |