diff --git a/lib/live_beats/mp3_stat.ex b/lib/live_beats/mp3_stat.ex index 2016fb4..8b7e39c 100644 --- a/lib/live_beats/mp3_stat.ex +++ b/lib/live_beats/mp3_stat.ex @@ -8,7 +8,7 @@ defmodule LiveBeats.MP3Stat do use Bitwise alias LiveBeats.MP3Stat - defstruct duration: 0, path: nil, title: nil, tags: nil + defstruct duration: 0, path: nil, title: nil, artist: nil, tags: nil @declared_frame_ids ~w(AENC APIC ASPI COMM COMR ENCR EQU2 ETCO GEOB GRID LINK MCDI MLLT OWNE PRIV PCNT POPM POSS RBUF RVA2 RVRB SEEK SIGN SYLT SYTC TALB TBPM TCOM TCON TCOP TDEN TDLY TDOR TDRC TDRL TDTG TENC TEXT TFLT TIPL TIT1 TIT2 TIT3 TKEY TLAN TLEN TMCL TMED TMOO TOAL TOFN TOLY TOPE TOWN TPE1 TPE2 TPE3 TPE4 TPOS TPRO TPUB TRCK TRSN TRSO TSOA TSOP TSOT TSRC TSSE TSST TXXX UFID USER USLT WCOM WCOP WOAF WOAR WOAS WORS WPAY WPUB WXXX) @@ -36,7 +36,11 @@ defmodule LiveBeats.MP3Stat do def parse(path) do {tag_info, rest} = parse_tag(File.read!(path)) duration = parse_frame(rest, 0, 0, 0) - {:ok, %MP3Stat{duration: round(duration), path: path, tags: tag_info, title: tag_info["TIT2"]}} + title = Enum.at(tag_info["TIT2"] || [], 0) + artist = Enum.at(tag_info["TPE1"] || [], 0) + + {:ok, + %MP3Stat{duration: round(duration), path: path, tags: tag_info, title: title, artist: artist}} rescue _ -> {:error, :bad_file} end diff --git a/test/live_beats/mp3_stat_test.exs b/test/live_beats/mp3_stat_test.exs new file mode 100644 index 0000000..382095f --- /dev/null +++ b/test/live_beats/mp3_stat_test.exs @@ -0,0 +1,12 @@ +defmodule LiveBeats.MP3StatTest do + use ExUnit.Case, async: true + + alias LiveBeats.MP3Stat + + test "parse/1 with valid mp3" do + {:ok, %MP3Stat{} = stat} = MP3Stat.parse("test/support/fixtures/silence1s.mp3") + assert stat.duration == 1 + assert stat.title == "Silence" + assert stat.artist == "Anon" + end +end diff --git a/test/support/fixtures/silence1s.mp3 b/test/support/fixtures/silence1s.mp3 index fed8dc6..2bb8c0d 100644 Binary files a/test/support/fixtures/silence1s.mp3 and b/test/support/fixtures/silence1s.mp3 differ