Rtransat

Rtransat

How to read id3v2 tags from mp3 files?

Hi, I’m new with Elixir and for learning I try to read id3v2 tags from mp3 files. The stdlib for working with binary seems awesome with pattern matching.
I have question about how to parse some data (title, artist, etc)
By following the spec it seems to have a flag with encodage information but I don’t know how to read it. I think I have utf16 for the title (TIT2) because I have some bytes to 0.

My current code:

file = File.stream!("./lib/file.mp3", [:read, :binary], 128)
id3tag_bytes = file |> Enum.take(1) |> hd

<<header::binary-size(10), rest::binary>> = id3tag_bytes

<<"ID3", major::binary-size(1), revision::binary-size(1), flags::binary-size(1),
  size::binary-size(4)>> =
  header

case flags do
  <<0>> -> IO.puts("No extended header")
  _ -> IO.puts("Extended header")
end

<<frame_overview::binary-size(10), frame::binary>> = rest

<<frame_id::binary-size(4), frame_size::binary-size(4), frame_flags::binary-size(2)>> =
  frame_overview

<<frame_size_int::size(4)-unit(8)>> = frame_size
IO.inspect(frame_size_int, label: "Frame Size")
<<title::binary-size(frame_size_int - 10), _rest::binary>> = frame

# id3 = <<header::binary-size(3), _v::binary-size(1), _flags::binary-size(1), _size::binary-size(4)>>

IO.inspect(header, label: "Header")
IO.inspect(:binary.decode_unsigned(major), label: "Version")
IO.inspect(:binary.decode_unsigned(revision), label: "Revision")
IO.inspect(:binary.decode_unsigned(flags), label: "Flags")
IO.inspect(frame_id, label: "Frame ID")
IO.inspect(:binary.decode_unsigned(frame_size), label: "Frame Size")
IO.inspect(size, label: "Size")
IO.inspect(frame_overview, label: "Frame Overview")
IO.inspect(title, label: "Title")
IO.inspect(title |> :unicode.characters_to_binary(:utf16, :utf8), label: "Title")

You can find the first 100 bytes from the file: id3tag_bytes

<<73, 68, 51, 3, 0, 0, 0, 4, 109, 110, 84, 73, 84, 50, 0, 0, 0, 19, 0, 0, 1,
  255, 254, 76, 0, 97, 0, 32, 0, 81, 0, 117, 0, 234, 0, 116, 0, 101, 0, 84, 80,
  69, 49, 0, 0, 0, 17, 0, 0, 1, 255, 254, 79, 0, 114, 0, 101, 0, 108, 0, 115, 0,
  97, 0, 110, 0, 84, 65, 76, 66, 0, 0, 0, 57, 0, 0, 1, 255, 254, 67, 0, 105, 0,
  118, 0, 105, 0, 108, 0, 105, 0, 115, 0, 97, 0, 116, 0, 105, 0, 111>>

The title in TIT2 frame should be “La Quête” which can be found here: <<76, 0, 97, 0, 32, 0, 81, 0, 117, 0, 234, 0, 116, 0, 101>> but like I said previously there are some bytes to 0 so I guess it’s encoded in utf16 and the tag is readable without the bytes 0.
<<76, 97, 32, 81, 117, 234::utf8, 116, 101>>

The frame size is 19 in header but we need to remove the header size so 19 - 10 = 9 so I think I need to convert utf16 to uf8 before reading the title, otherwise the size of my title is not the same.

Currently, the last IO.inspect print {:incomplete, "ǿ﹌a ", <<0>>}

Thx :wink:

Marked As Solved

kip

kip

ex_cldr Core Team

Here’s my final experiment in case its useful to you:

defmodule MP3 do
  def extract_tags(<<>>) do
    []
  end

  def extract_tags(<<0, _rest::binary>>) do
    []
  end

  def extract_tags(<<"ID3", _major::binary-size(1), _revision::binary-size(1), _flags::binary-size(1), _size::integer-size(32), rest::binary>>) do
    extract_tags(rest)
  end
  
  def extract_tags(data) do
    <<frame_overview::binary-size(10), frame::binary>> = data
    <<frame_id::binary-size(4), frame_payload_size::unsigned-integer-32, _frame_flags::binary-size(2)>> = frame_overview
    <<frame_payload::binary-size(frame_payload_size), rest::binary>> = frame

    {text, encoding} =
      case frame_payload do
        <<1, 0xff, 0xfe, text::binary>> -> {text, {:utf16, :little}}
        <<1, 0xfe, 0xff, text::binary>> -> {text, {:utf16, :big}}
        <<0, text::binary>> -> {text, :iso8859}  
      end
    
    frame_text =
      case encoding do
        :iso8859 -> :iconv.convert("iso8859-1", "utf8", text)
        encoding -> :unicode.characters_to_binary(text, encoding, :utf8)
      end

    [{frame_id, frame_text} | extract_tags(rest)]
  end
end

Example

iex> id3tag_bytes = File.read!("../Hells Bells.mp3")
iex> MP3.extract_tags(id3tag_bytes)
[
  {"TPE1", "AC/DC"},
  {"TYER", "1980"},
  {"TALB", "Back In Black"},
  {"TRCK", "01"},
  {"TIT2", "Hells Bells"}
]

Also Liked

kip

kip

ex_cldr Core Team

The following script is proof-of-concept you might find useful:

<<header::binary-size(10), rest::binary>> = id3tag_bytes
<<"ID3", major::binary-size(1), revision::binary-size(1), flags::binary-size(1), size::integer-size(32)>> = header
<<frame_overview::binary-size(10), frame::binary>> = rest
<<frame_id::binary-size(4), frame_size::unsigned-integer-32, frame_flags::binary-size(2)>> = frame_overview

frame_payload_size = frame_size
frame_payload = <<frame::binary-size(frame_payload_size)>>

{title, encoding} =
  case frame_payload do
    <<1, 0xff, 0xfe, title::binary>> -> {title, {:utf16, :little}}
    <<1, 0xfe, 0xff, title::binary>> -> {title, {:utf16, :big}}
    <<0, title::binary>> -> {title, :iso8859}  
  end

case encoding do
  :iso8859 -> :iconv.convert("iso8859-1", "utf8", title)
  encoding -> :unicode.characters_to_binary(title, encoding, :utf8)
end
kip

kip

ex_cldr Core Team

Welcome to the Elixir community!

If we look at what the encoding of the expected title is we can see its probably UTF16-LE since its a true subset of the frame payload:

iex> :iconv.convert("utf8", "utf16le", title)
<<76, 0, 97, 0, 32, 0, 81, 0, 117, 0, 234, 0, 116, 0, 101, 0>>

So now we know what we are looking for, we can examine the actual frame content, which appears to be:

iex> <<header::binary-size(10), rest::binary>> = id3tag_bytes
iex> <<"ID3", major::binary-size(1), revision::binary-size(1), flags::binary-size(1), size::binary-size(4)>> = header
iex> <<frame_overview::binary-size(10), frame::binary>> = rest
iex> <<frame_id::binary-size(4), frame_payload_size::unsigned-integer-32, frame_flags::binary-size(2)>> = frame_overview
iex> frame_payload = <<frame::binary-size(frame_payload_size)>>
iex> <<encoding::unsigned-integer-8, payload::binary>> = frame_payload
  
iex> IO.inspect(frame_payload_size, label: "Frame Payload Size")
Frame Payload Size: 19
iex> IO.inspect encoding, label: "Payload encoding (0 == ISO8859, 1 == UTF16)"
Payload encoding (0 == ISO8859, 1 == UTF16): 1
iex> IO.inspect payload, label: "Frame payload"
Frame payload: <<255, 254, 76, 0, 97, 0, 32, 0, 81, 0, 117, 0, 234, 0, 116, 0, 101, 0>>

Observations of the frame payload

  1. <<255, 254>> is the BOM indicating UTF16 little endian.
  2. The rest of the data is the actual string which we can decode with the following. Note that we don’t include the BOM, but we do use it to determine the endianness for conversion:
iex> title = <<76, 0, 97, 0, 32, 0, 81, 0, 117, 0, 234, 0, 116, 0, 101, 0>>
iex> :unicode.characters_to_binary(title, {:utf16, :little}, :utf8)
"La Quête"

Note that :unicode.characters_to_binary/3 does not detect endianness from a BOM, so the BOM needs to be excluded. :iconv.convert/3 has the same consideration.

Possible next steps

  1. For ISO-8859-1 conversion you may need to include :iconv as a dependency.
kip

kip

ex_cldr Core Team

IO.read and File.read return different types. chardata() is not the same as binary()

IO.read/2
@spec read(device(), :eof | :line | non_neg_integer()) :: chardata() | nodata()

File.read/1
@spec read(Path.t()) :: {:ok, binary()} | {:error, posix()}

In your case, I think I would just be doing:

case File.read(path) do
  {:ok, binary} -> extract_tags(binary)
  error -> error
end

Also note that I edited the case clause for :iso8859 to correctly binary pattern match.

Where Next?

Popular in Questions Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement