Extracting Metadata from Audio Files

Saturday, March 18th 2023

To extract artist and album information from an audio file using JavaScript, you can use the jsmediatags library. This library allows you to read and write metadata tags for audio files in various formats, including MP3, AAC, and FLAC.

Here's an example of how you can use jsmediatags to extract artist and album information from an MP3 file:

const jsmediatags = require('jsmediatags'); // Load the MP3 file jsmediatags.read('song.mp3', { onSuccess: function(tag) { // Extract the artist and album information from the tag const artist = tag.tags.artist; const album = tag.tags.album; console.log('Artist:', artist); console.log('Album:', album); }, onError: function(error) { console.log('Error:', error.type, error.info); } });

In this example, we first import the jsmediatags library and then use the read method to load the song.mp3 file. We provide two callback functions: onSuccess and onError. The onSuccess callback is called when the file is loaded successfully, and it provides a tag object containing the metadata for the file. We can then extract the artist and album fields from the tag object and log them to the console.

Note that jsmediatags is not a foolproof solution and may not be able to extract metadata from all audio files. Some audio files may not have any metadata, while others may have incomplete or inaccurate metadata. In addition, the metadata format may differ between audio file formats. Therefore, it's important to test your code with a variety of audio files to ensure that it works as expected.

Supported Formats

The jsmediatags NPM package supports several audio file formats, including MP3, OGG, WAV, FLAC, M4A, and MP4.