i want to make ann web app where i can upload a video. After uploading video its should be compress by ffmpeg after that i want to keep the data on aws S3.
I can compress by ffmpeg. but i have no idea which file i should keep in s3 because after compress its create many chunk file and one mpd xml file.
Also i want to stream the video from mpd file after fetching data from s3.
here is my upload function code.
const videoPath = req.file.path; const outputDir = path.join(__dirname, 'dash'); const uniqueId = uuidv4(); const outputFilePath = path.join(outputDir,
${uniqueId}_stream.mpd); const bucketName = process.env.S3_BUCKET_NAME; const s3Key =
dash/${uniqueId}_stream.mpd`;
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
ffmpeg(videoPath)
.outputOptions([
'-profile:v main',
'-use_template 1',
'-use_timeline 1',
'-b:v 1000k',
'-b:a 128k',
'-f dash'
])
.on('end', async () => {
console.log('DASH file created successfully');
try {
const s3Url = await uploadToS3(outputFilePath, bucketName, s3Key);
res.status(200).json({ message: 'Video uploaded and DASH file created', url: s3Url });
} catch (err) {
console.error('Error uploading to S3: ', err);
res.status(500).json({ message: 'Error uploading to S3', error: err.message });
}
})
.on('error', (err) => {
console.error('Error processing video: ', err);
res.status(500).json({ message: 'Error processing video', error: err.message });
})
.save(outputFilePath);`
`const uploadToS3 = (filePath, bucketName, key) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) return reject(err);
const params = {
Bucket: bucketName,
Key: key,
Body: data,
ContentType: 'application/dash+xml'
};
s3.upload(params, (err, data) => {
if (err) return reject(err);
resolve(data.Location);
});
});
});
};`
i have tried this version of code.
now i want to know what is the best to way to keep data in s3 for compress video after ffmpeg.
Kamruzzaman Rabeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.