Issue: Error streaming a video file to GenAI from node.js for content generation #2049
Replies: 1 comment
-
Solution:The docs were a bit scarce when it came to my use case, but the solution included less code than initially thought. I hope this can be of use to someone. // routes.js
const { Router } = require('express')
const analyseRoutes = require('./analyseRoutes.js')
const router = Router()
const multer = require('multer')
const upload = multer({ storage: multer.memoryStorage() });
router.use('/analyse',upload.single("videos"), analyseRoutes)
module.exports= router; // analyseRoutes.js
const {Router} = require('express')
const router = Router()
const analyseControllers = require('../controllers/analyseControllers.js')
router.post('', analyseControllers.analyseVideo)
module.exports = router; // analyseControllers.js
const {createUserContent, createPartFromUri, Part} = require('@google/genai')
const { GoogleAuth } = require('google-auth-library');
const genAI = require('../services/genAI.js')
const approvedKeywords = [
// ...List of keywords
];
exports.analyseVideo = async (req, res) => {
console.log(req.file);
try {
if (!req.file) {
return res.status(400).json({ error: 'No video file uploaded.' });
}
const videoBuffer = req.file.buffer;
let mimeType = req.file.mimetype;
if (mimeType == 'application/octet-stream') {
mimeType='video/mov'
}
const promptText = process.env.PROMPT + ` Keywords: ${approvedKeywords.join(',')}`
const videoPart = {
inlineData: {
data: videoBuffer.toString('base64'),
mimeType: mimeType
}
};
const response = await genAI.models.generateContent({
model: "gemini-2.0-flash",
contents: createUserContent([
videoPart,
promptText,
]),
});
const analysis = response.candidates[0].content.parts[0].text;
console.log(response)
res.status(200).json({ analysis: analysis });
} catch (error) {
console.error('Error analyzing video:', error);
res.status(500).json({ error: 'Failed to analyze video.', details: error.message });
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am trying to connect a very simple node.js server running on localhost to upload a single video, get it processed by GenAI where it would choose from a series of keywords (from a given list) to describe said video.
I managed to wire everything up via trial-error, but this version of the code gives me some very cryptic error feedback:
As far as troubleshooting, I have looked at the docs but could not find an example that even remotely resembles what I am trying to accomplish. I have console.log'ed the heck at out of every env variable, every config params, every bit of the request object and it all looks correct. I am obviously missing something!
Thank you in advance for your help, I am here for any clarification.
The route:
https://github.com/phildehovre/hpm-node-api/blob/master/routes/analyseRoutes.js
The controller:
https://github.com/phildehovre/hpm-node-api/blob/master/controllers/analyseControllers.js
Beta Was this translation helpful? Give feedback.
All reactions