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");
21 use Getopt::Long qw(:config gnu_getopt);
24 use lib "$FindBin::Bin";
29 # Submit a command to the MPD server; each argument to this function
30 # is quoted and sent as a single argument to MPD.
32 my $cmd = join(' ', map { MPD::escape } @_);
37 # Returns a hash reference containing all tracks in the current play queue.
38 # The hash keys are filenames.
39 sub get_tracks_in_play_queue {
43 mpd_exec("playlistinfo");
48 if (/^(\w+): (.*)$/) {
50 if (exists($matches{$2})) {
51 $entry = $matches{$2};
54 $matches{$2} = $entry;
58 if (exists($entry->{$1})) {
59 $entry->{$1}->{$2} = 1;
61 $entry->{$1} = { $2 => 1 }
69 # Given an MPD playlist name, returns a reference to an array containing
70 # (in order) the files in the playlist.
71 sub get_playlist_files {
75 mpd_exec("listplaylist", $plname);
80 if (/^(\w+): (.*)$/) {
93 Copyright © 2019 Nick Bowler
94 License GPLv3+: GNU General Public License version 3 or any later version.
95 This is free software: you are free to change and redistribute it.
96 There is NO WARRANTY, to the extent permitted by law.
101 my $fh = $_[1] // *STDERR;
103 print $fh "Usage: $0 [options] playlist\n";
104 print "Try $0 --help for more information.\n" unless (@_ > 0);
108 print_usage(*STDOUT);
110 This is "mpdreload": a tool to reload a stored playlist into the MPD queue.
113 -h, --host=HOST Connect to the MPD server on HOST, overriding defaults.
114 -p, --port=PORT Connect to the MPD server on PORT, overriding defaults.
115 -V, --version Print a version message and then exit.
116 -H, --help Print this message and then exit.
118 Report bugs to <nbowler\@draconx.ca>
123 'host|h=s' => \$MPD::host,
124 'port|p=s' => \$MPD::port,
126 'V|version' => sub { print_version(); exit },
127 'H|help' => sub { print_help(); exit },
128 ) or do { print_usage(); exit 1};
131 print STDERR "Playlist name is required\n" unless @ARGV;
132 print STDERR "Excess command-line arguments\n" if @ARGV;
134 print_usage(); exit 1
137 $sock = MPD::connect();
139 # Retrieve the current play queue and target play queue.
140 my $current = get_tracks_in_play_queue();
141 my $target = get_playlist_files($ARGV[0]);
143 mpd_exec("command_list_begin");
144 for (my $i = 0; $i < @$target; $i++) {
145 my $f = $target->[$i];
146 my $ids = $current->{$f}->{Id};
148 my $id = (keys %$ids)[0];
151 # Remove tracks with no unused queue IDs
152 delete $current->{$f} unless (keys %$ids > 0);
155 mpd_exec("moveid", $id, $i);
157 mpd_exec("addid", $f, $i);
161 # Remove any tracks left from the old play queue.
162 foreach (keys %$current) {
163 my $ids = $current->{$_}->{Id};
164 foreach (keys %$ids) {
165 mpd_exec("deleteid", $_);
169 mpd_exec("command_list_end");