98ae5419a4fd2b739280c3ed7d6cdb3d

This is part of a larger project. It is designed as a PHP shell script that sucks up a folder of FLAC files and then invokes various external programs to re-encode the file as different file formats, giving the user progress reports. The major problems are in the two loops, which seem inefficient to me. In particular, in the latter loop, which does the donkey work, there is probably a better way to invoke the files. Right now, if the filename of the FLAC file is in any way non-standard (eg contains a . or a ') then the program bugs out. Some kind of error checking might be good too...how does, for example, die work in php and can I suppress the "directory already there" messages etc...? Thanks very much indeed.

Eventually this will be initialized by a cron script to allow for web access.

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

$flacpath = '/bin/flac';
$lamepath = '/bin/lame';
$oggpath = '/bin/oggenc';

ini_set('memory_limit', '18M');
system ('clear');
require_once('getid3/getid3.php');

if ($argv[1] == null){
	$dir = getcwd();
}
else {
	$dir = $argv[1];
}

$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}
sort ($files);

print "Ribcage Add Release v 1.0\n";
print "Looking through $dir for FLAC files...";
$getID3 = new getID3;

array ($tracks);

foreach ($files as $filename) {
	list ($name, $ext) = explode ('.', $filename);
	
	if ($ext == 'flac'){
		$file = $getID3->analyze("$dir/$filename");
		getid3_lib::CopyTagsToComments($file);
		print_r ($file);
		$tracks [] = array (
			'path' => addslashes("$dir/$filename"),
			'filename' => addslashes($filename),
			'filename_name' => addslashes($name),
			
			'artist'=> @$file['comments']['artist'][0],
			'title' => @$file['comments']['title'][0],
			'album' => @$file['comments']['album'][0],
			'year' => @$file['comments']['date'][0],
			
			'track' => @$file['comments']['tracknumber'][0],
			'total_tracks' => @$file['comments']['tracktotal'][0]
			);
	}
}

$no_of_tracks = count($tracks);

if ($no_of_tracks == 0){
	print "nope\n";
	print "I didn't find any FLAC files\n";
	exit ('1');
}
print "found ".$no_of_tracks." files\n";

$release = array (
		'title'=>$tracks[0]['album'],
		'artist'=>$tracks[0]['artist'],
		'year'=>$tracks[0]['year'],
		'total_tracks' => $tracks[0]['total_tracks']
	);


$storage = join ('/', array($dir, $release['artist']));

print "Making directory tree...";
mkdir ($storage);

$storage = join ('/', array($dir, $release['artist'], $release['title']));
mkdir ($storage);

mkdir (join ('/', array($storage, 'download')));
mkdir (join ('/', array($storage, 'stream')));
mkdir (join ('/', array($storage, 'download', 'flac')));
mkdir (join ('/', array($storage, 'download', 'mp3')));
mkdir (join ('/', array($storage, 'download', 'ogg')));
mkdir (join ('/', array($storage, 'zip')));

print "done\n\n";

print $release['artist'].' - '.$release['title']."\n";
print "====================================================\n\n";

$current_track = 1;

foreach ($tracks as $track) {		
	
	print "=== [".$current_track.'/'.$no_of_tracks.'] '.$track['title']."\n";
	print "Converting to wav file for processing...";
	system ("$flacpath -d -s '".$track['path']."'");
	print "done \n";
	
	$wav = join ('.',array($track['filename_name'], 'wav'));
	$mp3 = join ('.',array($track['filename_name'], 'mp3'));
	$ogg = join ('.',array($track['filename_name'], 'ogg'));

	$lametag= "--ta '".$release['artist']."' --tl '".$release['title']."' --ty '".$release['year']."' --tn '".$track['track']."' --tt '".$track['title']."'";
	
	print "Converting to high quality mp3...";
	system ("$lamepath $lametag --quiet -V 0 --vbr-new '".$dir."/".$wav."' '".join ('/', array($storage, 'download','mp3',$mp3))."'");
	print "done \n";
	
	print "Converting to streamable mp3...";
	system ("$lamepath $lametag --quiet --cbr -b 128 '".$dir."/".$wav."' '".join ('/', array($storage, 'stream',$mp3 ))."'");
	print "done\n";
	
	$oggtag= "-a '".$release['artist']."' -l '".$release['title']."' -d '".$release['year']."' -N '".$track['track']."' -t '".$track['title']."'";
	
	print "Converting to ogg file...";
	system ("$oggpath $oggtag -Q -q 5 -o '".join ('/', array($storage, 'download','ogg',$ogg ))."' '".$dir."/".$wav."'");
	print "done\n";
	
	print "Moving flac file to correct directory...";
	rename ($track['path'],join ('/', array($storage, 'download','flac',$track['filename'])));
	print "done\n";
	
	print "Cleaning up...";
	unlink ("$dir/$wav");
	print "done\n";
	
	print "Adding file to database...";
// Code eventually to add to a SQL database...
	print "done\n";
	
	$current_track++;
	print "\n";
	
}

print "Making zip release bundles...flac...";
system ("zip -9 -jqr '".$storage."/zip/".$release['artist']."_".$release['title']."_flac.zip' '".$storage."/download/flac'");
echo "mp3...";
system ("zip -9 -jqr '".$storage."/zip/".$release['artist']."_".$release['title']."_mp3.zip' '".$storage."/download/mp3'");
echo "ogg...";
system ("zip -9 -jqr '".$storage."/zip/".$release['artist']."_".$release['title']."_ogg.zip' '".$storage."/download/ogg'");
print "done \n";

print "All done!\n";
exit (0);

?>

Refactorings

No refactoring yet !

05238ed17efcbc354d5222fb9f8f0558

James

November 1, 2007, November 01, 2007 21:39, permalink

No rating. Login to rate!

Just offering changes for your problem with the file extension - take this line out:

list ($name, $ext) = explode ('.', $filename);

replace it with:

1
2
3
4
5
6
7
8
9
10
11
$namebits = explode( '.', $filename );
if( count( $namebits ) > 1 ) # it's got an extension
{

$ext = array_pop( $namebits ); #grab the last bit, will be the extension
$filename = count( $namebits > 1 ) ? implode( '.', $namebits ) : $namebits[0]; # put all the rest together as the filename
}
else # doesn't have an extension?
{

}
98ae5419a4fd2b739280c3ed7d6cdb3d

wegosublime

November 20, 2007, November 20, 2007 16:52, permalink

No rating. Login to rate!

Thanks for that. The primary problem is when I have a ' in the title of a song the whole thing craps out. It leaves me with this error message.

=== [12/13] The Little Things I've Lost
Converting to wav file for processing...sh: -c: line 1: unexpected EOF while looking for matching `''
sh: -c: line 2: syntax error: unexpected end of file
done
Converting to high quality mp3...sh: -c: line 1: syntax error near unexpected token `('
sh: -c: line 1: `/home/recordsonribs/bin/lame --ta 'We Show Up On Radar' --tl 'Growing Up A Girl (Instructions On How To Grow A Girl Without Your Wife Finding Out)' --ty '2007' --tn '13' --tt 'The Little Things I\'ve Lost' --quiet -V 0 --vbr-new './inbox/13 The Little Things I\'ve Lost.wav' './inbox/We Show Up On Radar/Growing Up A Girl (Instructions On How To Grow A Girl Without Your Wife Finding Out)/download/mp3/13 The Little Things I\'ve Lost.mp3''
done

F6ecf914dd8ae101aace8552d894ed1a

yaleman

November 28, 2007, November 28, 2007 22:14, permalink

2 ratings. Login to rate!

I think what you need is a whole lot more addslashes() in the code to handle those things (or just in the right places) I've got some other optimisations to add to your code (to reduce repetition and the like) - this isn't anywhere near finished but have a look...

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

$flacpath = '/bin/flac';
$lamepath = '/bin/lame';
$oggpath = '/bin/oggenc';

$filetypes = array( 'flac', 'mp3', 'ogg' );
global $filetypes;

ini_set('memory_limit', '18M');
system ('clear');
require_once('getid3/getid3.php');

$dir = ($argv[1] == null ) ? getcwd() : $argv[1];
#if ($argv[1] == null){ $dir = getcwd(); }
#else { $dir = $argv[1]; }

$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) 
	{
    $files[] = $filename;
	}
sort ($files);

print "Ribcage Add Release v 1.0\n";
print "Looking through $dir for FLAC files...";
$getID3 = new getID3;

array ($tracks);

foreach ($files as $filename) 
	{
	$namebits = explode( '.', $filename );
	if( count( $namebits ) > 1 ) # it's got an extension
		{
		$ext = array_pop( $namebits ); #grab the last bit, will be the extension
		$filename = count( $namebits > 1 ) ? implode( '.', $namebits ) : $namebits[0]; # put all the rest together as the filename
		}
	if ($ext == 'flac')
		{
		$file = $getID3->analyze("$dir/$filename");
		getid3_lib::CopyTagsToComments($file);
		print_r ($file);
		$tracks [] = array (
			'path' => addslashes("$dir/$filename"),
			'filename' => addslashes($filename),
			'filename_name' => addslashes($name),
			
			'artist'=> @$file['comments']['artist'][0],
			'title' => @$file['comments']['title'][0],
			'album' => @$file['comments']['album'][0],
			'year' => @$file['comments']['date'][0],
			
			'track' => @$file['comments']['tracknumber'][0],
			'total_tracks' => @$file['comments']['tracktotal'][0]
			);
		}
	}

$no_of_tracks = count($tracks);

if ($no_of_tracks == 0){
	print "nope\n";
	print "I didn't find any FLAC files\n";
	exit ('1');
}
print "found ".$no_of_tracks." files\n";

$release = array (
		'title'=>$tracks[0]['album'],
		'artist'=>$tracks[0]['artist'],
		'year'=>$tracks[0]['year'],
		'total_tracks' => $tracks[0]['total_tracks']
	);


$storage = join ('/', array($dir, $release['artist']));

print "Making directory tree...";
mkdir ($storage);

#$storage = join ('/', array($dir, $release['artist'], $release['title']));
$storage .= "/{$release['title']}";
mkdir ($storage);

$dirstomake = array( 'download', 'stream', 'download/flac', 'download/mp3', 'download/ogg', 'zip' );
foreach( $dirstomake as $foo ) 
	{ 
	#mkdir (join ('/', array($storage, $foo ))); 
	mkdir( "{$storage}/{$foo}");
	}
/*
mkdir (join ('/', array($storage, 'download')));
mkdir (join ('/', array($storage, 'stream')));
mkdir (join ('/', array($storage, 'download', 'flac')));
mkdir (join ('/', array($storage, 'download', 'mp3')));
mkdir (join ('/', array($storage, 'download', 'ogg')));
mkdir (join ('/', array($storage, 'zip')));
*/

print "done\n\n";

print $release['artist'].' - '.$release['title']."\n";
print "====================================================\n\n";

$current_track = 1;

foreach ($tracks as $track) {		
	
	print "=== [".$current_track.'/'.$no_of_tracks.'] '.$track['title']."\n";
	print "Converting to wav file for processing...";
	system ("$flacpath -d -s '".addslashes( $track['path'] )."'");
	print "done \n";
	

	$wav = join ('.',array($track['filename_name'], 'wav'));
	$mp3 = join ('.',array($track['filename_name'], 'mp3'));
	$ogg = join ('.',array($track['filename_name'], 'ogg'));

	$lametag= "--ta '".$release['artist']."' --tl '".$release['title']."' --ty '".$release['year']."' --tn '".$track['track']."' --tt '".$track['title']."'";
	
	print "Converting to high quality mp3...";
	system ("$lamepath $lametag --quiet -V 0 --vbr-new '".$dir."/".$wav."' '".join ('/', array($storage, 'download','mp3',$mp3))."'");
	print "done \n";
	
	print "Converting to streamable mp3...";
	system ("$lamepath $lametag --quiet --cbr -b 128 '".$dir."/".$wav."' '".join ('/', array($storage, 'stream',$mp3 ))."'");
	print "done\n";
	
	$oggtag= "-a '".$release['artist']."' -l '".$release['title']."' -d '".$release['year']."' -N '".$track['track']."' -t '".$track['title']."'";
	
	print "Converting to ogg file...";
	system ("$oggpath $oggtag -Q -q 5 -o '".join ('/', array($storage, 'download','ogg',$ogg ))."' '".$dir."/".$wav."'");
	print "done\n";
	
	print "Moving flac file to correct directory...";
	rename ($track['path'],join ('/', array($storage, 'download','flac',$track['filename'])));
	print "done\n";
	
	print "Cleaning up...";
	unlink ("$dir/$wav");
	print "done\n";
	
	print "Adding file to database...";
// Code eventually to add to a SQL database...
	print "done\n";
	
	$current_track++;
	print "\n";
	
}

echo "Making zip release bundles...";

foreach( $filetypes as $foo )
	{
	echo $foo.'...';
	system( "zip -9 -jqr '{$storage}/zip/{$release['artist']}_{$release['title']}_{$foo}.zip' '{$storage}/download/{$foo}'" );
	}

print "done \n";
print "All done!\n";
exit (0);

?>
98ae5419a4fd2b739280c3ed7d6cdb3d

wegosublime

November 29, 2007, November 29, 2007 02:41, permalink

2 ratings. Login to rate!

Thanks very much dude! anything else you could add would be awesome and I'll (naturally) give you full credit!

98ae5419a4fd2b739280c3ed7d6cdb3d

wegosublime

November 29, 2007, November 29, 2007 02:45, permalink

No rating. Login to rate!

Meant to click on 5 stars, apologies!

Your refactoring





Format Copy from initial code

or Cancel