Commit 8107f789 by ramdayalmunda

progress bar handling while generating video

parent 685ede17
......@@ -17,24 +17,25 @@ module.exports.generateVideo = async function (req, res) {
let uid;
let _id
let filesCreated = []
let socketPayload
try {
let tutorShotData = req.body.tutorShotData;
let videoPath; // local path of the video file
let thumbnailPath // local path of the thumbnail file
let fileName; // remote name of the final video file
let thumbnailName; // remote name of the final thumbnail
let updatedData;
if (tutorShotData) {
uid = tutorShotData.uid
_id = tutorShotData._id
let socketPayload = {
socketPayload = {
uid: tutorShotData.uid,
_id: tutorShotData._id,
task: "Video Generation",
percent: 0,
subTask: [ { task: "Image Compiled", percent: 0 } ]
subTask: [{ task: "Image Compiled", percent: 0 }]
}
if ( tutorShotData?.audio?.length ){
if (tutorShotData?.audio?.length) {
socketPayload.subTask.push({ task: "Audio mixing", percent: 0 })
}
......@@ -50,7 +51,7 @@ module.exports.generateVideo = async function (req, res) {
let frameNumber = 1;
let imageArr = []
socket.emit("videoGenerationProgress", socketPayload)
socket.emit("videoGenerationProgress", socketPayload)
for (let i = 0; i < tutorShotData.segments.length; i++) {
if (tutorShotData.segments[i].imageUrl) tutorShotData.segments[i].signedImageUrl = await generatePreSignedGetUrl(tutorShotData.segments[i].imageUrl)
......@@ -83,9 +84,9 @@ module.exports.generateVideo = async function (req, res) {
filesCreated.push(thumbnailPath)
}
socketPayload.subTask[0].percent = Math.round(((i+1)/tutorShotData.segments.length)*75); // 75% for generating images
socketPayload.percent = Math.round( tutorShotData?.audio?.length?socketPayload.subTask[0].percent/2:socketPayload.subTask[0].percent );
socket.emit("videoGenerationProgress", socketPayload);
socketPayload.subTask[0].percent = Math.round(((i + 1) / tutorShotData.segments.length) * 75); // 75% for generating images
socketPayload.percent = Math.round(tutorShotData?.audio?.length ? socketPayload.subTask[0].percent / 2 : socketPayload.subTask[0].percent);
socket.emit("videoGenerationProgress", socketPayload);
}
......@@ -101,8 +102,8 @@ module.exports.generateVideo = async function (req, res) {
await createVideoFromImages([], options);
filesCreated.push(path.dirname(videoPath))
socketPayload.subTask[0].percent = 100; // 100% if video(without audio) is generated
socketPayload.percent = Math.round( tutorShotData?.audio?.length?socketPayload.subTask[0].percent/2:socketPayload.subTask[0].percent );
socket.emit("videoGenerationProgress", socketPayload);
socketPayload.percent = Math.round(tutorShotData?.audio?.length ? socketPayload.subTask[0].percent / 2 : socketPayload.subTask[0].percent);
socket.emit("videoGenerationProgress", socketPayload);
!(async function () {
deleteFile(imageArr)
......@@ -112,29 +113,42 @@ module.exports.generateVideo = async function (req, res) {
if (tutorShotData.audio?.length) {
socketPayload.percent = 50
socketPayload.subTask[0].percent = 50
socket.emit("videoGenerationProgress", socketPayload)
socket.emit("videoGenerationProgress", socketPayload)
let audioPath = path.join(TEMP_AUDIO_DIR, `ts-${tutorShotData._id}.mp3`); // path to only audio
await createAudio(tutorShotData.audio, audioPath)
finalVideoPath = path.join(TEMP_VIDEO_DIR, `ts-final-${tutorShotData._id}.mp4`); // path to combined video and audio
await joinAudioVideo(videoPath, audioPath, finalVideoPath)
}else{
socketPayload.percent = 100
socketPayload.subTask[0].percent = 100
socket.emit("videoGenerationProgress", socketPayload)
} else {
socketPayload.percent = 99
socketPayload.subTask[0].percent = 99
socket.emit("videoGenerationProgress", socketPayload)
}
}
// uploading process
let remotePath = path.join(CONFIG.S3_TUTOR_SHOT, fileName)
let remoteThumbnailPath = path.join(CONFIG.S3_TUTOR_SHOT, thumbnailName)
await uploadToAwsS3(videoPath, remotePath)
await uploadToAwsS3(thumbnailPath, remoteThumbnailPath)
tutorShotData.thumbnailUrl = remoteThumbnailPath
tutorShotData.signedVideoUrl = await generatePreSignedGetUrl(remotePath)
tutorShotData.signedThumbnailUrl = await generatePreSignedGetUrl(remoteThumbnailPath)
tutorShotData.videoUrl = remotePath
tutorShotData.generatingVideo = false
socketPayload.tutorShotData = {
videoUrl: remotePath,
thumbnailUrl: remoteThumbnailPath,
signedVideoUrl: tutorShotData?.signedVideoUrl,
signedThumbnailUrl: tutorShotData?.signedThumbnailUrl,
}
socketPayload.percent = 100
socketPayload.complete = true
socket.emit("videoGenerationProgress", socketPayload)
let remotePath = path.join(CONFIG.S3_TUTOR_SHOT, fileName)
let remoteThumbnailPath = path.join(CONFIG.S3_TUTOR_SHOT, thumbnailName)
console.log('---Tutor-shot-generated----', tutorShotData?._id)
}
await uploadToAwsS3(videoPath, remotePath)
await uploadToAwsS3(thumbnailPath, remoteThumbnailPath)
tutorShotData.thumbnailUrl = remoteThumbnailPath
tutorShotData.videoUrl = remotePath
tutorShotData.generatingVideo = false
console.log('----tutor-shot-generated----', tutorShotData?._id)
res.status(STATUS_CODE.OK).json(tutorShotData)
} catch (err) {
console.log('------ERROR:GENERATING VIDEO', err)
......
......@@ -3,7 +3,6 @@ const socketIO = require('socket.io-client')
const CONFIG = require('./config.js')
let socket = socketIO(CONFIG.SOCKET_URL)
socket.on('connect', ()=>{console.log('Socket connected!') })
socket.on('disconnect', ()=>{ console.log('Socket disconnected!') })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment