convert-audio-to-video-mpeg
v1.0.1
Published
A package for converting audio files to video/MPEG format to ensure cross-platform compatibility, especially for Android, iOS, and Safari browsers.
Downloads
157
Maintainers
Keywords
Readme
Any Audio format to video/mpeg format coverter
The main aim of this package is to create a common audio format that can be supported on both iOS and Windows. While working on audio-related projects, many of us face the issue where the audio uploaded to our backend server does not play on iOS Safari. Alternatively, if it plays on Safari, it might not play on Google Chrome. This is the problem I faced and successfully overcame, so I will create this as a package for developers to use.
This npm package allows you to convert audio files (e.g., MP3) into a video format (Video/MPEG).
It can store the audio in a specified path or inside the server. The package will return the path, and if you want to store the audio in the cloud or another storage service, you can use the audio stored in the specified path.
This makes it easy to convert to a common format that can play audio across multiple platforms like iOS, web, and Android. While it is primarily focused on server-side use, you can also use it on the frontend.
Blog
I have written a blog post explaining the issue I faced and how I found a solution to overcome it. You can read more about it here to understand more about this package their i have shared my code that i have used to create this npm package.
Installation
To install the package into your project, run:
npm i convert-audio-to-video-mpeg
---
Fetures
- Converts audio (MP3, WAV, etc.) to MPEG (Video) format.
- Works across platforms: Android, iOS, Safari, and more.
- Store the converted video in the folder path you provided.
- Supported in bothe Common and ES modules.
Code
This is the example format:
convertAudioToVideo(uploadPath, audioFile)
.then((videoPath) => {
console.log('Video conversion successful:', videoPath);
})
.catch((error) => {
console.error('Conversion failed:', error);
});
---
You can use your logic inside the 'then' condition. If it is true, you can perform other operations to store the audio in the cloud using the provided path.
Example Code:
// Import the correct module (CommonJS or ES Module, based on your setup)
import { convertAudioToVideo } from 'convert-audio-to-video-mpeg'; // If you're using ES modules
// replace this with your original audio from the fronend or rest API
const audioFile = {
originalname: 'audio.mp3',
path: 'path present inside the audio file',
};
const uploadPath = 'provide the path to which you can store the audio file';
// Call the function and test it
convertAudioToVideo(uploadPath, audioFile)
.then((videoPath) => {
console.log('Video conversion successful:', videoPath);
})
.catch((error) => {
console.error('Conversion failed:', error);
});
After execution, you can store it on the local server or upload it to the cloud. If you successfully upload the audio to the cloud, you can delete the audio to clean the server by using the following command:
fs.unlink(videopath)
Use this code only if you want to delete the audio from the uploaded folder to clean your server. If you want the audio to stay on your server, do not use this command.
In Case of Error
Before using this package, ensure the following:
FFmpeg is installed on your system. You can download and install it from FFmpeg's official website.
Alternatively, you can use the
ffmpeg-static
package that comes bundled with the required binaries (this is automatically handled by the package).You will need a folder to temporarily store the converted video files. This is where the audio-to-video conversion will happen.