Kjetil's Information Center: A Blog About My Projects

Cue Sheet Splitter

Here is a Perl script that reads a cue sheet file and attempts to use that to split an associated WAV file.
It relies on both the "sox" (Sound eXchange, the Swiss Army knife of audio manipulation) and "flac" (Free Lossless Audio Codec) external tools.

I have used it successfully on several sets, take a look:

#!/usr/bin/perl -w
use strict;
# FLAC to WAV: flac -d *.flac

my $src = shift @ARGV or die "Usage: $0 <big wav file>\n";

undef $/;

my $last_track = 0;
my $last_title = 0;
my $last_artist = 0;
my $last_ts_min = 0;
my $last_ts_sec = 0;
my $last_ts_msec = 0;

while (<>) {
  while (m/TRACK (\d+) AUDIO.*?TITLE "([^"]*)".*?PERFORMER "([^"]*)".*?INDEX \d+ (\d+):(\d+):(\d+)/gs) {
    my $track = $1;
    my $title = $2;
    my $artist = $3;
    my $ts_min = $4;
    my $ts_sec = $5;
    my $ts_msec = $6;

    my $last_msec = ($last_ts_min * 60 * 100) + ($last_ts_sec * 100) + $last_ts_msec;
    my $msec = ($ts_min * 60 * 100) + ($ts_sec * 100) + $ts_msec;
    my $diff_total = $msec - $last_msec;
    my $duration = $diff_total / 100;

    if ($last_track > 0) {
      system("sox $src $last_track.wav trim $last_ts_min:$last_ts_sec.$last_ts_msec $duration");
      system("flac --best $last_track.wav");
      system("mv $last_track.flac \"$last_track - $last_artist - $last_title.flac\"");
    }

    $last_track = $track;
    $last_title = $title;
    $last_artist = $artist;
    $last_ts_min = $ts_min;
    $last_ts_sec = $ts_sec;
    $last_ts_msec = $ts_msec;
  }
}

system("sox $src $last_track.wav trim $last_ts_min:$last_ts_sec.$last_ts_msec");
system("flac --best $last_track.wav");
system("mv $last_track.flac \"$last_track - $last_artist - $last_title.flac\"");
          


Topic: Scripts and Code, by Kjetil @ 01/03-2014, Article Link