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 Aug 3, 2023
1 parent 3d56a9c commit 979e041
Show file tree
Hide file tree
Showing 2 changed files with 28 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 @@ -9,6 +9,7 @@ const {
const { validate } = require('./request');
const { VIDEO_QUEUE_EVENTS: QUEUE_EVENTS } = require('../../queues/constants');
const { addQueueItem } = require('../../queues/queue');
const { getVideoDurations } = require('../../queues/video-processor');

const BASE_URL = `/api/videos`;

Expand Down Expand Up @@ -133,15 +134,17 @@ 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,
status: "pending"
status: "pending",
durations:videoDurations
};
console.log('dbPayload', dbPayload);
// TODO: save the file info and get the id from the database
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 979e041

Please sign in to comment.