You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Node.js通过Google Cloud Video Intelligence API查询视频标注请求状态?

How to Check VideoAnnotation Request Status in Google Cloud Video Intelligence API (Node.js)

Great job grabbing the operation instance from your annotateVideo() call! You’ve got two simple, reliable ways to check the status of your video annotation request—depending on whether you still have the live operation object handy or just saved its name for later use.

Option 1: Use the Existing Operation Instance

If you still have access to the operation variable from your initial annotateVideo() promise, you can leverage built-in properties and methods to check status directly:

  • Check completion status: Use the done boolean property. When true, the operation has finished (either successfully or with an error).
  • Fetch progress metadata: Call operation.getMetadata() to get real-time details like progress percentage, start time, and processing logs.
  • Wait for final results (optional): If you want to block until the operation finishes and retrieve annotations, use operation.promise()—you already had this commented out in your code!

Here’s how to update your existing code to add status checks:

var vidOptions = { "inputUri": gCloudURL, "features": [ 'LABEL_DETECTION' ] };
client.annotateVideo(vidOptions).then(function(results){
  const operation = results[0];

  // Check if the operation is already done
  console.log(`Operation completed: ${operation.done}`);

  // Fetch latest progress metadata
  operation.getMetadata().then(metadataResponse => {
    const metadata = metadataResponse[0];
    console.log(`Processing progress: ${metadata.progressPercent}%`);
    console.log(`Started at: ${metadata.startTime}`);
  });

  // Optional: Wait for completion and get final results
  operation.promise()
    .then(finalResults => {
      console.log('Annotation finished successfully!');
      console.log('Results:', finalResults[0]);
    })
    .catch(err => {
      console.error('Annotation failed:', err);
    });

  res.json(operation);
});

Option 2: Query by Saved Operation Name

If you stored the operation’s name (from operation.name) in a database or config for later reference, use the Video Intelligence client’s getOperation() method to retrieve its status anytime:

const { VideoIntelligenceServiceClient } = require('@google-cloud/video-intelligence');
const client = new VideoIntelligenceServiceClient();

// Replace with your saved operation name
const savedOperationName = 'projects/your-project-id/operations/unique-operation-id';

client.getOperation({name: savedOperationName}).then(response => {
  const operation = response[0];
  
  console.log(`Operation completed: ${operation.done}`);
  
  if (operation.done) {
    if (operation.result) {
      console.log('Final annotation results:', operation.result);
    } else if (operation.error) {
      console.error('Operation failed with error:', operation.error);
    }
  } else {
    // Get progress for in-flight operations
    client.getOperationMetadata({name: savedOperationName}).then(metaResponse => {
      console.log(`Current progress: ${metaResponse[0].progressPercent}%`);
    });
  }
});

Quick Tips:

  • metadata.progressPercent gives a rough estimate of how much processing is complete.
  • When operation.done is true, always check operation.result for successful annotations or operation.error for failures.
  • Ensure your service account has the videointelligence.operations.get permission to fetch operation statuses.

内容的提问来源于stack exchange,提问作者Tarun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:57:57