Commit 19deb370 by Pragati Upadhyay

remove console

parent 7e59d23e
...@@ -3,7 +3,7 @@ const { generateCanvas, mergeImages } = require("../helper/tutor-shot") ...@@ -3,7 +3,7 @@ const { generateCanvas, mergeImages } = require("../helper/tutor-shot")
const path = require('path') const path = require('path')
const sharp = require('sharp') const sharp = require('sharp')
const fs = require('fs') const fs = require('fs')
const { createVideoFromImages, joinAudioVideo } = require("../helper/ffmpeg-helper") const { createVideoFromImages,createVideo, joinAudioVideo } = require("../helper/ffmpeg-helper")
const { deleteFile, downloadFile } = require("../helper/utilities") const { deleteFile, downloadFile } = require("../helper/utilities")
const { generatePreSignedGetUrl, uploadToAwsS3 } = require("../helper/upload") const { generatePreSignedGetUrl, uploadToAwsS3 } = require("../helper/upload")
const mongoose = require('mongoose') const mongoose = require('mongoose')
...@@ -42,7 +42,7 @@ module.exports.generateVideo = async function (req, res) { ...@@ -42,7 +42,7 @@ module.exports.generateVideo = async function (req, res) {
let imageDir = TEMP_IMAGE_DIR let imageDir = TEMP_IMAGE_DIR
let imagePathString = path.join(imageDir, `${tutorShotData._id}_%d.png`) let imagePathString = path.join(imageDir, `${tutorShotData._id}_%d.png`)
videoPath = path.join(TEMP_VIDEO_DIR, `${tutorShotData._id}.webm`) videoPath = path.join(TEMP_VIDEO_DIR, `${tutorShotData._id}.mp4`)
thumbnailPath = path.join(TEMP_IMAGE_DIR, `${tutorShotData._id}_thumbnail`) thumbnailPath = path.join(TEMP_IMAGE_DIR, `${tutorShotData._id}_thumbnail`)
fileName = `${tutorShotData._id}.webm` fileName = `${tutorShotData._id}.webm`
thumbnailName = `${tutorShotData._id}_thumbnail.png` thumbnailName = `${tutorShotData._id}_thumbnail.png`
...@@ -66,18 +66,18 @@ module.exports.generateVideo = async function (req, res) { ...@@ -66,18 +66,18 @@ module.exports.generateVideo = async function (req, res) {
dimension: tutorShotData.segments[i].dimension dimension: tutorShotData.segments[i].dimension
} }
) )
let limit = 24 * ((tutorShotData.segments[i]?.duration) ? (tutorShotData.segments[i].duration) : 2); let limit = ((tutorShotData.segments[i]?.duration) ? (tutorShotData.segments[i].duration) : 2);
let frameNum = 0 let frameNum = 0
let firstImage = path.join(imageDir, `${tutorShotData._id}_${frameNumber++}.png`) let firstImage = path.join(imageDir, `${tutorShotData._id}_${frameNumber++}.png`)
await sharp(mergedImage).toFile(firstImage) await sharp(mergedImage).toFile(firstImage)
imageArr.push(firstImage) imageArr.push(firstImage)
filesCreated.push(firstImage) filesCreated.push(firstImage)
do { // do {
let fileName = path.join(imageDir, `${tutorShotData._id}_${frameNumber++}.png`) // let fileName = path.join(imageDir, `${tutorShotData._id}_${frameNumber++}.png`)
imageArr.push(fileName) // imageArr.push(fileName)
filesCreated.push(fileName) // filesCreated.push(fileName)
fs.copyFileSync(firstImage, fileName) // fs.copyFileSync(firstImage, fileName)
} while (++frameNum < limit) // } while (++frameNum < limit)
if (i == 0 && thumbnailPath) { if (i == 0 && thumbnailPath) {
fs.copyFileSync(firstImage, thumbnailPath) fs.copyFileSync(firstImage, thumbnailPath)
...@@ -98,8 +98,8 @@ module.exports.generateVideo = async function (req, res) { ...@@ -98,8 +98,8 @@ module.exports.generateVideo = async function (req, res) {
imagePathString: imagePathString, imagePathString: imagePathString,
deleteImages: true,// default is true deleteImages: true,// default is true
} }
await createVideo(imageArr,videoPath)
await createVideoFromImages([], options); // await createVideoFromImages([], options);
filesCreated.push(path.dirname(videoPath)) filesCreated.push(path.dirname(videoPath))
socketPayload.subTask[0].percent = 100; // 100% if video(without audio) is generated 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); socketPayload.percent = Math.round(tutorShotData?.audio?.length ? socketPayload.subTask[0].percent / 2 : socketPayload.subTask[0].percent);
...@@ -129,7 +129,7 @@ module.exports.generateVideo = async function (req, res) { ...@@ -129,7 +129,7 @@ module.exports.generateVideo = async function (req, res) {
let remotePath = path.join(CONFIG.S3_TUTOR_SHOT, fileName) let remotePath = path.join(CONFIG.S3_TUTOR_SHOT, fileName)
let remoteThumbnailPath = path.join(CONFIG.S3_TUTOR_SHOT, thumbnailName) let remoteThumbnailPath = path.join(CONFIG.S3_TUTOR_SHOT, thumbnailName)
await uploadToAwsS3(videoPath, remotePath) let url = await uploadToAwsS3(videoPath, remotePath)
await uploadToAwsS3(thumbnailPath, remoteThumbnailPath) await uploadToAwsS3(thumbnailPath, remoteThumbnailPath)
tutorShotData.thumbnailUrl = remoteThumbnailPath tutorShotData.thumbnailUrl = remoteThumbnailPath
tutorShotData.signedVideoUrl = await generatePreSignedGetUrl(remotePath) tutorShotData.signedVideoUrl = await generatePreSignedGetUrl(remotePath)
......
...@@ -24,7 +24,88 @@ const path = require('path') ...@@ -24,7 +24,88 @@ const path = require('path')
* } options * } options
* @returns * @returns
*/ */
module.exports.createVideoFromImages = async function (imageArr, options) { function createClipFromImage(imagePath, duration, outputClipPath, callback) {
ffmpeg(imagePath)
.inputOptions('-loop 1')
.inputOptions(`-t ${duration}`)
.inputOptions('-framerate 1')
.outputOptions([
'-c:v libx264', // Use H.264 codec
// '-pix_fmt yuv420p', // Pixel format compatible with most devices
'-crf 23', // Constant Rate Factor that balances quality and file size
'-preset veryfast' // Faster encoding with reasonable file size
])
.output(outputClipPath)
.on('end', () => {
console.log(`Clip created: ${outputClipPath}`);
callback(null, outputClipPath); // Pass the clip path on success
})
.on('error', (err) => {
console.error(`Error: ${err.message}`);
callback(err);
})
.run();
}
function concatenateClips(clips, outputPath, callback) {
const fileList = 'filelist.txt';
const fileContent = clips.map(clip => `file '${clip}'`).join('\n');
fs.writeFileSync(fileList, fileContent);
console.log("created", outputPath, 'x' )
ffmpeg()
.input(fileList)
.inputOptions(['-f concat','-safe 0']) // Corrected input options
.outputOptions('-c copy')
.output(outputPath)
.on('end', () => {
console.log('Final video created successfully');
callback(null);
fs.unlinkSync(fileList);
clips.forEach(clip => fs.unlinkSync(clip));
})
.on('error', (err) => {
console.error(`Error: ${err}`);
callback(err);
})
.run();
}
module.exports.createVideo = async function(screenshots,outputPath) {
return new Promise(async (res, rej) => {
let clips = [];
for (let index = 0; index < screenshots.length; index++) {
const screenshot = screenshots[index];
const duration = screenshots[index]?.duration ? screenshots[index].duration : 3;
const clipPath = `clip${index}.mp4`; // Temporary clip path
createClipFromImage(screenshot, duration, clipPath, (err, clip) => {
if (err) {
console.error(`Failed to create clip for ${screenshot}: ${err}`);
return;
}
clips.push(clip);
if (index === screenshots.length - 1) {
// All screenshots processed, concatenate clips
concatenateClips(clips, outputPath, (err) => {
if (err) {
console.error(`Failed to concatenate clips: ${err}`);
return;
}
console.log('All done');
res();
});
}
});
}
});
}
module.exports.createVideoFromImages = async function (imageArr,options) {
return new Promise(async (res, rej) => { return new Promise(async (res, rej) => {
......
...@@ -31,8 +31,6 @@ module.exports.uploadToAwsS3 = async function (localPath, remotePath, options) { ...@@ -31,8 +31,6 @@ module.exports.uploadToAwsS3 = async function (localPath, remotePath, options) {
// successful response // successful response
}); });
if (options && !options.preventUnlink) { if (options && !options.preventUnlink) {
console.log("🚀 ~ localPath:", localPath)
try { Fs.unlinkSync(localPath) } try { Fs.unlinkSync(localPath) }
catch (err) { console.log(err) } catch (err) { console.log(err) }
} }
......
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