Applicable when
- You need to concatenate two or more audio files from multiple audio sources and you would like to control the multiple audio sources separately.
Implementation
In use cases such as uploading an audio prompt to Amazon Connect we don't have the ability to upload multiple audio files in a single prompt. For example, if you want to tell the user to choose digits for different languages you would be forced to record a single audio file for all languages which will be hard to maintain. The most practical approach in these situations is to maintain separate copies of the audio files (which you can modify independently) and then have an automated process to concatenate all the audio recordings as you wish.
If you are also working on a NodeJs environment, you might want to make use of audioconcat which you can incorporate to your project under an MIT license. Before, proceeding to the actual code you will also need to install audioconcat's dependencies: @ffmpeg-installer/ffmpeg and fluent-ffmpeg. You can install all of these via npm install.
import { Log } from '../utils/logging';
const audioconcat = require('audioconcat');
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');
const ffmpeg = require('fluent-ffmpeg');
export function generateAudioPrompts(): void {
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
const songs = [
'audio-file1.mp3',
'audio-file2.mp3'
];
audioconcat(songs)
.concat('concatenated-audio.mp3')
.on('error', error => Log.error('Failed to concatenate files', error))
.on('end', () => Log.info('Generating audio prompts'));
ffmpeg('language-choice.mp3')
.toFormat('wav')
.on('error', err => Log.error('An error occurred: ', err))
.on('end', () => Log.info('Audio prompts done'))
.save('../connect/language-prompts/language-choice.wav');
}
As we can see above, we first set the installer path in ffmpeg. When this is done, we can simply select the audio files we want to concatenate and use audioconcat.concat. The parameter value will be the concatenated file name. Finally, we can make use of ffmpeg (now that we have it in our dependencies) to make final format conversion if needed and select exactly the path where we want to save the concatenated files.
Comments
0 comments
Please sign in to leave a comment.