Skip to content

Instantly share code, notes, and snippets.

@enakai00
Created December 27, 2013 13:20
Show Gist options
  • Save enakai00/8146855 to your computer and use it in GitHub Desktop.
Save enakai00/8146855 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# cdconv - Command line CD ripper and ogg-vorbis encoder
#
# 2008/09/27 ver1.1
#
use strict;
use CDDB_get qw( get_cddb get_discids );
my $DEBUG = 0;
my $TMPFILE = "/tmp/cddbtmp.$$";
my $Basedir = "/data/music";
my ( $Album, $Artist, @Songs );
sub syscmd {
$_ = shift;
if ( $DEBUG ) {
print $_ . "\n";
return;
}
system ( $_ );
}
sub fconv {
my ( $s );
my %rules =
( '\'' => '’', '"' => '”', '\\\\' => '¥', '\/' => '/', ':' => ':',
'\*' => '*', '\?' => '?', '<' => '<', '>' => '>', '\|' => '|',
'〜' => '~' );
$_ = shift;
foreach $s ( keys( %rules ) ) {
$_ =~ s/$s/$rules{ $s }/g;
}
return $_;
}
sub fpath {
my ( $fname, $fpath );
$_ = shift;
$fname = sprintf( "%02d - %s.ogg", $_ + 1, $Songs[ $_ ] );
$fpath = "$Basedir/" . fconv( $Artist ) . " - " . fconv( $Album )
. "/" . fconv( $fname );
return $fpath;
}
sub get_cdinfo {
my ( $s, $j, $n, $i );
my ( $artist, $title, $cat, $cddbid, $song );
my ( @c_artist, @c_title, @c_songs );
my %config = (
"CDDB_HOST" => "freedb.freedb.org", "CDDB_PORT" => 8880,
"HEELO_ID" => "root nowhere.com null 1.0", "CD_DEVICE" => "/dev/cdrom",
"input" => 0, multi => 1
);
@_ = get_cddb( \%config );
$n = 0;
foreach $s ( @_ ) {
( $artist ,$title, $cat, $cddbid )
= ( $s->{"artist"}, $s->{"title"}, $s->{"cat"}, $s->{"id"} );
system ( "wget -O $TMPFILE.$n http://www.freedb2.org/freedb/$cat/$cddbid 1>/dev/null 2>&1" );
$n++;
}
for ( $i = 0; $i < $n; $i++ ) {
$title = `grep "^DTITLE" $TMPFILE.$i`; chomp $title;
$title =~ m/^DTITLE=(.*) \/ (.*)$/;
( $c_artist[ $i ], $c_title[ $i ] ) = ( $1, $2 );
$song = `grep "^TTITLE" $TMPFILE.$i`;
$song =~ s/^TTITLE\d+=//g; $song =~ s/\nTTITLE\d+=/\n/g; chomp $song;
print "Entry $i : $c_artist[ $i ] - $c_title[ $i ]\n";
$j = 0;
@_ = split ( "\n", $song );
while ( $_ = shift ) {
$c_songs[ $i ][ $j ] = $_;
printf ( "%02d - %s\n", $j + 1, $_ );
$j++;
}
print "\n";
unlink ( "$TMPFILE.$i" );
}
print ( "which? (q:quit)" );
$s = <>; chomp $s;
exit ( 1 ) if ( $s eq "q" );
( $Artist, $Album ) = ( $c_artist[ $s ], $c_title[ $s ] );
@Songs = @{$c_songs[ $s ]};
}
sub show_cdinfo {
my ( $s, $n );
while ( 1 ) {
print "\nEncode into the following files.\n";
foreach $_ ( 0 ... $#Songs ) {
print fpath( $_ ) . "\n";
}
print "\nAre you sure? (y/n/q/a/t/s#)";
$s = <>; chomp $s;
return 1 if ( $s eq "y" );
return 0 if ( $s eq "n" );
exit ( 1 ) if ( $s eq "q" );
if ( $s =~ m/s(\d+)/ ) {
$n = $1;
print "New song title for $n? ";
$s = <>; chomp $s; $Songs[ $n - 1 ] = $s;
next;
}
if ( $s eq "t" ) {
print "New album title? ";
$s = <>; chomp $s; $Album = $s;
next;
}
if ( $s eq "a" ) {
print "New artist? ";
$s = <>; chomp $s; $Artist = $s;
next;
}
}
}
sub do_encode {
my ( $i, $j, $file );
foreach $i ( 0 ... $#Songs ) {
$file = fpath( $i );
$j = $i + 1;
syscmd ( "cdparanoia -q -- \"$j\" - | oggenc -q 5 -o \"$file\" -" );
$j = sprintf( "%02d", $j );
syscmd ( "vorbiscomment -R -w -t \"ARTIST=$Artist\" -t \"TITLE=$Songs[ $i ]\" -t \"TRACKNUMBER=$j\" -t \"ALBUM=$Album\" \"$file\"" );
}
print "Done\n";
}
MAIN: {
while ( 1 ) {
print "Looking up CDDB...\n";
get_cdinfo();
last if ( show_cdinfo() );
} do_encode();
}
# vi:ts=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment