summaryrefslogtreecommitdiffstats
path: root/TOOLS/wma2ogg.pl
blob: 7b81b430f4492d23c41c7c003088e1f79e35e72c (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/perl

#################################
# wma converter 0.3.6 for Linux #
#################################

# Made by Peter Simon <simon.peter@linuxuser.hu>
# License: GNU GPL
#
# Needed programs:
#
# Getopt::Long - Perl Module
# MPlayer - http://www.mplayerhq.hu
# BladeEnc - http://bladeenc.mp3.no
# oggenc - http://www.vorbis.com
# mp3info - http://www.ibiblio.org/mp3info

# changelog:
# 0.1.0
# decoding all files to wav without any switches and encoders
# 0.2.0
# converting to mp3 or ogg with bitrate, delete options
# 0.2.1
# L.A.M.E. support for fanatics
# Default output filetype: ogg
# Some error warnings
# 0.3.0
# Reading the wma tags out and puting into the mp3 or the ogg tags. (What the MPlayer shows of those.)
# Default output bitrate: the original bitrate
# Simlink for the default encode mode (wma2ogg = ogg, wma2mp3 = mp3)
# 0.3.1
# Neglecting missing encoders if those are not in use.
# 0.3.2
# Using mp3info for the mp3 tags
# 0.3.3
# Convert more then one files in the start dir.
# 0.3.4-5
# Some bugfixes.
# 0.3.6
# Some bugfixes by Diego Biurrun

# Why BladeEnc?
# Eg.: The L.A.M.E.'s code isn't compatible for some hardwer-decoders. Makes noise and clashings.
# I never met this trouble using BladeEnc.
# That's it.

use Getopt::Long qw(:config pass_through);

$ver="0.3.6";

GetOptions("help|?",\&showhelp, 'a' => \$all, "file|f=s" => \@files,"t=s" => \$mtype, "lame" => \$needlame, "del" => \$delete, "br=i" => \$sbrate);

if (@ARGV) {
	foreach (@ARGV) {
			error ("Missing parameter after the $_ switch!");
	}
	exit;
}

print "wma2ogg $ver\nPeter Simon <simon.peter\@linuxuser.hu>";

if (!$all && !@files) {
	error ("There is no selected file!");
	exit;
}

if ($0 =~/wma2mp3/ && !$mtype) {
	$mtype="mp3";
}

if ($mtype eq "ogg") {
	$ttype{"ogg"}="oggenc";
	needed_prgs (oggenc, "http://www.vorbis.com");
}

if ($needlame && $mtype eq "ogg") {
	error ("\nYou can not use L.A.M.E. & oggenc together!\n");
	exit;
}

if (!$mtype && !$needlame) {
	$mtype="ogg";
	$ttype{"ogg"}="oggenc";
	needed_prgs (oggenc, "http://www.vorbis.com");
}


if (!$needlame && $mtype eq "mp3") {
	$ttype{"mp3"}="BladeEnc";
	needed_prgs (BladeEnc, "http://bladeenc.mp3.no", mp3info, "http://www.ibiblio.org/mp3info");
}

if ($needlame) {
	$mtype="mp3";
	$ttype{"mp3"}="lame";
	needed_prgs (lame, "http://lame.sourceforge.net", mp3info, "http://www.ibiblio.org/mp3info");
}

# Main program
	print "\nUsing lame - WARNING - this is not optimal!\n";
	ch_needed ();
	ch_type ();
	ch_files ();
	decode();
# the end.


sub ch_type {
	$o_type=$ttype{$mtype};

	if ($mtype ne "wav") {
		$def_path=$ENV{PATH};
		@exec_path=split /\:/, $def_path;
		foreach $temp_path (@exec_path) {
			if (-d $temp_path && !$enc_ok) {
				$enc_ok=`find $temp_path -name $o_type -type f -maxdepth 1 2>/dev/null`;
				chomp ($enc_ok);
			}
		}
	}
	if ((!$o_type || !$enc_ok) && $mtype ne "wav") {
		error("Unknown encoder.");
		exit;
	}
}

sub ch_br {
	if ($sbrate && ((($sbrate <32 || $sbrate>350) && $mtype eq "mp3")  ||   (($sbrate<64 || $sbrate>500) && $mtype eq "ogg") )) {
		error("Invalid, missing or redundant bitrate.");
		exit;
	}
}

sub ch_files {
	if ($all && @files) {
		error ("You can't use -a switch and -f together!");
		exit;
	}

	if ($all) {
		@enc_files=`ls | grep '.wma'\$`;
		foreach (@enc_files) {
			chomp $_;
		}
	}

	if (@files) {
		@enc_files=@files;
	}

}

sub showhelp {
print "\n\nUsage: wma2ogg [OPTIONS] (-f FILE1 | -f FILE2 ... | -a)\n
-f, -file         filename
-a                converts all wma files in the current directory\n
OPTIONS:
-t                output filetype (ogg, mp3) [default=ogg]
-lame             I wanna use L.A.M.E. sure enough!
-br               bitrate (kb/s) [default=from the wma]
-del              remove wma file(s) after the transcoding\n";
print $miss_text;
print "\n$errtext\nExiting program.\n";
}

sub error {
	$errtext=@_[0];
	showhelp ();

}

sub missing_prg {
	$what=$keys;
	$that=$needed{$keys};
	$miss_text.="\nThe needed \'$what\' program is missing, see: $that!";
}

sub ch_needed {

	`perl -e 'use Getopt::Long;' 2>./err`;
	open (FILE, "<./err");
	while ($sor=<FILE>) {
		if ($sor =~ /Can\'t locate/) {
			missing_prg ("Getopt::Long", ": your Perl is too old... (uhhh... get a new one!)");
		}
	}
	`rm ./err`;


	foreach $keys (keys %needed) {
		`$keys 2>./err`;
		open (FILE, "<./err");
		while ($sor=<FILE>) {
			if ($sor =~ /$keys\: command not found/) {
				missing_prg ();
				$error=1;
			}
		}
	`rm ./err`;
	}

	close FILE;
	if ($error) {
	showhelp ();
	exit;
	}
}

sub get_tags {
	my $outfile;
	open (FILE, "<./1");
	while ($sor=<FILE>) {
		$outfile.=$sor;
	}
	close FILE;

	$outfile=~ s/\((\d+\,\d+)\ kbit\)/$1/e;
	print "\noriginal bitrate: $1";
	$kept_orig_brate=$1;
	$kept_orig_brate=~ s/(\d+)/$1/e;
	$kept_orig_brate=$1;

	if (!$sbrate) {
		$brate=$kept_orig_brate;
		print " (kept as default)";
	} else {
		$brate=$sbrate;
		print " (new: $brate,0)";
	}

	ch_br ();

	`rm ./1`;
	my @temp_info=split /Clip\ info\:/, $outfile;
	my @temp2_info=split /\n/, @temp_info[1];
	my @temp_title=split /\ /, $temp2_info[1],3;
	my @temp_author=split /\ /, $temp2_info[2],3;
	my @temp_copyright=split /\ /, $temp2_info[3],3;
	my @temp_comments=split /\ /, $temp2_info[4],3;
	return ($temp_title[2], $temp_author[2], $temp_copyright[2], $temp2_comments[2]);
}

sub needed_prgs {
	%needed=(
		$_[0]=>$_[1],
		$_[2]=>$_[3],
		mplayer=>"http://www.mplayerhq.hu",
	);
}

sub mp3_info {
			if ($title) {
				$infofile=" -t '$title'";
			}
			if ($author) {
				$infofile.=" -a '$author'";
			}
			if ($comments) {
				$infofile.=" -c '$comments'";
			}

			`mp3info "$p_name.$mtype" $infofile`;

			undef ($infofile);
}


sub decode {
	foreach (@enc_files) {
		$wav_name=$_;
		$wav_name=~ s/(.+)\./$1/e;
		$p_name=$1;
		$wav_name=$p_name.".wav";
		$pwd=`pwd`;
		chomp $pwd;
		$pwd.="/t2";
		print "\nConverting $_ to \"wav\" file.\n";
		print "Using MPlayer...\n";

		`mplayer "$_" -ao pcm -input conf="$pwd" 2>/dev/null >./1`;
		`mv "audiodump.wav" "$wav_name"`;

		@tags=get_tags ();

		$title=$tags[0];
		print "\ntitle: $title";
		$author=$tags[1];
		print "\nauthor: $author";
		$copyright=$tags[2];
		print "\ncopyright: $copyright";
		$comments=$tags[3];
		print "\ncomments: $comments";

		$comments=$copyright." ".$comments."Transcoded by wma2ogg";

		print "\n\nConverting $wav_name to \"$mtype\" file.";

		print "\nUsing $o_type...";

		if ($mtype eq "ogg") {
			$br_sw="b";
			if ($title) {
				$infofile=" -t '$title'";
			}
			if ($author) {
				$infofile.=" -a '$author'";
			}
			if ($comments) {
				$infofile.=" -c COMMENT='$comments'";
			}

			`"$o_type" "-$br_sw" "$brate" "-Q" "./$wav_name" $infofile "-o" "$p_name.$mtype"`;
			`rm -f "$wav_name"`;
		}
		if ($mtype eq "mp3" && !$needlame) {
			$br_sw="br";
			`"$o_type" "$wav_name" "$p_name.$mtype" "-$br_sw" "$brate" 2>/dev/null >/dev/null`;

			mp3_info ();

			`rm -f "$wav_name"`;
		}
		if ($mtype eq "mp3" && $needlame) {

			$br_sw="b";
			`"$o_type" "-$br_sw" "$brate" "-f" "$wav_name" "$p_name.$mtype" 2>/dev/null >/dev/null`;

			mp3_info ();

			`rm -f "$wav_name"`;
		}
		if ($delete) {
			`rm -f "$_"`;
		}
		if (-e "./err") {
			`rm ./err`;
		}
	}
	print "\n\nDone.\n\n";
}