| 72 | } |
| 73 | |
| 74 | async function analyzeLabelsLocal(path) { |
| 75 | // [START video_analyze_labels] |
| 76 | // Imports the Google Cloud Video Intelligence library + Node's fs library |
| 77 | const video = require('@google-cloud/video-intelligence').v1; |
| 78 | const fs = require('fs'); |
| 79 | const util = require('util'); |
| 80 | |
| 81 | // Creates a client |
| 82 | const client = new video.VideoIntelligenceServiceClient(); |
| 83 | |
| 84 | /** |
| 85 | * TODO(developer): Uncomment the following line before running the sample. |
| 86 | */ |
| 87 | // const path = 'Local file to analyze, e.g. ./my-file.mp4'; |
| 88 | |
| 89 | // Reads a local video file and converts it to base64 |
| 90 | const readFile = util.promisify(fs.readFile); |
| 91 | const file = await readFile(path); |
| 92 | const inputContent = file.toString('base64'); |
| 93 | |
| 94 | // Constructs request |
| 95 | const request = { |
| 96 | inputContent: inputContent, |
| 97 | features: ['LABEL_DETECTION'], |
| 98 | }; |
| 99 | |
| 100 | // Detects labels in a video |
| 101 | const [operation] = await client.annotateVideo(request); |
| 102 | console.log('Waiting for operation to complete...'); |
| 103 | const [operationResult] = await operation.promise(); |
| 104 | // Gets annotations for video |
| 105 | const annotations = operationResult.annotationResults[0]; |
| 106 | |
| 107 | const labels = annotations.segmentLabelAnnotations; |
| 108 | labels.forEach(label => { |
| 109 | console.log(`Label ${label.entity.description} occurs at:`); |
| 110 | label.segments.forEach(segment => { |
| 111 | const time = segment.segment; |
| 112 | if (time.startTimeOffset.seconds === undefined) { |
| 113 | time.startTimeOffset.seconds = 0; |
| 114 | } |
| 115 | if (time.startTimeOffset.nanos === undefined) { |
| 116 | time.startTimeOffset.nanos = 0; |
| 117 | } |
| 118 | if (time.endTimeOffset.seconds === undefined) { |
| 119 | time.endTimeOffset.seconds = 0; |
| 120 | } |
| 121 | if (time.endTimeOffset.nanos === undefined) { |
| 122 | time.endTimeOffset.nanos = 0; |
| 123 | } |
| 124 | console.log( |
| 125 | `\tStart: ${time.startTimeOffset.seconds}` + |
| 126 | `.${(time.startTimeOffset.nanos / 1e6).toFixed(0)}s` |
| 127 | ); |
| 128 | console.log( |
| 129 | `\tEnd: ${time.endTimeOffset.seconds}.` + |
| 130 | `${(time.endTimeOffset.nanos / 1e6).toFixed(0)}s` |
| 131 | ); |