funcshotChangeURI(wio.Writer,filestring)error{ctx:=context.Background()client,err:=video.NewClient(ctx)iferr!=nil{returnerr}deferclient.Close()op,err:=client.AnnotateVideo(ctx,&videopb.AnnotateVideoRequest{Features:[]videopb.Feature{videopb.Feature_SHOT_CHANGE_DETECTION,},InputUri:file,})iferr!=nil{returnerr}resp,err:=op.Wait(ctx)iferr!=nil{returnerr}// A single video was processed. Get the first result.result:=resp.AnnotationResults[0].ShotAnnotationsfor_,shot:=rangeresult{start,_:=ptypes.Duration(shot.StartTimeOffset)end,_:=ptypes.Duration(shot.EndTimeOffset)fmt.Fprintf(w,"Shot: %s to %s\n",start,end)}returnnil}
Java
如需向 Video Intelligence 进行身份验证,请设置应用默认凭证。
如需了解详情,请参阅为本地开发环境设置身份验证。
// Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClienttry(VideoIntelligenceServiceClientclient=VideoIntelligenceServiceClient.create()){// Provide path to file hosted on GCS as "gs://bucket-name/..."AnnotateVideoRequestrequest=AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.SHOT_CHANGE_DETECTION).build();// Create an operation that will contain the response when the operation completes.OperationFuture<AnnotateVideoResponse,AnnotateVideoProgress>response=client.annotateVideoAsync(request);System.out.println("Waiting for operation to complete...");// Print detected shot changes and their location ranges in the analyzed video.for(VideoAnnotationResultsresult:response.get().getAnnotationResultsList()){if(result.getShotAnnotationsCount() > 0){System.out.println("Shots: ");for(VideoSegmentsegment:result.getShotAnnotationsList()){doublestartTime=segment.getStartTimeOffset().getSeconds()+segment.getStartTimeOffset().getNanos()/1e9;doubleendTime=segment.getEndTimeOffset().getSeconds()+segment.getEndTimeOffset().getNanos()/1e9;System.out.printf("Location: %.3f:%.3f\n",startTime,endTime);}}else{System.out.println("No shot changes detected in "+gcsUri);}}}
Node.js
如需向 Video Intelligence 进行身份验证,请设置应用默认凭证。
如需了解详情,请参阅为本地开发环境设置身份验证。
// Imports the Google Cloud Video Intelligence libraryconstvideo=require('@google-cloud/video-intelligence').v1;// Creates a clientconstclient=newvideo.VideoIntelligenceServiceClient();/** * TODO(developer): Uncomment the following line before running the sample. */// const gcsUri = 'GCS URI of file to analyze, e.g. gs://my-bucket/my-video.mp4';constrequest={inputUri:gcsUri,features:['SHOT_CHANGE_DETECTION'],};// Detects camera shot changesconst[operation]=awaitclient.annotateVideo(request);console.log('Waiting for operation to complete...');const[operationResult]=awaitoperation.promise();// Gets shot changesconstshotChanges=operationResult.annotationResults[0].shotAnnotations;console.log('Shot changes:');if(shotChanges.length===1){console.log('The entire video is one shot.');}else{shotChanges.forEach((shot,shotIdx)=>{console.log(`Scene ${shotIdx} occurs from:`);if(shot.startTimeOffset===undefined){shot.startTimeOffset={};}if(shot.endTimeOffset===undefined){shot.endTimeOffset={};}if(shot.startTimeOffset.seconds===undefined){shot.startTimeOffset.seconds=0;}if(shot.startTimeOffset.nanos===undefined){shot.startTimeOffset.nanos=0;}if(shot.endTimeOffset.seconds===undefined){shot.endTimeOffset.seconds=0;}if(shot.endTimeOffset.nanos===undefined){shot.endTimeOffset.nanos=0;}console.log(`\tStart: ${shot.startTimeOffset.seconds}`+`.${(shot.startTimeOffset.nanos/1e6).toFixed(0)}s`);console.log(`\tEnd: ${shot.endTimeOffset.seconds}.`+`${(shot.endTimeOffset.nanos/1e6).toFixed(0)}s`);});}
"""Detects camera shot changes."""video_client=videointelligence.VideoIntelligenceServiceClient()features=[videointelligence.Feature.SHOT_CHANGE_DETECTION]operation=video_client.annotate_video(request={"features":features,"input_uri":path})print("\nProcessing video for shot change annotations:")result=operation.result(timeout=90)print("\nFinished processing.")# first result is retrieved because a single video was processedfori,shotinenumerate(result.annotation_results[0].shot_annotations):start_time=(shot.start_time_offset.seconds+shot.start_time_offset.microseconds/1e6)end_time=(shot.end_time_offset.seconds+shot.end_time_offset.microseconds/1e6)print("\tShot {}: {} to {}".format(i,start_time,end_time))