I have uploaded a 1GB .mp4 file to an AWS S3 bucket. Using the AWS-SDK provided by the npm package, I am able to download the entire video. However, I have a specific requirement to generate a thumbnail at the 6-second mark of the video. Currently, I download the entire 1GB video to my local machine and then generate the thumbnail at the desired duration.
To optimize server resources and reduce disk load, I plan to download only the first 10 seconds of the video, which should be approximately 10MB or less in size. By doing so, I can significantly reduce download time and server load while still fulfilling my requirement of generating the thumbnail at the 6-second mark. Therefore, instead of downloading the entire 1GB video, I aim to download only the 10MB segment corresponding to the first 10 seconds of the video.
I am using nodejs, expressJS, as backed Technologies.
`
`async function downloadS3FileToLocalDirAndReturnPath(videoKey) {
return new Promise(async (resolve, reject) => {
try {
AWS.config.update({
accessKeyId: config.AWS.KEYS.accessKeyId,
secretAccessKey: config.AWS.KEYS.secretAccessKey,
region: config.AWS.KEYS.region,
httpOptions: config.AWS.KEYS.httpOptions
});
const s3 = new AWS.S3();
// Specify the local file path where you want to save the downloaded video
const localFilePath = `${os.tmpdir()}/${Date.now()}_sre.mp4`;
// Configure the parameters for the S3 getObject operation
const params = {
Bucket: config.AWS.S3_BUCKET,
Key: videoKey
};
const result = await s3.getObject(params).promise();
const fileContent = result.Body;
fs.writeFileSync(localFilePath, fileContent);
resolve(localFilePath);
} catch (error) {
reject(error);
}
});
}`
this code was working fine to download the whole video , but i need to download only first 10 seconds duration
S3: How to do a partial read / seek without downloading the complete file?
I tried this ,before posting this question with above post, video was downloading , it was not playing , by throwing this error , the file contains no playable streams
async function generateThumbnails(videoKey) {
const s3 = new AWS.S3();
const params = {
Bucket: KEYS.bucket,
Key: videoKey, // Specify the key of the video file in S3
Range: `bytes=0-${1024 * 800}`, // Specify the range of bytes you want to retrieve
};
const file = fs.createWriteStream(`/tmp/${Date.now()}_rama.mp4`);
const s3Stream = s3.getObject(params).createReadStream();
s3Stream.pipe(file);
s3Stream.on("error", (error) => {
console.log("Error Occured while File downloading!! ");
});
s3Stream.on("finish", () => {
console.log("File downloaded Successfully ");
});
}