diff --git a/server/src/modules/models/video/controller.js b/server/src/modules/models/video/controller.js index 565e334..f4508d1 100644 --- a/server/src/modules/models/video/controller.js +++ b/server/src/modules/models/video/controller.js @@ -5,6 +5,7 @@ const { validate } = require('./request'); const { Video, name } = require('./model'); const { VIDEO_QUEUE_EVENTS: QUEUE_EVENTS } = require('../../queues/constants'); const { addQueueItem } = require('../../queues/queue'); +const { getVideoDurations } = require('../../queues/video-processor'); const { getFakeVideosData } = require('./data'); @@ -130,14 +131,16 @@ const setupRoutes = (app) => { app.post(`${BASE_URL}/upload`, uploadProcessor, async (req, res) => { try { - console.log('POST upload', JSON.stringify(req.body)); + + const videoDurations = await getVideoDurations(`./${req.file.path}`) const dbPayload = { ...req.body, fileName: req.file.filename, originalName: req.file.originalname, recordingDate: new Date(), videoLink: req.file.path, - viewCount:0 + viewCount:0, + durations:videoDurations }; console.log('dbPayload', dbPayload); // TODO: save the file info and get the id from the database diff --git a/server/src/modules/models/video/schema.js b/server/src/modules/models/video/schema.js index a27bb5e..4046027 100644 --- a/server/src/modules/models/video/schema.js +++ b/server/src/modules/models/video/schema.js @@ -34,6 +34,11 @@ const updateSchema = async (db) => { minimum: 0, description: "must be an integer", }, + durations: { + bsonType: "double", + minimum: 0.0, + description: "video durations in second. Must be a double", + }, visibility: { enum: VIDEO_VISIBILITIES, description: "can only be one of the enum values and is required", diff --git a/server/src/modules/queues/video-processor.js b/server/src/modules/queues/video-processor.js index 32f32db..6105035 100644 --- a/server/src/modules/queues/video-processor.js +++ b/server/src/modules/queues/video-processor.js @@ -1,8 +1,12 @@ /** execute function will take a filePath and run ffmpeg command to convert it to mp4 */ -const ffmpegInstaller = require("@ffmpeg-installer/ffmpeg"); const ffmpeg = require("fluent-ffmpeg"); + +const ffmpegInstaller = require("@ffmpeg-installer/ffmpeg"); ffmpeg.setFfmpegPath(ffmpegInstaller.path); -console.log(ffmpegInstaller.path, ffmpegInstaller.version); + +const ffprobeInstaller = require('@ffprobe-installer/ffprobe'); +ffmpeg.setFfprobePath(ffprobeInstaller.path); + const path = require("path"); const { VIDEO_QUEUE_EVENTS: QUEUE_EVENTS } = require("./constants"); const { addQueueItem } = require("./queue"); @@ -102,4 +106,20 @@ const processMp4ToHls = async (filePath, outputFolder, jobData) => { return; }; -module.exports = { processRawFileToMp4, processMp4ToHls, generateThumbnail }; +const getVideoDurations = (filePath) => { + + return new Promise((resolve,reject) => { + let durations = 0.0 + ffmpeg.ffprobe(filePath, function(err, metadata) { + if(!err){ + durations = metadata.format.duration + } + resolve(durations) + }); + }) + + +} + + +module.exports = { processRawFileToMp4, processMp4ToHls, generateThumbnail, getVideoDurations };