如何用Node.js通过Google Cloud Video Intelligence API查询视频标注请求状态?
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
doneboolean property. Whentrue, 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.progressPercentgives a rough estimate of how much processing is complete.- When
operation.doneistrue, always checkoperation.resultfor successful annotations oroperation.errorfor failures. - Ensure your service account has the
videointelligence.operations.getpermission to fetch operation statuses.
内容的提问来源于stack exchange,提问作者Tarun

