-
Notifications
You must be signed in to change notification settings - Fork 2
/
mp3_reencode.pl
executable file
·125 lines (106 loc) · 4.49 KB
/
mp3_reencode.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl -w
# script to reencode MP3's at a different bitrate
# TODO
# fix the variables to all be delcared before use
# add a use strict directive
# change all the prints to warns
# add an output path option, but use the default $outdir if one is not passed
# in
# oneliner for reencoding FLAC files
# for FILE in *.flac; do newname=`ls ${FILE} | sed 's/flac$/mp3/'`;
# /sw/bin/flac -d -c $FILE | ~/Documents/bin/lame -h -S -b 256 - $newname; done
# for FILE in *.flac; do newname=$(ls ${FILE} | sed 's/flac$/wav/'); flac -d -c
# $FILE > ~/out/Shows/ArtistName/$newname; done
# 24 bit flac files? sure!!!
# for FILE in *.flac; do newname=`ls ${FILE} | sed 's/flac$/wav/'`;
# ecasound -i $FILE -o ${newname}; done
# metaflac --set-tag="TITLE=Long Slow Goodbye (Acoustic)"
# --set-tag="ARTIST=Queens of the Stone Age" --set-tag="DATE=19May2005"
# --set-tag="DESCRIPTION=Trimmed from the complete intervew file. Recorded
# using a Sangean ATS505 shortwave receiver connected to an Apple 12\"
# Powerbook. Editing performed in GarageBand, encoded using the OpenDarwin
# flac port" --set-tag="LOCATION=San Diego, California, United States"
# --set-tag="ORGANIZATION=Courtesy of Spicyjack's Secret Stash"
# QOTSA-Long_Slow_Goodbye-Acoustic-San_Diego-18May2005.flac
# metaflac --show-md5sum QOTSA-Interview-KBZT_San_Diego-19Mar2005.flac
use Getopt::Std; # parsing command line switches
my %opts; # hash for command line options
&getopts("f:l:s:w", \%opts); # go get the command line options
# set the output directory, no trailng slash please!
$outdir = "/home/brian/out";
# make sure we were passed a path to search for MP3's
if ( ! ($opts{f} || $opts{l} || $opts{s}) ) {
print "mp3_reencode.pl (c) 2001 Brian Manning\n";
print "Usage:\n";
print "mp3_reencode.pl [-f path] [-l filename]\n";
print "-s: re-encode a single file, use the full path to the file\n";
print "-f: system(find) *.mp3 on a path and reencode\n";
print "-l: reencode each file listed in filename\n";
print "-w: output to a .wav file (for burning audio CD's)\n";
print "Output will be sent to $outdir, with each album being\n";
print "placed in it's own separate folder.\n"; exit 1;
}
# here's where we get the list of files to reencode
if ( exists $opts{f} ) {
# call system(find $path) to get a list of MP3's
print "Executing system(find)...\n";
@filelist = `find \"$opts{f}\" -name \"*.mp3\" -print`;
} elsif ( exists $opts{s} ) {
# single file, so just give filelist that file only
$filelist[0] = $opts{s};
} elsif ( exists $opts{l} ) {
# or open the passed in list of files
open (MP3LIST, "$opts{l}");
@filelist = <MP3LIST>;
close (MP3LIST);
} else {
# we didn't get any valid options. exit
# we shouldn't ever reach this die statement
die("Please use the -f, -s, or -l switches when running this script");
}# if $ARGV[0]
$total_files = 1; # set a line counter
$start_time = time; # set the overall start time
# read in each line of the 'find', then run it against the database
foreach $file (@filelist) {
chomp($file);
@parts = split('/', $file);
$maxparts = @parts;
$artist = $parts[$maxparts - 3];
$album = $parts[$maxparts - 2];
$song = $parts[$maxparts - 1];
print "\n============================\n";
print "reencoding song #$total_files\n";
# make the artist directory if it does not exist already
if ( ! -d "$outdir/$artist" ) { # artist directory not there
mkdir("$outdir/$artist",0777) ||
die "cannot mkdir $outdir/$artist: $!\n";
} # if ! -d $artist
# make the album directory if needed
if ( ! -d "$outdir/$artist/$album" ) { # album directory not there
mkdir("$outdir/$artist/$album",0777) ||
die "cannot mkdir $outdir/$artist/$album: $!\n";
} # if ! -d $artist/$album
# now reencode the file
$song_time = time;
if ( ! $opts{w} ) {
# re-encode at a lower bitrate
$command = "/usr/bin/lame -h -S -b 128 ";
} else {
# output to a wav file
$song =~ s/mp3$/wav/i;
$command = "/usr/bin/lame --decode ";
} # if ( ! $opts{w} )
# the extra part of the command string that gives the file to re-encode
$command .= "\"$file\" \"$outdir/$artist/$album/$song\"";
$encode_time = time - $song_time;
system($command);
print "reencoded $artist/$album/$song in $encode_time secs\n";
# update total input lines parsed
$total_files++;
} # foreach $file (@filelist)
# tell'em how we did...
$total_time=time - $start_time;
$total_min = $total_time / 60;
print "Reencoded $total_files MP3's ";
print "in $total_time seconds, or $total_min minutes.\n";
exit 0;