Detect shot changes in a streaming video
Stay organized with collections
Save and categorize content based on your preferences.
Detect shot changes in a streaming video file.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Detect shot changes in a streaming video file.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Shot change](/video-intelligence/docs/streaming/shot-change)\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to Video Intelligence, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.BidiStream.html;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig;\n import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient;\n import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html;\n import io.grpc.StatusRuntimeException;\n import java.io.IOException;\n import java.nio.file.Files;\n import java.nio.file.Path;\n import java.nio.file.Paths;\n import java.util.Arrays;\n import java.util.concurrent.TimeoutException;\n\n class StreamingShotChangeDetection {\n\n // Perform streaming video detection for shot changes\n static void streamingShotChangeDetection(String filePath)\n throws IOException, TimeoutException, StatusRuntimeException {\n // String filePath = \"path_to_your_video_file\";\n\n try (StreamingVideoIntelligenceServiceClient client =\n StreamingVideoIntelligenceServiceClient.create()) {\n\n Path path = Paths.get(filePath);\n byte[] data = Files.readAllBytes(path);\n // Set the chunk size to 5MB (recommended less than 10MB).\n int chunkSize = 5 * 1024 * 1024;\n int numChunks = (int) Math.ceil((double) data.length / chunkSize);\n\n StreamingLabelDetectionConfig labelConfig =\n StreamingLabelDetectionConfig.newBuilder().setStationaryCamera(false).build();\n\n StreamingVideoConfig streamingVideoConfig =\n StreamingVideoConfig.newBuilder()\n .setFeature(StreamingFeature.STREAMING_SHOT_CHANGE_DETECTION)\n .setLabelDetectionConfig(labelConfig)\n .build();\n\n BidiStream\u003cStreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse\u003e call =\n client.streamingAnnotateVideoCallable().call();\n\n // The first request must **only** contain the audio configuration:\n call.send(\n StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build());\n\n // Subsequent requests must **only** contain the audio data.\n // Send the requests in chunks\n for (int i = 0; i \u003c numChunks; i++) {\n call.send(\n StreamingAnnotateVideoRequest.newBuilder()\n .setInputContent(\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html#com_google_protobuf_ByteString_copyFrom_byte___(\n Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize)))\n .build());\n }\n\n // Tell the service you are done sending data\n call.closeSend();\n\n for (StreamingAnnotateVideoResponse response : call) {\n StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();\n if (response.hasError()) {\n System.out.println(response.getError().getMessage());\n System.out.format(\n \"Error was occured with the following status: %s\\n\", response.getError());\n }\n for (VideoSegment segment : annotationResults.getShotAnnotationsList()) {\n double startTimeOffset =\n segment.getStartTimeOffset().getSeconds()\n + segment.getStartTimeOffset().getNanos() / 1e9;\n double endTimeOffset =\n segment.getEndTimeOffset().getSeconds() + segment.getEndTimeOffset().getNanos() / 1e9;\n\n System.out.format(\"Shot: %fs to %fs\\n\", startTimeOffset, endTimeOffset);\n }\n }\n }\n }\n }\n\n### Node.js\n\n\nTo authenticate to Video Intelligence, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * TODO(developer): Uncomment these variables before running the sample.\n */\n // const path = 'Local file to analyze, e.g. ./my-file.mp4';\n const {StreamingVideoIntelligenceServiceClient} =\n require('https://cloud.google.com/nodejs/docs/reference/video-intelligence/latest/overview.html').v1p3beta1;\n const fs = require('fs');\n\n // Instantiates a client\n const client = new https://cloud.google.com/nodejs/docs/reference/video-intelligence/latest/video-intelligence/v1p3beta1.streamingvideointelligenceserviceclient.html();\n // Streaming configuration\n const configRequest = {\n videoConfig: {\n feature: 'https://cloud.google.com/nodejs/docs/reference/video-intelligence/latest/video-intelligence/protos.google.cloud.videointelligence.v1p3beta1.streamingfeature.html',\n },\n };\n const readStream = fs.createReadStream(path, {\n highWaterMark: 5 * 1024 * 1024, //chunk size set to 5MB (recommended less than 10MB)\n encoding: 'base64',\n });\n //Load file content\n const chunks = [];\n readStream\n .on('data', chunk =\u003e {\n const request = {\n inputContent: chunk.toString(),\n };\n chunks.push(request);\n })\n .on('close', () =\u003e {\n // configRequest should be the first in the stream of requests\n stream.write(configRequest);\n for (let i = 0; i \u003c chunks.length; i++) {\n stream.write(chunks[i]);\n }\n stream.end();\n });\n\n const stream = client.streamingAnnotateVideo().on('data', response =\u003e {\n //Gets annotations for video\n const annotations = response.annotationResults;\n const shotChanges = annotations.shotAnnotations;\n console.log(JSON.stringify(shotChanges));\n if (shotChanges.length === 1) {\n console.log('The entire video is one shot.');\n }\n shotChanges.forEach(shot =\u003e {\n console.log(\n ` Shot: ${shot.startTimeOffset.seconds || 0}` +\n `.${(shot.startTimeOffset.nanos / 1e6).toFixed(0)}s to ${\n shot.endTimeOffset.seconds || 0\n }` +\n `.${(shot.endTimeOffset.nanos / 1e6).toFixed(0)}s`\n );\n });\n });\n\n### Python\n\n\nTo authenticate to Video Intelligence, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import videointelligence_v1p3beta1 as videointelligence\n\n # path = 'path_to_file'\n\n client = videointelligence.StreamingVideoIntelligenceServiceClient()\n\n # Set streaming config.\n config = videointelligence.StreamingVideoConfig(\n feature=(videointelligence.StreamingFeature.STREAMING_SHOT_CHANGE_DETECTION)\n )\n\n # config_request should be the first in the stream of requests.\n config_request = videointelligence.StreamingAnnotateVideoRequest(\n video_config=config\n )\n\n # Set the chunk size to 5MB (recommended less than 10MB).\n chunk_size = 5 * 1024 * 1024\n\n # Load file content.\n stream = []\n with io.open(path, \"rb\") as video_file:\n while True:\n data = video_file.read(chunk_size)\n if not data:\n break\n stream.append(data)\n\n def stream_generator():\n yield config_request\n for chunk in stream:\n yield videointelligence.StreamingAnnotateVideoRequest(input_content=chunk)\n\n requests = stream_generator()\n\n # streaming_annotate_video returns a generator.\n # The default timeout is about 300 seconds.\n # To process longer videos it should be set to\n # larger than the length (in seconds) of the stream.\n responses = client.streaming_annotate_video(requests, timeout=600)\n\n # Each response corresponds to about 1 second of video.\n for response in responses:\n # Check for errors.\n if response.error.message:\n print(response.error.message)\n break\n\n for annotation in response.annotation_results.shot_annotations:\n start = (\n annotation.start_time_offset.seconds\n + annotation.start_time_offset.microseconds / 1e6\n )\n end = (\n annotation.end_time_offset.seconds\n + annotation.end_time_offset.microseconds / 1e6\n )\n\n print(\"Shot: {}s to {}s\".format(start, end))\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=video)."]]