| 137 | } |
| 138 | |
| 139 | async function analyzeShots(gcsUri) { |
| 140 | // [START video_analyze_shots] |
| 141 | // Imports the Google Cloud Video Intelligence library |
| 142 | const video = require('@google-cloud/video-intelligence').v1; |
| 143 | |
| 144 | // Creates a client |
| 145 | const client = new video.VideoIntelligenceServiceClient(); |
| 146 | |
| 147 | /** |
| 148 | * TODO(developer): Uncomment the following line before running the sample. |
| 149 | */ |
| 150 | // const gcsUri = 'GCS URI of file to analyze, e.g. gs://my-bucket/my-video.mp4'; |
| 151 | |
| 152 | const request = { |
| 153 | inputUri: gcsUri, |
| 154 | features: ['SHOT_CHANGE_DETECTION'], |
| 155 | }; |
| 156 | |
| 157 | // Detects camera shot changes |
| 158 | const [operation] = await client.annotateVideo(request); |
| 159 | console.log('Waiting for operation to complete...'); |
| 160 | const [operationResult] = await operation.promise(); |
| 161 | // Gets shot changes |
| 162 | const shotChanges = operationResult.annotationResults[0].shotAnnotations; |
| 163 | console.log('Shot changes:'); |
| 164 | |
| 165 | if (shotChanges.length === 1) { |
| 166 | console.log('The entire video is one shot.'); |
| 167 | } else { |
| 168 | shotChanges.forEach((shot, shotIdx) => { |
| 169 | console.log(`Scene ${shotIdx} occurs from:`); |
| 170 | if (shot.startTimeOffset === undefined) { |
| 171 | shot.startTimeOffset = {}; |
| 172 | } |
| 173 | if (shot.endTimeOffset === undefined) { |
| 174 | shot.endTimeOffset = {}; |
| 175 | } |
| 176 | if (shot.startTimeOffset.seconds === undefined) { |
| 177 | shot.startTimeOffset.seconds = 0; |
| 178 | } |
| 179 | if (shot.startTimeOffset.nanos === undefined) { |
| 180 | shot.startTimeOffset.nanos = 0; |
| 181 | } |
| 182 | if (shot.endTimeOffset.seconds === undefined) { |
| 183 | shot.endTimeOffset.seconds = 0; |
| 184 | } |
| 185 | if (shot.endTimeOffset.nanos === undefined) { |
| 186 | shot.endTimeOffset.nanos = 0; |
| 187 | } |
| 188 | console.log( |
| 189 | `\tStart: ${shot.startTimeOffset.seconds}` + |
| 190 | `.${(shot.startTimeOffset.nanos / 1e6).toFixed(0)}s` |
| 191 | ); |
| 192 | console.log( |
| 193 | `\tEnd: ${shot.endTimeOffset.seconds}.` + |
| 194 | `${(shot.endTimeOffset.nanos / 1e6).toFixed(0)}s` |
| 195 | ); |
| 196 | }); |