Skip to content

Instantly share code, notes, and snippets.

@asakasinsky
Forked from hauntedhost/ids_parser.exs
Created December 30, 2022 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asakasinsky/46603463128f35348b80b8cf90efd057 to your computer and use it in GitHub Desktop.
Save asakasinsky/46603463128f35348b80b8cf90efd057 to your computer and use it in GitHub Desktop.
Elixir mp3 id3 parser
defmodule Id3Parser do
@id3_tag_size 128
def parse(file_name) do
case File.read(file_name) do
{:ok, mp3} ->
# mp3 size minus 128 bytes where id3 tag is located
mp3_byte_size = byte_size(mp3) - @id3_tag_size
# pattern match mp3 binary to capture id3_tag
<< _ :: binary-size(mp3_byte_size), id3_tag :: binary >> = mp3
# pattern match components of id3_tag
<< "TAG",
raw_title :: binary-size(30),
raw_artist :: binary-size(30),
raw_album :: binary-size(30),
raw_year :: binary-size(30),
_ :: binary >> = id3_tag
# trim unprintable characters
[title, artist, album, year] = trim([raw_title, raw_artist, raw_album, raw_year])
IO.puts "#{artist} - #{title} (#{album}, #{year})"
_ ->
IO.puts "Couldn't open #{file_name}"
end
end
defp trim([string | rest]) do
[trim(string) | trim(rest) ]
end
defp trim([]), do: []
defp trim(string) do
String.split(string, <<0>>) |> List.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment