3 # Copyright © 2019-2020 Nick Bowler
5 # Replace the current MPD play queue with a saved playlist, by rearranging
6 # existing queue entries when possible. This avoids losing the current
7 # player state when the loaded playlist is similar to the current queue.
9 # License GPLv3+: GNU General Public License version 3 or any later version.
10 # This is free software: you are free to change and redistribute it.
11 # There is NO WARRANTY, to the extent permitted by law.
16 use Encode::Locale qw(decode_argv);
17 decode_argv(Encode::FB_CROAK);
19 binmode(STDOUT, ":utf8");
20 use IO::Socket::INET6;
23 use Getopt::Long qw(:config gnu_getopt);
26 use lib "$FindBin::Bin";
29 my $host = $ENV{MPD_HOST} // "localhost";
30 my $port = $ENV{MPD_PORT} // 6600;
33 # Submit a command to the MPD server; each argument to this function
34 # is quoted and sent as a single argument to MPD.
36 my $cmd = join(' ', map { MPD::escape } @_);
41 # Returns a hash reference containing all tracks in the current play queue.
42 # The hash keys are filenames.
43 sub get_tracks_in_play_queue {
47 mpd_exec("playlistinfo");
52 if (/^(\w+): (.*)$/) {
54 if (exists($matches{$2})) {
55 $entry = $matches{$2};
58 $matches{$2} = $entry;
62 if (exists($entry->{$1})) {
63 $entry->{$1}->{$2} = 1;
65 $entry->{$1} = { $2 => 1 }
73 # Given an MPD playlist name, returns a reference to an array containing
74 # (in order) the files in the playlist.
75 sub get_playlist_files {
79 mpd_exec("listplaylist", $plname);
84 if (/^(\w+): (.*)$/) {
97 Copyright © 2019 Nick Bowler
98 License GPLv3+: GNU General Public License version 3 or any later version.
99 This is free software: you are free to change and redistribute it.
100 There is NO WARRANTY, to the extent permitted by law.
105 my $fh = $_[1] // *STDERR;
107 print $fh "Usage: $0 [options] playlist\n";
108 print "Try $0 --help for more information.\n" unless (@_ > 0);
112 print_usage(*STDOUT);
114 This is "mpdreload": a tool to reload a stored playlist into the MPD queue.
117 -h, --host=HOST Connect to the MPD server on HOST, overriding defaults.
118 -p, --port=PORT Connect to the MPD server on PORT, overriding defaults.
119 -V, --version Print a version message and then exit.
120 -H, --help Print this message and then exit.
122 Report bugs to <nbowler\@draconx.ca>
127 'host|h=s' => \$host,
128 'port|p=s' => \$port,
130 'V|version' => sub { print_version(); exit },
131 'H|help' => sub { print_help(); exit },
132 ) or do { print_usage(); exit 1};
135 print STDERR "Playlist name is required\n" unless @ARGV;
136 print STDERR "Excess command-line arguments\n" if @ARGV;
138 print_usage(); exit 1
142 if ($host =~ /^[@\/]/) {
144 $sock = new IO::Socket::UNIX(Type => SOCK_STREAM(), Peer => $host);
146 $sock = new IO::Socket::INET6(PeerAddr => $host,
150 $sock or die "failed to connect to MPD: $!";
151 binmode($sock, ":utf8");
153 if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
154 die "MPD failed to announce version: $!";
157 # Retrieve the current play queue and target play queue.
158 my $current = get_tracks_in_play_queue();
159 my $target = get_playlist_files($ARGV[0]);
161 mpd_exec("command_list_begin");
162 for (my $i = 0; $i < @$target; $i++) {
163 my $f = $target->[$i];
164 my $ids = $current->{$f}->{Id};
166 my $id = (keys %$ids)[0];
169 # Remove tracks with no unused queue IDs
170 delete $current->{$f} unless (keys %$ids > 0);
173 mpd_exec("moveid", $id, $i);
175 mpd_exec("addid", $f, $i);
179 # Remove any tracks left from the old play queue.
180 foreach (keys %$current) {
181 my $ids = $current->{$_}->{Id};
182 foreach (keys %$ids) {
183 mpd_exec("deleteid", $_);
187 mpd_exec("command_list_end");