1#!/usr/local/bin/perl
2
3#################################
4# wma converter 0.3.6 for Linux #
5#################################
6
7# Made by Peter Simon <simon.peter@linuxuser.hu>
8# License: GNU GPL
9#
10# Needed programs:
11#
12# Getopt::Long - Perl Module
13# MPlayer - http://www.mplayerhq.hu
14# BladeEnc - http://bladeenc.mp3.no
15# oggenc - http://www.vorbis.com
16# mp3info - http://www.ibiblio.org/mp3info
17
18# changelog:
19# 0.1.0
20# decoding all files to wav without any switches and encoders
21# 0.2.0
22# converting to mp3 or ogg with bitrate, delete options
23# 0.2.1
24# L.A.M.E. support for fanatics
25# Default output filetype: ogg
26# Some error warnings
27# 0.3.0
28# Reading the wma tags out and puting into the mp3 or the ogg tags. (What the MPlayer shows of those.)
29# Default output bitrate: the original bitrate
30# Simlink for the default encode mode (wma2ogg = ogg, wma2mp3 = mp3)
31# 0.3.1
32# Neglecting missing encoders if those are not in use.
33# 0.3.2
34# Using mp3info for the mp3 tags
35# 0.3.3
36# Convert more then one files in the start dir.
37# 0.3.4-5
38# Some bugfixes.
39# 0.3.6
40# Some bugfixes by Diego Biurrun
41
42# Why BladeEnc?
43# Eg.: The L.A.M.E.'s code isn't compatible for some hardwer-decoders. Makes noise and clashings.
44# I never met this trouble using BladeEnc.
45# That's it.
46
47use Getopt::Long qw(:config pass_through);
48
49$ver="0.3.6";
50
51GetOptions("help|?",\&showhelp, 'a' => \$all, "file|f=s" => \@files,"t=s" => \$mtype, "lame" => \$needlame, "del" => \$delete, "br=i" => \$sbrate);
52
53if (@ARGV) {
54	foreach (@ARGV) {
55			error ("Missing parameter after the $_ switch!");
56	}
57	exit;
58}
59
60print "wma2ogg $ver\nPeter Simon <simon.peter\@linuxuser.hu>";
61
62if (!$all && !@files) {
63	error ("There is no selected file!");
64	exit;
65}
66
67if ($0 =~/wma2mp3/ && !$mtype) {
68	$mtype="mp3";
69}
70
71if ($mtype eq "ogg") {
72	$ttype{"ogg"}="oggenc";
73	needed_prgs (oggenc, "http://www.vorbis.com");
74}
75
76if ($needlame && $mtype eq "ogg") {
77	error ("\nYou can not use L.A.M.E. & oggenc together!\n");
78	exit;
79}
80
81if (!$mtype && !$needlame) {
82	$mtype="ogg";
83	$ttype{"ogg"}="oggenc";
84	needed_prgs (oggenc, "http://www.vorbis.com");
85}
86
87
88if (!$needlame && $mtype eq "mp3") {
89	$ttype{"mp3"}="BladeEnc";
90	needed_prgs (BladeEnc, "http://bladeenc.mp3.no", mp3info, "http://www.ibiblio.org/mp3info");
91}
92
93if ($needlame) {
94	$mtype="mp3";
95	$ttype{"mp3"}="lame";
96	needed_prgs (lame, "http://lame.sourceforge.net", mp3info, "http://www.ibiblio.org/mp3info");
97}
98
99# Main program
100	print "\nUsing lame - WARNING - this is not optimal!\n";
101	ch_needed ();
102	ch_type ();
103	ch_files ();
104	decode();
105# the end.
106
107
108sub ch_type {
109	$o_type=$ttype{$mtype};
110
111	if ($mtype ne "wav") {
112		$def_path=$ENV{PATH};
113		@exec_path=split /\:/, $def_path;
114		foreach $temp_path (@exec_path) {
115			if (-d $temp_path && !$enc_ok) {
116				$enc_ok=`find $temp_path -name $o_type -type f -maxdepth 1 2>/dev/null`;
117				chomp ($enc_ok);
118			}
119		}
120	}
121	if ((!$o_type || !$enc_ok) && $mtype ne "wav") {
122		error("Unknown encoder.");
123		exit;
124	}
125}
126
127sub ch_br {
128	if ($sbrate && ((($sbrate <32 || $sbrate>350) && $mtype eq "mp3")  ||   (($sbrate<64 || $sbrate>500) && $mtype eq "ogg") )) {
129		error("Invalid, missing or redundant bitrate.");
130		exit;
131	}
132}
133
134sub ch_files {
135	if ($all && @files) {
136		error ("You can't use -a switch and -f together!");
137		exit;
138	}
139
140	if ($all) {
141		@enc_files=`ls | grep '.wma'\$`;
142		foreach (@enc_files) {
143			chomp $_;
144		}
145	}
146
147	if (@files) {
148		@enc_files=@files;
149	}
150
151}
152
153sub showhelp {
154print "\n\nUsage: wma2ogg [OPTIONS] (-f FILE1 | -f FILE2 ... | -a)\n
155-f, -file         filename
156-a                converts all wma files in the current directory\n
157OPTIONS:
158-t                output filetype (ogg, mp3) [default=ogg]
159-lame             I wanna use L.A.M.E. sure enough!
160-br               bitrate (kb/s) [default=from the wma]
161-del              remove wma file(s) after the transcoding\n";
162print $miss_text;
163print "\n$errtext\nExiting program.\n";
164}
165
166sub error {
167	$errtext=@_[0];
168	showhelp ();
169
170}
171
172sub missing_prg {
173	$what=$keys;
174	$that=$needed{$keys};
175	$miss_text.="\nThe needed \'$what\' program is missing, see: $that!";
176}
177
178sub ch_needed {
179
180	`perl -e 'use Getopt::Long;' 2>./err`;
181	open (FILE, "<./err");
182	while ($sor=<FILE>) {
183		if ($sor =~ /Can\'t locate/) {
184			missing_prg ("Getopt::Long", ": your Perl is too old... (uhhh... get a new one!)");
185		}
186	}
187	`rm ./err`;
188
189
190	foreach $keys (keys %needed) {
191		`$keys 2>./err`;
192		open (FILE, "<./err");
193		while ($sor=<FILE>) {
194			if ($sor =~ /$keys\: command not found/) {
195				missing_prg ();
196				$error=1;
197			}
198		}
199	`rm ./err`;
200	}
201
202	close FILE;
203	if ($error) {
204	showhelp ();
205	exit;
206	}
207}
208
209sub get_tags {
210	my $outfile;
211	open (FILE, "<./1");
212	while ($sor=<FILE>) {
213		$outfile.=$sor;
214	}
215	close FILE;
216
217	$outfile=~ s/\((\d+\,\d+)\ kbit\)/$1/e;
218	print "\noriginal bitrate: $1";
219	$kept_orig_brate=$1;
220	$kept_orig_brate=~ s/(\d+)/$1/e;
221	$kept_orig_brate=$1;
222
223	if (!$sbrate) {
224		$brate=$kept_orig_brate;
225		print " (kept as default)";
226	} else {
227		$brate=$sbrate;
228		print " (new: $brate,0)";
229	}
230
231	ch_br ();
232
233	`rm ./1`;
234	my @temp_info=split /Clip\ info\:/, $outfile;
235	my @temp2_info=split /\n/, @temp_info[1];
236	my @temp_title=split /\ /, $temp2_info[1],3;
237	my @temp_author=split /\ /, $temp2_info[2],3;
238	my @temp_copyright=split /\ /, $temp2_info[3],3;
239	my @temp_comments=split /\ /, $temp2_info[4],3;
240	return ($temp_title[2], $temp_author[2], $temp_copyright[2], $temp2_comments[2]);
241}
242
243sub needed_prgs {
244	%needed=(
245		$_[0]=>$_[1],
246		$_[2]=>$_[3],
247		mplayer=>"http://www.mplayerhq.hu",
248	);
249}
250
251sub mp3_info {
252			if ($title) {
253				$infofile=" -t '$title'";
254			}
255			if ($author) {
256				$infofile.=" -a '$author'";
257			}
258			if ($comments) {
259				$infofile.=" -c '$comments'";
260			}
261
262			`mp3info "$p_name.$mtype" $infofile`;
263
264			undef ($infofile);
265}
266
267
268sub decode {
269	foreach (@enc_files) {
270		$wav_name=$_;
271		$wav_name=~ s/(.+)\./$1/e;
272		$p_name=$1;
273		$wav_name=$p_name.".wav";
274		$pwd=`pwd`;
275		chomp $pwd;
276		$pwd.="/t2";
277		print "\nConverting $_ to \"wav\" file.\n";
278		print "Using MPlayer...\n";
279
280		`mplayer "$_" -ao pcm -input conf="$pwd" 2>/dev/null >./1`;
281		`mv "audiodump.wav" "$wav_name"`;
282
283		@tags=get_tags ();
284
285		$title=$tags[0];
286		print "\ntitle: $title";
287		$author=$tags[1];
288		print "\nauthor: $author";
289		$copyright=$tags[2];
290		print "\ncopyright: $copyright";
291		$comments=$tags[3];
292		print "\ncomments: $comments";
293
294		$comments=$copyright." ".$comments."Transcoded by wma2ogg";
295
296		print "\n\nConverting $wav_name to \"$mtype\" file.";
297
298		print "\nUsing $o_type...";
299
300		if ($mtype eq "ogg") {
301			$br_sw="b";
302			if ($title) {
303				$infofile=" -t '$title'";
304			}
305			if ($author) {
306				$infofile.=" -a '$author'";
307			}
308			if ($comments) {
309				$infofile.=" -c COMMENT='$comments'";
310			}
311
312			`"$o_type" "-$br_sw" "$brate" "-Q" "./$wav_name" $infofile "-o" "$p_name.$mtype"`;
313			`rm -f "$wav_name"`;
314		}
315		if ($mtype eq "mp3" && !$needlame) {
316			$br_sw="br";
317			`"$o_type" "$wav_name" "$p_name.$mtype" "-$br_sw" "$brate" 2>/dev/null >/dev/null`;
318
319			mp3_info ();
320
321			`rm -f "$wav_name"`;
322		}
323		if ($mtype eq "mp3" && $needlame) {
324
325			$br_sw="b";
326			`"$o_type" "-$br_sw" "$brate" "-f" "$wav_name" "$p_name.$mtype" 2>/dev/null >/dev/null`;
327
328			mp3_info ();
329
330			`rm -f "$wav_name"`;
331		}
332		if ($delete) {
333			`rm -f "$_"`;
334		}
335		if (-e "./err") {
336			`rm ./err`;
337		}
338	}
339	print "\n\nDone.\n\n";
340}
341