Fandom

Music Player Daemon Community Wiki

Hack:Mpc-play

525pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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 --no-status stop",$DEBUG);
say_and_do("mpc --no-status 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(0.1);
}while($stat=~/^Updating DB/m);

# generating playlist
foreach $link(@links)
{
	$link=~s/\`/\\\`/g;
	say_and_do("mpc --no-status 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                                                                                               
import sys                                                                                              
                                                                                                        
MPD_ROOT = '/home/jan/snd'                                                                        
TMP_DIR = 'external'                                                                                    
                                                                                                        
if not len(sys.argv) > 1:                                                                               
    print("Usage:\n{0} <files>".format(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 = os.path.join(TMP_DIR, os.path.basename(file))                                            
    try:                                                                                                
        os.symlink(os.path.abspath(file), os.path.join(MPD_ROOT, linkname))                             
    except OSError:                                                                                     
        pass                                                                                            
    print("Adding " + linkname)                                                                         
    links.append(linkname)                                                                              
                                                                                                        
os.system('mpc update {0} >/dev/null'.format(TMP_DIR))                                                  
                                                                                                        
while still_updating():                                                                                 
    pass                                                                                                
                                                                                                        
for link in links:                                                                                      
    os.system('mpc add "{0}"'.format(link))

Also on Fandom

Random Wiki