DOCS/xml: Get rid of xmllint wrapper redirection
[mplayer/glamo.git] / TOOLS / wma2ogg.pl
blobdcd4d7e994581cce4d08083f941304583df8399a
1 #!/usr/bin/perl
3 #################################
4 # wma converter 0.3.6 for Linux #
5 #################################
7 # Made by Peter Simon <simon.peter@linuxuser.hu>
8 # License: GNU GPL
10 # Needed programs:
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
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
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.
47 use Getopt::Long qw(:config pass_through);
49 $ver="0.3.6";
51 GetOptions("help|?",\&showhelp, 'a' => \$all, "file|f=s" => \@files,"t=s" => \$mtype, "lame" => \$needlame, "del" => \$delete, "br=i" => \$sbrate);
53 if (@ARGV) {
54 foreach (@ARGV) {
55 error ("Missing parameter after the $_ switch!");
57 exit;
60 print "wma2ogg $ver\nPeter Simon <simon.peter\@linuxuser.hu>";
62 if (!$all && !@files) {
63 error ("There is no selected file!");
64 exit;
67 if ($0 =~/wma2mp3/ && !$mtype) {
68 $mtype="mp3";
71 if ($mtype eq "ogg") {
72 $ttype{"ogg"}="oggenc";
73 needed_prgs (oggenc, "http://www.vorbis.com");
76 if ($needlame && $mtype eq "ogg") {
77 error ("\nYou can not use L.A.M.E. & oggenc together!\n");
78 exit;
81 if (!$mtype && !$needlame) {
82 $mtype="ogg";
83 $ttype{"ogg"}="oggenc";
84 needed_prgs (oggenc, "http://www.vorbis.com");
88 if (!$needlame && $mtype eq "mp3") {
89 $ttype{"mp3"}="BladeEnc";
90 needed_prgs (BladeEnc, "http://bladeenc.mp3.no", mp3info, "http://www.ibiblio.org/mp3info");
93 if ($needlame) {
94 $mtype="mp3";
95 $ttype{"mp3"}="lame";
96 needed_prgs (lame, "http://lame.sourceforge.net", mp3info, "http://www.ibiblio.org/mp3info");
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.
108 sub ch_type {
109 $o_type=$ttype{$mtype};
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);
121 if ((!$o_type || !$enc_ok) && $mtype ne "wav") {
122 error("Unknown encoder.");
123 exit;
127 sub 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;
134 sub ch_files {
135 if ($all && @files) {
136 error ("You can't use -a switch and -f together!");
137 exit;
140 if ($all) {
141 @enc_files=`ls | grep '.wma'\$`;
142 foreach (@enc_files) {
143 chomp $_;
147 if (@files) {
148 @enc_files=@files;
153 sub showhelp {
154 print "\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
157 OPTIONS:
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";
162 print $miss_text;
163 print "\n$errtext\nExiting program.\n";
166 sub error {
167 $errtext=@_[0];
168 showhelp ();
172 sub missing_prg {
173 $what=$keys;
174 $that=$needed{$keys};
175 $miss_text.="\nThe needed \'$what\' program is missing, see: $that!";
178 sub ch_needed {
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!)");
187 `rm ./err`;
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;
199 `rm ./err`;
202 close FILE;
203 if ($error) {
204 showhelp ();
205 exit;
209 sub get_tags {
210 my $outfile;
211 open (FILE, "<./1");
212 while ($sor=<FILE>) {
213 $outfile.=$sor;
215 close FILE;
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;
223 if (!$sbrate) {
224 $brate=$kept_orig_brate;
225 print " (kept as default)";
226 } else {
227 $brate=$sbrate;
228 print " (new: $brate,0)";
231 ch_br ();
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]);
243 sub needed_prgs {
244 %needed=(
245 $_[0]=>$_[1],
246 $_[2]=>$_[3],
247 mplayer=>"http://www.mplayerhq.hu",
251 sub mp3_info {
252 if ($title) {
253 $infofile=" -t '$title'";
255 if ($author) {
256 $infofile.=" -a '$author'";
258 if ($comments) {
259 $infofile.=" -c '$comments'";
262 `mp3info "$p_name.$mtype" $infofile`;
264 undef ($infofile);
268 sub 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";
280 `mplayer "$_" -ao pcm -input conf="$pwd" 2>/dev/null >./1`;
281 `mv "audiodump.wav" "$wav_name"`;
283 @tags=get_tags ();
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";
294 $comments=$copyright." ".$comments."Transcoded by wma2ogg";
296 print "\n\nConverting $wav_name to \"$mtype\" file.";
298 print "\nUsing $o_type...";
300 if ($mtype eq "ogg") {
301 $br_sw="b";
302 if ($title) {
303 $infofile=" -t '$title'";
305 if ($author) {
306 $infofile.=" -a '$author'";
308 if ($comments) {
309 $infofile.=" -c COMMENT='$comments'";
312 `"$o_type" "-$br_sw" "$brate" "-Q" "./$wav_name" $infofile "-o" "$p_name.$mtype"`;
313 `rm -f "$wav_name"`;
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`;
319 mp3_info ();
321 `rm -f "$wav_name"`;
323 if ($mtype eq "mp3" && $needlame) {
325 $br_sw="b";
326 `"$o_type" "-$br_sw" "$brate" "-f" "$wav_name" "$p_name.$mtype" 2>/dev/null >/dev/null`;
328 mp3_info ();
330 `rm -f "$wav_name"`;
332 if ($delete) {
333 `rm -f "$_"`;
335 if (-e "./err") {
336 `rm ./err`;
339 print "\n\nDone.\n\n";