| 1 | package URI::Title::MP3; | ||
| 2 | $URI::Title::MP3::VERSION = '1.902'; | ||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use MP3::Info; | ||
| 7 | use File::Temp qw(tempfile); | ||
| 8 | |||
| 9 | sub types {( | ||
| 10 | 'audio/mp3', | ||
| 11 | )} | ||
| 12 | |||
| 13 | sub _get_tag { | ||
| 14 | my $data = shift; | ||
| 15 | my (undef, $temp) = tempfile(); | ||
| 16 | open FILE, ">$temp" or die $!; | ||
| 17 | print FILE $data; | ||
| 18 | close FILE; | ||
| 19 | my $tag = get_mp3tag($temp); | ||
| 20 | if ($tag) { | ||
| 21 | my $info = get_mp3info($temp); | ||
| 22 | $tag->{info} = $info; | ||
| 23 | } | ||
| 24 | unlink($temp); | ||
| 25 | return $tag; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub title { | ||
| 29 | my ($class, $url, $data, $type) = @_; | ||
| 30 | my $tag; | ||
| 31 | if (-f $url) { | ||
| 32 | $tag = get_mp3tag($url); | ||
| 33 | if ($tag) { | ||
| 34 | my $info = get_mp3info($url); | ||
| 35 | $tag->{info} = $info; | ||
| 36 | } | ||
| 37 | |||
| 38 | } else { | ||
| 39 | $tag = _get_tag( $data . URI::Title::_get_end($url) ); | ||
| 40 | } | ||
| 41 | return unless $tag; | ||
| 42 | return unless ($tag->{ARTIST} or $tag->{TITLE}); | ||
| 43 | |||
| 44 | $tag->{ARTIST} ||= "Unknown Artist"; | ||
| 45 | $tag->{TITLE} ||= "Unknown Title"; | ||
| 46 | my $title = "$tag->{ARTIST} - $tag->{TITLE}"; | ||
| 47 | |||
| 48 | if (my $total = $tag->{info}{SECS} and -f $url) { | ||
| 49 | my $m = $total / 60; | ||
| 50 | my $s = $total % 60; | ||
| 51 | $title .= sprintf(" (%d:%02d)", $m, $s); | ||
| 52 | } | ||
| 53 | |||
| 54 | return $title; | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | __END__ |