Skip to content

Commit

Permalink
feat: store video durations while uploading a video (foyzulkarim#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
aninda052 committed Jul 26, 2023
1 parent 047667d commit 649d500
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
7 changes: 5 additions & 2 deletions server/src/modules/models/video/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions server/src/modules/models/video/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 23 additions & 3 deletions server/src/modules/queues/video-processor.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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 };

0 comments on commit 649d500

Please sign in to comment.