Audio Resampling in Node.js - JavaScript

arhamrumi

Arham Rumi

Posted on April 7, 2023

Audio Resampling in Node.js - JavaScript

In various applications—whether web, mobile, or desktop—there are often times when you need to save audio files. Depending on your requirements, you might prioritize minimizing storage space over sound quality. Audio files can vary significantly in size based on their format; for instance, WAV files are uncompressed and tend to be much larger than MP3 files.

In many cases, the specific file format may not be critical; what matters most is the audio content itself. Converting WAV files to MP3 format is an effective solution for reducing storage usage.

Another approach to consider is resampling, where you adjust the bit depth and frequency of the audio file. While resampling serves various purposes, we will focus on using it primarily to conserve storage space.

Without further ado, let’s explore how this process works. Before executing the code below, ensure that you have installed the FFmpeg tool on your system.

Here is the quick guide to install ffmpeg on Windows.


Assuming that you have successfully installed ffmpeg tool on your system, let's dig into the code.

  • Import required modules
const path = require("path");
const ffmpeg = require("fluent-ffmpeg");
Enter fullscreen mode Exit fullscreen mode

You have to install fluent-ffmpeg module using this command.

npm i fluent-ffmpeg
Enter fullscreen mode Exit fullscreen mode
  • Create a function to resample audio file

In this process, fluent-ffmpeg will handle the core tasks, including resampling the audio. Within the audioFrequency() method, you can specify your desired sample rate, and the save() method will save the converted file to the designated path. This conversion operation returns a wealth of information about the process, which is why we store it in the result variable. This allows us to perform additional operations based on that information if needed.

const compressAudio = async (inputFile, outputFile) => {
  const result = ffmpeg(inputFile).audioFrequency(16000).save(outputFile);
  if (result) {
    console.log("Audio compressed successfully");
  }
};
Enter fullscreen mode Exit fullscreen mode
  • Calling the function

Now we have to call it by giving two arguments. i.e. the first one will be the path to input file and the second one will be the path to our output file.

let inputFile = "path/to/source_file.wav";
let dir_name = path.dirname(inputFile);
let file_name = path.parse(inputFile).name;

let outputFile = dir_name + "\\" + file_name + ".mp3";

await compressAudio(inputFile, outputFile);
Enter fullscreen mode Exit fullscreen mode

That's all. You will see that in your given directory a new file is created.


In conclusion, managing audio files efficiently is essential for optimizing storage and ensuring the best user experience in your applications. By leveraging tools like Fluent-FFmpeg, you can easily convert audio formats and resample files to meet your specific needs. Whether you’re developing web, mobile, or desktop applications, understanding how to handle audio files effectively can enhance your project’s overall functionality.

If you have any questions or encounter any challenges while implementing these techniques, feel free to leave a comment below. Don't forget to share this article with fellow developers who may benefit from it. Happy coding!

💖 💪 🙅 🚩
arhamrumi
Arham Rumi

Posted on April 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related