Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Automatically Encode Ogg / MP3s

by Vautrin (Hermit)
on May 18, 2005 at 18:51 UTC ( [id://458392]=sourcecode: print w/replies, xml ) Need Help??
Category: Audio Related Programs
Author/Contact Info Daniel R. Anderson
trompelamort KOALA gmail.com =~ s/marsupial/@/;
Description: Basically what this file does is read in a list of filenames, and use those filenames to encode all wav files in the directory to either Ogg or Mp3. Basically it lets me run $ cdparanoia -B to create all the wav files, and then use the following syntax to call ogger.pl:
$ ./ogger.pl "Filelist" "Genre" "Artist" "Album" "Encoding"
Where Encoding is either "ogg" or "mp3", and Filelist is a list of tracks, one after another.

#! /usr/bin/perl

# created by Daniel R. Anderson (2005)
# trompelamort KOALA gmail.com =~ s/marsupial/@/;
# this code protected under the GPL

use strict;
use warnings;

# VARIABLES
# these variables are needed to process the files

my $filelist    = shift @ARGV;
my $genre       = shift @ARGV;
my $artist      = shift @ARGV;
my $album       = shift @ARGV;
my $encoding    = (shift (@ARGV)) || "mp3";
# quality for the ogg encoder on a scale of 1 to 10
# 10 being best
my $ogg_quality = 10;
# quality for the mp3 encoder (lame) on a scale of 0 to 9
# 0 being the best
# a quick and dirty test says that you do not want to use 0
# a 9 seems to be CD quality sound
# since you can't get better than CD quality sound
# stick with the 9
my $mp3_quality = 9;
# shall we print debug messages?
my $debug       = 1;

#error checking
unless ($filelist and $artist and $album) {
  die ("Usage: ./ogger.pl filelist artist album\n");
}
elsif (not (-e $filelist)) {
  die ("Filename $filelist does not exist!\n");
}
elsif (not (-r $filelist)) {
  die ("We do not have sufficient permission to read $filelist\n");
}

print "Using $filelist as file list.\n";


# open the file list for reading
open ("FILELIST", "< ./$filelist")
  or die ("Cannot open $filelist because $!\n");

# grab all the filenames
my @filenames = <FILELIST>;

if ($debug) {
  print "DEBUG INFO:\n\n";
  print "filenames:\n";
  my @filecopy = @filenames;
  while (@filecopy) {
    print (shift @filecopy);
  }
}

# my $filename = <FILELIST>;
# while ($filename) {
#   $filename = chomp $filename;
#   print "filename $filename\n";
#   if ($filename) {
#     push @filenames, $filename;
#   }
#   $filename = <FILELIST>
# }

# clean up our mess
close (FILELIST) or die ("Cannot close FILELIST due to $!\n");

# we've got the filenames, set up the encodings
my $encoder  = "";
my $switches = "";


# set up the data we need to encode the files

if ($encoding eq "ogg") {
  # encode the wav files using the Ogg Vorbis encoding
  $encoder  = "oggenc";
  $switches = "--genre \"$genre\" --tracknum \"??track??\" --title \"?
+?trackname??\""
      . " --album \"$album\" --artist \"$artist\" --quality=$ogg_quali
+ty -o \"??output_file??.ogg\" "
      . "\"??input_file??\"";
} elsif ($encoding eq "mp3") {
  # encode the wav files using the mp3 encoding
  # make use of LAME
  # first convert the quality from oggenc to lame
  $encoder  = "lame";
  $switches = "-q $mp3_quality \"??input_file??\" \"??output_file??.mp
+3\" ";
} else {
  die ("We should never get here!");
}

unless ($encoder) {
  die ("No encoder specified!\n");
} elsif (not $switches) {
  die ("No switches specified!\n");
}

if ($debug) {
  print "DEBUG INFO\n\n";
  print "Encoder: $encoder\n";
  print "Switches: $switches\n\n";
}

my $counter = 1;
while (@filenames) {
  my $filename = shift (@filenames);
  chomp $filename;
  next unless $filename;
  print "Encoding filename $filename\n";
  my $tempcounter = $counter;
  if ($counter < 10) {
    $tempcounter = '0' . $counter;
  }
  my $input_file     = "track" . $tempcounter . ".cdda.wav";
  my $output_file    = $tempcounter . ". $artist -- $album -- $filenam
+e";

  # although an array/while-loop may seem superfluous,
  # it is important because it allows for multiple
  # commands... i.e. encode this file
  # then move it to a prespecified directory.
  my @to_execute = (
            "$encoder $switches",
           );
  while (@to_execute) {
    my $execute_me = shift (@to_execute);
    $execute_me =~ s[\?\?output_file\?\?][$output_file];
    $execute_me =~ s[\?\?input_file\?\?][$input_file];
    $execute_me =~ s[\?\?track\?\?][$tempcounter];
    $execute_me =~ s[\?\?trackname\?\?][$filename];
    next unless $execute_me;
    if ($debug) {
      print "DEBUG INFO:\n\n";
      print "Executing command:\n$execute_me\n\n";
    }
    `$execute_me`;
  }
  $counter++;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://458392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-03-28 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found