Reading and writing metadata from MP3 files
In this chapter's introduction, we mentioned the fact that in Elixir, strings are binaries. In this recipe, we will use a binary file (an MP3 file) and apply some of the operations that we previously performed on strings. We will pattern match the MP3 binary file to extract some information—ID3 v2 information—and we will replace a portion of the file—ID3 v1 information—by constructing a new string and concatenating it to the end of the binary file. In the end, we will still have a proper MP3 file that we will be able to play on our favorite music player!
Note
MP3 files have some metadata stored on them in the form of ID3 tags. The first version of the ID3 tag was stored in the last 128 bytes of the file. The new ID3 tag (v2) is stored at the beginning of the file and may have variable length.
Getting ready
To get started, create a new mp3_info.ex
file and add the following code:
defmodule Mp3Info do @file_name "Divider.mp3" def id3_v2_basic_info...