Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Hack:Mpc-play

From Music Player Daemon Community Wiki

Jump to: navigation, search

This simple perl-script allow you to use mpc as general-purpose player like xmms (i.e. to play any file, even if it is outside mpdroot directory).

Download: mirror 1 (version 0.1: outdated)

#!/usr/bin/perl

# mpc-play
# version 0.2
# by Ilya "Voyager" Schurov (http://comm.noo.ru/iv-en/)

$MUSIC_PREFIX="/home/user/mpd-root";
$TEMP_DIR="external";
$SCRIPT_NAME="mpc-play";
# warning! all symlinks in $MUSIC_PREFIX/$TEMP_DIR will be lost on start!

$DEBUG=0;


$PWD=`pwd`;
chomp $PWD;

if(!@ARGV)
{
	print "Usage:\n$SCRIPT_NAME <files-to-play>\n";
}

say_and_do("mpc stop",$DEBUG);
say_and_do("mpc clear",$DEBUG);

# removing symlinks from $TEMP_DIR

while(<$MUSIC_PREFIX/$TEMP_DIR/*>)
{
		unlink if(-l);
}


foreach $file (@ARGV)
{
	$link=$file;
		
	# stripping slashes from arguments
	$link=~s/\//_/g;
	if($file!~/^\//)
	{
		$file="$PWD/$file";
	}
		
	symlink("$file","$MUSIC_PREFIX/$TEMP_DIR/$link") 
               || die("Can't create symlink from $file to $MUSIC_PREFIX/$TEMP_DIR/$link: $!");
	push @links, $link;
}
say_and_do("mpc update $TEMP_DIR",$DEBUG);

# now we need to wait while mpd updating DB
do
{
	$stat=`mpc`;
	sleep(1);
}while($stat=~/^Updating DB/m);

# generating playlist
foreach $link(@links)
{
	$link=~s/\`/\\\`/g;
	say_and_do("mpc add \"$TEMP_DIR/$link\"",$DEBUG);
}

#let's the music begins! :)
say_and_do("mpc play",$DEBUG);

sub say_and_do
{
	my $str=shift;
	my $debug=shift;
	print "$str\n" if($debug);
	system($str);
}


Here's a little python script that does essentialy the same, just hacked up because I didn't like some small things about the perl version and I'm not comfortable writing perl. :-)

#!/usr/bin/env python
import os, sys

mpd_root = '/home/jan/snd'
tmp_dir = 'tmp'

if not len(sys.argv) > 1:
    print >>sys.stderr, 'Usage:\n%s <files>' % sys.argv[0]
    sys.exit(1)

def still_updating():
    return 'Updating DB' in os.popen('mpc').read()

links = []
for file in sys.argv[1:]:
    linkname = '%s/%s' % (tmp_dir, os.path.basename(file))
    os.symlink(file, '%s/%s' % (mpd_root, linkname))
    links.append(linkname)

os.system('mpc update %s >/dev/null' % tmp_dir)

while still_updating(): pass

for link in links:
    os.system('mpc add "%s"' % link)
Rate this article:
Share this article: