Last active
August 29, 2015 14:02
-
-
Save benjamintanweihao/c9bafe91e53a7bb0576c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule ID3Parser do | |
| def parse(file_name) do | |
| case File.read(file_name) do | |
| {:ok, binary} -> | |
| # ID3 tag information is located at the last 128 bytes. | |
| mp3_byte_size = (byte_size(binary) - 128) | |
| << _ :: [size(mp3_byte_size), binary], id3_tag :: binary >> = binary | |
| << "TAG", tags :: binary >> = id3_tag | |
| << title :: [size(30), binary], | |
| artist :: [size(30), binary], | |
| album :: [size(30), binary], | |
| year :: [size(4), binary], | |
| comment :: [size(30), binary], | |
| _rest :: binary >> = tags | |
| IO.puts title # I Was Made For Loving You | |
| IO.puts artist # KISS | |
| IO.puts year # 1979 | |
| IO.puts album # Dynasty | |
| IO.puts comment # Best wedding entrance song! | |
| _ -> | |
| IO.puts "Couldn't open #{file_name}" | |
| end | |
| end | |
| end | |
| ID3Parser.parse("sample.mp3") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment