ogg support
[id3fs.git] / lib / ID3FS / File / Ogg.pm
1 package ID3FS::File::Ogg;
2
3 use strict;
4 use warnings;
5 use Ogg::Vorbis::Header;
6
7 sub new
8 {
9     my $proto=shift;
10     my $class=ref($proto) || $proto;
11     my $self={};
12     bless($self,$class);
13
14     $self->{path}=shift;
15     $self->{ogg}=Ogg::Vorbis::Header->new($self->{path});
16     use Data::Dumper;
17     # load tags
18     print Dumper $self->{ogg}->comment_tags;
19     return $self;
20 }
21
22 sub get
23 {
24     my ($self, $tag, $verbose)=@_;
25     my @comments=$self->{ogg}->comment($tag);
26     if(@comments)
27     {
28         # take first comment with actual contents
29         while(my $comment=shift @comments)
30         {
31             if(defined($comment) &&
32                length($comment)  &&
33                $comment =~ /\S+/)
34             {
35                 $comment =~ s/\//-/g; # drop slashes
36                 return $comment;
37             }
38         }
39     }
40     warn("$self->{path}: no $tag defined in Ogg comments\n") if($verbose);
41     return undef;
42 }
43
44 sub artist    { shift->get("Artist", 1); }
45 sub album     { shift->get("Album", 1);  }
46 sub audiotype { return "ogg";            }
47 sub haspic    { return undef;            } # FIXME
48 sub v1genre   { return undef;            } # ID3 only
49
50 # We don't care if year is not set
51 sub year
52 {
53     my ($self)=@_;
54     my $date=shift->get("Date", 0);
55     return undef unless($date);
56     if($date =~/(\d\d\d\d)/)
57     {
58         $date=$1;
59     }
60     return $date;
61 }
62
63 sub tags
64 {
65     my $self=shift;
66     my @comments=$self->{ogg}->comment("Genre");
67     my $tags={};
68     if(@comments)
69     {
70         # filter for useful comments
71         @comments= grep { defined($_); } @comments;
72         @comments= grep { length($_); }  @comments;
73         @comments= grep { /\S+/; }       @comments;
74         for my $comment (@comments)
75         {
76             if($comment=~/([^\/]+)\/(.*)/)
77             {
78                 my $tagname=$1;
79                 my $tagval=$2;
80                 $tagval=~s/\//-/g;
81                 $tags->{$tagname}=$tagval;
82             }
83             else
84             {
85                 $tags->{$comment}=undef;
86             }
87         }
88     }
89     return $tags;
90 }
91
92 1;
93