1#
2# -*- Perl -*-
3# $Id: mp3.pl,v 1.1.2.16 2008-05-09 08:37:44 opengl2772 Exp $
4# Copyright (C) 2002 Luc@2113.ch ,
5#               2003-2008 Namazu Project All rights reserved ,
6#     This is free software with ABSOLUTELY NO WARRANTY.
7#
8#  This program is free software; you can redistribute it and/or modify
9#  it under the terms of the GNU General Public License as published by
10#  the Free Software Foundation; either versions 2, or (at your option)
11#  any later version.
12#
13#  This program is distributed in the hope that it will be useful
14#  but WITHOUT ANY WARRANTY; without even the implied warranty of
15#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16#  GNU General Public License for more details.
17#
18#  You should have received a copy of the GNU General Public License
19#  along with this program; if not, write to the Free Software
20#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21#  02111-1307, USA
22#
23#  This file must be encoded in EUC-JP encoding
24#
25
26package mp3;
27use strict;
28
29require 'util.pl';
30require 'gfilter.pl';
31
32my $rpmpath = undef;
33
34sub mediatype() {
35    return ('audio/mpeg');
36}
37
38sub status() {
39    # http://sourceforge.net/projects/pudge/
40    if (util::checklib('MP3/Info.pm')) {
41        eval 'use MP3::Info 1.01;';
42        return 'yes' unless $@;
43    }
44    return 'no';
45}
46
47sub recursive() {
48    return 0;
49}
50
51sub pre_codeconv() {
52    return 0;
53}
54
55sub post_codeconv () {
56    return 0;
57}
58
59sub add_magic ($) {
60    my ($magic) = @_;
61    $magic->addMagicEntry("0\tstring\tID3\taudio/mpeg");
62    return;
63}
64
65sub filter($$$$$) {
66    my ($orig_cfile, $contref, $weighted_str, $headings, $fields)
67      = @_;
68    my $cfile = defined $orig_cfile ? $$orig_cfile : '';
69
70    my $id3v2header = substr($$contref, 0, 3);
71    unless ($id3v2header =~ /ID3/) {
72        my $id3v1header = substr($$contref, -128, 3);
73        unless ($id3v1header =~ /TAG/) {
74            util::vprint("Couldn't find ID3 tag\n");
75            $$contref = "";
76            return undef;
77        }
78    }
79
80    util::vprint("Processing mp3 file ... (using  MP3::Info module)\n");
81
82    my $mp3;
83
84    my $tmpfile  = util::tmpnam('NMZ.mp3');
85    {
86        my $fh = util::efopen("> $tmpfile");
87        print $fh $$contref;
88        util::fclose($fh);
89    }
90    $mp3 = $tmpfile;
91
92    if ($MP3::Info::VERSION >= 1.10) {
93        MP3::Info::use_mp3_utf8(0);
94    }
95
96    my $tagref = get_mp3tag($mp3);
97    if (ref $tagref->{TITLE}) {
98        $tagref = get_mp3tag($mp3, 2);
99    }
100
101    my $songname = defined $tagref->{TITLE} ? $tagref->{TITLE} : '';
102    my $artist = defined $tagref->{ARTIST} ? $tagref->{ARTIST} : '';
103    my $album = defined $tagref->{ALBUM} ? $tagref->{ALBUM} : '';
104    my $year = defined $tagref->{YEAR} ? $tagref->{YEAR} : '';
105    my $comment = defined $tagref->{COMMENT} ? $tagref->{COMMENT} : '';
106    my $genre = defined $tagref->{GENRE} ? $tagref->{GENRE} : '';
107    my $track = defined $tagref->{TRACKNUM} ? $tagref->{TRACKNUM} : '';
108
109    $songname =~ s/\0//g;
110    $artist =~ s/\0//g;
111    $album =~ s/\0//g;
112    $year =~ s/\0//g;
113    $comment =~ s/\0//g;
114    $genre =~ s/\0//g;
115    $track =~ s/\0//g;
116
117    my $data1 = "";
118    $$contref = "";
119
120    $data1 .= "Songname: $songname\n";
121    $data1 .= "Artist: $artist\n";
122    $data1 .= "Filename: " . gfilter::filename_to_title($cfile, $weighted_str);
123    $data1 .= "\n\n";
124    $data1 .= "Album: $album\n";
125    $data1 .= "Comment: $comment\n";
126    $data1 .= "Year: $year\n";
127    $data1 .= "Genre: $genre\n";
128    $data1 .= "Track: $track\n";
129
130    # codeconv::toeuc(\$data1);
131    codeconv::codeconv_document(\$data1);
132    $$contref = $data1;
133    mp3_filter($contref, $weighted_str, $fields, $headings);
134
135    $data1 =~ /\n\n(.+)/sg;
136    my $tmp = $1;
137    mp3::get_summary($tmp, $fields);
138
139    gfilter::line_adjust_filter($contref);
140    gfilter::line_adjust_filter($weighted_str);
141    gfilter::white_space_adjust_filter($contref);
142    gfilter::show_filter_debug_info($contref, $weighted_str,
143			   $fields, $headings);
144
145    unlink $tmpfile;
146    return undef;
147}
148
149sub mp3_filter($$$$) {
150    my ($contref, $weighted_str, $fields, $headings) = @_;
151
152    mp3::get_title($$contref, $weighted_str, $fields);
153    mp3::get_author($$contref, $fields);
154    mp3::get_album($$contref, $fields);
155    $$contref =~ s/^\w+:{1,1}?//gm;
156
157    return;
158}
159
160sub get_title($$$) {
161    my ($content, $weighted_str, $fields) = @_;
162
163    if ($content =~ /Songname: (.*)/) {
164	my $tmp = $1;
165	$fields->{'title'} = $tmp;
166    } else {
167        $content =~ /Filename: (.*)/;
168	my $tmp = $1;
169	$fields->{'title'} = $tmp;
170    }
171    my $weight = $conf::Weight{'html'}->{'title'};
172    $$weighted_str .= "\x7f$weight\x7f$fields->{'title'}\x7f/$weight\x7f\n";
173
174}
175
176sub get_author($$) {
177    my ($content, $fields) = @_;
178
179    if ($content =~ /Artist: (.*)/) {
180	my $tmp = $1;
181	$fields->{'author'} = $tmp;
182    }
183}
184
185sub get_album($$) {
186    my ($content, $fields) = @_;
187
188    if ($content =~ /Album: (.*)/) {
189	my $tmp = $1;
190	$fields->{'album'} = $tmp;
191    }
192}
193
194sub get_summary($$) {
195    my ($content, $fields) = @_;
196    $fields->{'summary'} = $content;
197}
198
1991;
200