s3-aiTextToSpeech
parent
2cbf1ebbaa
commit
5e23f73645
|
@ -7,7 +7,8 @@ const aiTextToSpeech = async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const polly = new AWS.Polly();
|
const polly = new AWS.Polly();
|
||||||
|
const s3 = new AWS.S3();
|
||||||
|
const BUCKET_NAME = 'polly-bs';
|
||||||
const voiceIdMap = {
|
const voiceIdMap = {
|
||||||
"Ava": "Ivy",
|
"Ava": "Ivy",
|
||||||
"Monstero": "Justin",
|
"Monstero": "Justin",
|
||||||
|
@ -30,22 +31,67 @@ const aiTextToSpeech = async (req, res) => {
|
||||||
const params = {
|
const params = {
|
||||||
Text: text,
|
Text: text,
|
||||||
OutputFormat: 'mp3',
|
OutputFormat: 'mp3',
|
||||||
Engine:'neural',
|
|
||||||
VoiceId: pollyVoiceId,
|
VoiceId: pollyVoiceId,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
function generateTimestampWithRandomDigits() {
|
||||||
|
// Get the current date and time
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
// Format the current date as YYYY-MM-DD_HH-mm-ss
|
||||||
|
const formattedDate = now.toISOString().replace('T', '_').replace(/\..+/, '').replace(/:/g, '-');
|
||||||
|
|
||||||
|
// Generate 4 random digits
|
||||||
|
const randomDigits = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
|
||||||
|
|
||||||
|
// Concatenate the formatted date with the random digits
|
||||||
|
return `${formattedDate}_${randomDigits}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example usage
|
||||||
|
const folder = generateTimestampWithRandomDigits();
|
||||||
|
|
||||||
|
// Generate timestamp for folder structure
|
||||||
|
// const timestamp = moment().format('YYYY-MM-DD_HH-mm-ss');
|
||||||
|
const folderName = `audio_files/${folder}`;
|
||||||
|
|
||||||
|
// Request Polly to synthesize speech
|
||||||
const data = await polly.synthesizeSpeech(params).promise();
|
const data = await polly.synthesizeSpeech(params).promise();
|
||||||
const audioBuffer = data.AudioStream;
|
const audioBuffer = data.AudioStream;
|
||||||
|
|
||||||
|
// Define S3 paths
|
||||||
|
const audioFilePath = `${folderName}/speech.mp3`;
|
||||||
|
const textFilePath = `${folderName}/text.txt`;
|
||||||
|
|
||||||
|
// Upload audio file to S3
|
||||||
|
await s3.upload({
|
||||||
|
Bucket: BUCKET_NAME,
|
||||||
|
Key: audioFilePath,
|
||||||
|
Body: audioBuffer,
|
||||||
|
ContentType: 'audio/mpeg'
|
||||||
|
}).promise();
|
||||||
|
|
||||||
|
// Upload text file to S3
|
||||||
|
await s3.upload({
|
||||||
|
Bucket: BUCKET_NAME,
|
||||||
|
Key: textFilePath,
|
||||||
|
Body: text,
|
||||||
|
ContentType: 'text/plain'
|
||||||
|
}).promise();
|
||||||
|
|
||||||
|
// Return success response with S3 paths
|
||||||
res.json({
|
res.json({
|
||||||
audioContent: audioBuffer.toString('base64'),
|
message: 'Speech generated and saved to S3 successfully',
|
||||||
|
audioFileUrl: `https://${BUCKET_NAME}.s3.amazonaws.com/${audioFilePath}`,
|
||||||
|
textFileUrl: `https://${BUCKET_NAME}.s3.amazonaws.com/${textFilePath}`
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error generating speech:', error);
|
console.error('Error generating speech or saving to S3:', error);
|
||||||
res.status(500).json({ error: 'Error generating speech' });
|
res.status(500).json({ error: 'Error generating speech or saving to S3' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = aiTextToSpeech;
|
module.exports = aiTextToSpeech;
|
Loading…
Reference in New Issue