summarylogtreecommitdiffstats
path: root/extm3u.patch
blob: 1741cd1c54958f369a4349501d88c74a523c8efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
--- a/extm3u.pl	2024-02-13 14:52:35.000000000 +0200
+++ b/extm3u.pl	2024-02-13 14:54:00.348720670 +0200
@@ -11,8 +11,8 @@
     $LIST_MP3 = 1;
 }
 
-if(check_install(module => 'Ogg::Vorbis::Header') ){
-    Module::Load::load('Ogg::Vorbis::Header');
+if(check_install(module => 'Ogg::Vorbis::Header::PurePerl') ){
+    Module::Load::load('Ogg::Vorbis::Header::PurePerl');
     $LIST_OGG = 1;
 }
 
@@ -21,19 +21,25 @@
     $LIST_FLAC = 1;
 }
 
+if(check_install(module => 'MP4::Info') ){
+    Module::Load::load('MP4::Info');
+    $LIST_MP4 = 1;
+}
+
 # Function prototypes:
-sub printFiles($$$$);
-sub readFiles($$);
-sub getMP3($);
-sub getOGG($);
-sub getFLAC($);
+sub printFiles($$$$$);
+sub readFiles($$$);
+sub getMP3($$);
+sub getMP4($$);
+sub getOGG($$);
+sub getFLAC($$);
 sub help();
 
 
 ############################################################################
 # Main
 
-getopts('r',\%OPT);
+getopts('ra',\%OPT);
 
 if (@ARGV == 0){
   help();
@@ -46,7 +52,7 @@
     $mp3root =~ s/\/$//; #remove trailing slash
 
 # gather or print the files
-    push(@all, readFiles($mp3root,$OPT{'r'}));
+    push(@all, readFiles($mp3root,$OPT{'r'},$OPT{'a'}));
 }
 
 # randomize output
@@ -62,14 +68,16 @@
 # prints a short Help text
 sub help() {
     my $mp3 = $LIST_MP3 ? 'found' : 'NOT found';
+    my $mp4 = $LIST_MP4 ? 'found' : 'NOT found';
     my $ogg = $LIST_OGG ? 'found' : 'NOT found';
     my $fla = $LIST_FLAC ? 'found' : 'NOT found';
 
 print STDERR <<STOP
 
-      Usage: extm3u.pl [-r] <music-dir[s]>
+      Usage: extm3u.pl [-r] [-a] <music-dir[s]>
 
       -r           Randomize playlist order (heavy memory use)
+      -a           Add album name into track name
       <music-dir>  Search this directory recursivly for audio files
 
       This tool generates a extended .m3u playlist from a given directory
@@ -84,12 +92,16 @@
       For running this script you'll need the following Perl modules:
 
       MP3::Info            for .mp3 files   ($mp3)
-      Ogg::Vorbis::Header  for .ogg files   ($ogg)
+      MP4::Info            for .mp4 files (.mp4, .m4a, .m4b, .m4p, .3gp)   ($mp4)
+      Ogg::Vorbis::Header::PurePerl  for .ogg files   ($ogg)
       Audio::FLAC::Header  for .flac files  ($fla)
 
       The scripts autodetects which modules are installed. If a module is
       not installed, the corresponding file type is ignored
 
+      ArchLinux users can install deps with one simple command:
+      paru -S perl-audio-flac-header perl-mp3-info perl-mp4-info perl-ogg-vorbis-header-pureperl
+
       _____________________________________________________________________
       extm3u.pl - Generates an extended .m3u mp3-Playlist
       Copyright (C) 2004-2010 Andreas Gohr <andi\@splitbrain.org>
@@ -106,66 +118,105 @@
 
 ###############################################################################
 # get a given FLAC
-sub getFLAC($){
+sub getFLAC($$){
     my $file = $_[0];
+    my $fetchalbum = $_[1];
     my $flac = new Audio::FLAC::Header($file);
 
     my $sec = int($flac->{trackTotalLengthSeconds});
     my $artist = $flac->tags('ARTIST');
+    my $album;
+    if($fetchalbum){
+        $album = $flac->tags('ALBUM');
+    }
     my $title = $flac->tags('TITLE');
 
-    return [$sec,$artist,$title];
+    return [$sec,$artist,$album,$title];
 }
 
 ###############################################################################
 # get a given MP3
-sub getMP3($){
+sub getMP3($$){
     my $file = $_[0];
+    my $fetchalbum = $_[1];
     my $info = MP3::Info::get_mp3info($file);
     my $tag  = MP3::Info::get_mp3tag($file);
 
     my $sec    = int($info->{SECS});
     my $artist = $tag->{ARTIST};
+    my $album;
+    if($fetchalbum){
+        $album  = $tag->{ALBUM};
+    }
     my $title  = $tag->{TITLE};
 
-    return [$sec,$artist,$title];
+    return [$sec,$artist,$album,$title];
+}
+
+###############################################################################
+# get a given MP4
+sub getMP4($$){
+    my $file = $_[0];
+    my $fetchalbum = $_[1];
+    my $info = MP4::Info::get_mp4info($file);
+    my $tag  = MP4::Info::get_mp4tag($file);
+
+    my $sec    = int($info->{SECS});
+    my $artist = $tag->{ARTIST};
+    my $album;
+    if($fetchalbum){
+        $album  = $tag->{ALBUM};
+    }
+    my $title  = $tag->{TITLE};
+
+    return [$sec,$artist,$album,$title];
 }
 
 ###############################################################################
 # get a given OGG
-sub getOGG($){
+sub getOGG($$){
     my $file = $_[0];
-    my $ogg  = new Ogg::Vorbis::Header($file);
+    my $fetchalbum = $_[1];
+    my $ogg  = new Ogg::Vorbis::Header::PurePerl($file);
     return if(!defined($ogg)); # this is no ogg
 
     my $sec = int($ogg->info->{'length'});
 
     # this strange construction is to fetch artist and title regardles
     # of tag case
-    my ($artist,$title);
+    my ($artist,$album,$title);
     my $tags = join("\n",$ogg->comment_tags());
     if($tags =~ m/^(artist)$/mi){
         $artist =  ($ogg->comment($1))[0];
     }
+    if($fetchalbum){
+        if($tags =~ m/^(album)$/mi){
+            $album =  ($ogg->comment($1))[0];
+        }
+    }
     if($tags =~ m/^(title)$/mi){
         $title =  ($ogg->comment($1))[0];
     }
 
-    return [$sec,$artist,$title];
+    return [$sec,$artist,$album,$title];
 }
 
 ##############################################################################
 # Print extended file info
-sub printFile($$$$){
+sub printFile($$$$$){
     $file   = $_[0];
     $sec    = $_[1];
-    $artist = $_[2];
-    $title  = $_[3];
-
-    $base = basename($file,['.mp3','.ogg','.fla','.flac','.wav']);
+    $artist = $_[2] =~ s/-/–/r;
+    $album  = $_[3] =~ s/-/–/r;
+    $title  = $_[4] =~ s/-/–/r;
+    $base = basename($file,['.mp3','.ogg','.fla','.flac','.wav','.mp4','.m4a','.m4b','.m4p','.3gp']);
 
     if ($artist ne '' || $title ne ''){
-        print ("#EXTINF:$sec,$artist - $title\n");
+        if ($album ne ''){
+            print ("#EXTINF:$sec,$artist - $album - $title\n");
+        }else{
+            print ("#EXTINF:$sec,$artist - $title\n");
+        }
     }else{
         print ("#EXTINF:$sec,$base\n");
     }
@@ -174,9 +225,10 @@
 
 ##############################################################################
 # Read a given directory and its subdirectories
-sub readFiles($$) {
+sub readFiles($$$) {
     my $path = $_[0];
     my $rand = $_[1];
+    my $alb = $_[2];
 
     my @files;
     if($path eq '-') {
@@ -194,18 +246,20 @@
         if (-d $file) {
             # recursion
             if($path ne '-') {
-               push(@allfiles,readFiles($file,$rand));
+               push(@allfiles,readFiles($file,$rand,$alb));
             }
         } else {
             # get audio file infos
             my $info = undef;
             if ($LIST_MP3 && $file =~ /\.mp3$/i) {
-                $info = getMP3($file);
+                $info = getMP3($file,$alb);
             } elsif ($LIST_FLAC && $file =~ /\.flac?$/i) {
-                $info = getFLAC($file);
+                $info = getFLAC($file,$alb);
             } elsif ($LIST_OGG && $file =~ /\.ogg$/i) {
-                $info = getOGG($file);
-            } elsif ($file =~ /\.(mp3|flac?|ogg|wav)$/i) { # audio file, but we dont understand it
+                $info = getOGG($file,$alb);
+            } elsif ($LIST_MP4 && $file =~ /\.(mp4|m4a|m4b|m4p|3gp)$/i) {
+                $info = getMP4($file,$alb)
+            } elsif ($file =~ /\.(mp3|flac?|ogg|wav|mp4|m4a|m4b|m4p|3gp)$/i) { # audio file, but we dont understand it
                 $info = [];
             }
 
@@ -214,7 +268,7 @@
                 if($rand){
                     push(@allfiles,[$file,$info]);
                 }else{
-                    printFile($file,$$info[0],$$info[1],$$info[2]);
+                    printFile($file,$$info[0],$$info[1],$$info[2],$$info[3]);
                 }
             }
         }