This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
banshee-playlist.pl
237 lines (181 loc) · 5.67 KB
/
banshee-playlist.pl
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
#! /usr/bin/env perl
# create m3u file for banshee playlist
use warnings;
use strict;
use DBI;
use File::Basename;
use Getopt::Long;
use IO::File;
use Pod::Usage;
use URI::Escape;
my $pkg = 'banshee-playlist';
my $version = '0.2';
my $db = "$ENV{HOME}/.config/banshee-1/banshee.db";
my ($export, $list, $quiet);
unless (GetOptions(help => sub { &pod2usage(-exitval => 0) },
'db=s' => \$db,
export => \$export,
list => \$list,
quiet => \$quiet,
version => sub { print "$pkg $version\n"; exit(0) }
))
{
warn("Try `$pkg --help' for more information.\n");
exit(1);
}
# connect to database
die("$pkg: banshee database does not exist or is not readable")
unless (-f $db && -r $db);
my $dbh = DBI->connect("dbi:SQLite:dbname=$db", '', '',
{ RaiseError => 1, AutoCommit => 0 });
# see what user wants
if ($list) {
&list_playlists($dbh);
}
else {
# get playlist
die("$pkg: incorrect number of arguments: @ARGV") unless @ARGV == 1;
my ($playlist) = @ARGV;
&get_list($dbh, $playlist, $export);
}
$dbh->disconnect;
exit(0);
# list the playlists
sub list_playlists {
my ($dbh) = @_;
my $list_q = q(select PlaylistID, Name from CorePlaylists);
my $list_s = $dbh->prepare($list_q);
$list_s->execute();
while (my $row = $list_s->fetchrow_arrayref) {
my ($id, $name) = @$row;
print("$name\n");
}
return;
}
sub get_list {
my ($dbh, $playlist, $export) = @_;
# prep query
my $track_q = q(
select t.Uri, e.ViewOrder, a.Name, t.Title, t.TrackNumber,
l.Title, t.Year, t.Genre, t.Duration/1000
from CorePlaylists as p
join CorePlaylistEntries as e on e.PlaylistID = p.PlaylistID
join CoreTracks as t on t.TrackID = e.TrackID
join CoreArtists as a on a.ArtistID = t.ArtistID
join CoreAlbums as l on l.AlbumID = t.AlbumID
where p.Name = ?
order by e.ViewOrder, e.EntryID
);
my $track_s = $dbh->prepare($track_q);
# execute query
$track_s->execute($playlist);
# open m3u file
my $m3u = IO::File->new(">$playlist.m3u");
die "$pkg: failed to open m3u file: $!" unless defined($m3u);
$m3u->print("#EXTM3U\n");
# get tracks and start loop
while (my $row = $track_s->fetchrow_arrayref) {
my (%track);
@track{qw(uri order artist title n album year genre duration)} = @$row;
my $path = $track{uri};
$path =~ s,^file://,,;
$path = uri_unescape($path);
if (! -f $path) {
warn("$pkg: file does not exist: '$path'\n");
}
if ($export) {
my $rv = &export_file($path, %track);
if (!$rv) {
warn("$pkg: failed to export file: $path");
}
else {
$path = $rv;
}
}
$m3u->print("#EXTINF:$track{duration},$track{artist} - $track{title}\n");
$m3u->print("$path\n");
}
$m3u->close;
}
# export music files
sub export_file {
my ($path, %track) = @_;
# parse file path
my @exts = qw(.mp3 .ogg .flac .m4a);
my ($base, $dir, $suffix) = fileparse($path, @exts);
if (!$suffix) {
warn("$pkg: unknown suffix: $path");
return;
}
my $wav = "$base.wav";
my $mp3 = "$base.mp3";
print("$pkg: export $path to $mp3\n") unless $quiet;
# see if our work is already done
if (-f $mp3) {
warn("$pkg: file already exists: $mp3");
return $mp3;
}
# create wav
my @wav_cmd = (qw(mplayer -really-quiet -nolirc -vo null -vc dummy -ao),
"pcm:waveheader:file=$wav", $path);
if (system(@wav_cmd) != 0) {
warn("$pkg: failed to convert audio file to wav: $path");
return;
}
# create mp3
my @lame_cmd = (qw(lame --quiet --preset medium), '--ta', $track{artist},
'--tl', $track{album}, '--ty', $track{year},
'--tn', $track{n}, '--tg', $track{genre},
'--tt', $track{title}, $wav, $mp3);
if (system(@lame_cmd) != 0) {
warn("$pkg: failed to convert wav to mp3: $path");
unlink($wav);
return;
}
unlink($wav);
return $mp3;
}
__END__
=pod
=head1 NAME
banshee-playlist - list contents of banshee playlists
=head1 SYNOPSIS
B<banshee-playlist> [OPTIONS]... [PLAYLIST]
=head1 DESCRIPTION
This programs reads a Banshee database and outputs playlist
information. It can create an m3u file for a PLAYLIST (the default),
list the available playlists, and optionally export the song music
files from a playlist.
=head1 OPTIONS
If an argument to a long option is mandatory, it is also mandatory for
the corresponding short option; the same is true for optional arguments.
=over 4
=item --db=PATH
Use PATH for banshee database rather than the default,
C<~/.config/banshee-1/banshee.db>.
=item --export
Write the m3u file and export the music files as LAME-encoded medium
quality MP3 files in the current directory.
=item --help
Display a brief description and listing of all available options.
=item --list
List the banshee playlists.
=item --version
Output version information and exit.
=item --
Terminate option processing. This option is useful when file names
begin with a dash (-).
=back
=head1 EXAMPLES
To get a list of the playlists in banshee, run
$ banshee-playlist --list
To write an m3u file for playlist Workout and export the song files to
the current directory, run
$ banshee-playlist --export Workout
=head1 BUGS
Please email the author if you identify a bug.
=head1 SEE ALSO
sqlite3(1), banshee(1)
=head1 AUTHOR
David Dooling <banjo@users.sourceforge.net>
=cut