google-cloud-functionsgoogle-speech-apivideo-intelligence-api

Google Cloud Video Intelligence Annotate Video node js example


I just copy and paste from this documentation. But somehow, I'm getting typescript error on my code.

See error message on my code base.

and here is the code example.

const videoContext = {
    speechTranscriptionConfig: {
      languageCode: "en-US",
      enableAutomaticPunctuation: true,
    },
  };

  const req = {
    inputUri: gcsUri,
    features: ["SPEECH_TRANSCRIPTION"],
    videoContext: videoContext,
  };

  const [operation] = await client.annotateVideo(req);
  console.log("Waiting for operation to complete...");
  const [operationResult] = await operation.promise();
  // There is only one annotation_result since only
  // one video is processed.
  const annotationResults = operationResult.annotationResults[0];

  for (const speechTranscription of annotationResults.speechTranscriptions) {
    // The number of alternatives for each transcription is limited by
    // SpeechTranscriptionConfig.max_alternatives.
    // Each alternative is a different possible transcription
    // and has its own confidence score.
    for (const alternative of speechTranscription.alternatives) {
      console.log("Alternative level information:");
      console.log(`Transcript: ${alternative.transcript}`);
      console.log(`Confidence: ${alternative.confidence}`);

      console.log("Word level information:");
      for (const wordInfo of alternative.words) {
        const word = wordInfo.word;
        const start_time = wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9;
        const end_time = wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9;
        console.log("\t" + start_time + "s - " + end_time + "s: " + word);
      }
    }
  }

Solution

  • I tried your code but it seems its working on my side:

    // Imports the Google Cloud Video Intelligence library
    const videoIntelligence = require('@google-cloud/video-intelligence');
    
    // Creates a client
    const client = new videoIntelligence.VideoIntelligenceServiceClient();
    
    /**
     * TODO(developer): Uncomment the following line before running the sample.
     */
    const gcsUri = 'gs://nestorbucket/big_buck_bunny_720p_2mb.mp4';
    
    async function analyzeVideoTranscript() {
        const videoContext = {
            speechTranscriptionConfig: {
              languageCode: "en-US",
              enableAutomaticPunctuation: true,
            },
          };
        
          const req = {
            inputUri: gcsUri,
            features: ["SPEECH_TRANSCRIPTION"],
            videoContext: videoContext,
          };
        
          const [operation] = await client.annotateVideo(req);
          console.log("Waiting for operation to complete...");
          const [operationResult] = await operation.promise();
          // There is only one annotation_result since only
          // one video is processed.
          const annotationResults = operationResult.annotationResults[0];
        
          for (const speechTranscription of annotationResults.speechTranscriptions) {
            // The number of alternatives for each transcription is limited by
            // SpeechTranscriptionConfig.max_alternatives.
            // Each alternative is a different possible transcription
            // and has its own confidence score.
            for (const alternative of speechTranscription.alternatives) {
              console.log("Alternative level information:");
              console.log(`Transcript: ${alternative.transcript}`);
              console.log(`Confidence: ${alternative.confidence}`);
        
              console.log("Word level information:");
              for (const wordInfo of alternative.words) {
                const word = wordInfo.word;
                const start_time = wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9;
                const end_time = wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9;
                console.log("\t" + start_time + "s - " + end_time + "s: " + word);
              }
            }
          }
        }
    
    
    analyzeVideoTranscript();
    

    Result: image

    Can you try check the before you begin section of this article?