Created
September 17, 2010 03:35
-
-
Save anonymous/583624 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Add cover images to XMMS2 medialib entries from file. | |
use strict; | |
use Audio::XMMSClient; | |
sub fold(&@) { | |
my $f = shift; | |
local ($a,$b) = (shift,undef); | |
$a = $f->($a, $b) while defined ($b = shift); | |
return $a; | |
} | |
sub slurp($) { | |
local $/; | |
open(FILE, '<', shift) or return undef; | |
$_ = <FILE>; | |
close FILE or return undef; | |
return $_; | |
} | |
# validate arguments | |
my $img = shift@ARGV; | |
print STDERR "$0: Usage: $0 cover_image mid ...\n" | |
and exit 1 unless $#ARGV+1; | |
print STDERR "$0: $img: $!\n" | |
and exit 1 unless -r $img; | |
print STDERR "$0: Media ID must be decimal number.\n" | |
and exit 1 unless fold {$a&&$b} map {!!m/^[0-9]+$/} @ARGV; | |
# connect to xmms2 daemon | |
my $xc = Audio::XMMSClient->new('xmms2addcover'); | |
$xc->connect or die $xc->get_last_error; | |
# slurp file into memory | |
my $dat = slurp $img; | |
die $! unless $dat; | |
# determine its type | |
my $type = eval { | |
require File::MMagic; | |
File::MMagic->new()->checktype_contents($dat) | |
}; | |
# store bindata | |
my $hash = eval { $xc->bindata_add($dat)->wait->value }; | |
die $xc->get_last_error unless $hash; | |
# set properties | |
my @res = (); | |
foreach (@ARGV) { | |
my $mid = int $_; | |
push @res, $xc->medialib_entry_property_set_str( | |
$mid, 'picture_front', $hash); | |
if ($type) { | |
push @res, $xc->medialib_entry_property_set_str( | |
$mid, 'picture_front_mime', $type) | |
} | |
else { | |
push @res, $xc->medialib_entry_property_remove( | |
$mid, 'picture_front_mime'); | |
} | |
} | |
foreach (@res) { $_->wait } | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment