Log on: Remember me
Powered by Elgg

Timo Baumann :: Blog Archives

April 2010

April 17, 2010

I've finally written the one script that was missing from the interwebs and that I have longed to have for so long:

#!/usr/bin/perl
# Copyright (C) 2010 Timo Baumann
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use Audio::Wav;
use Audio::Wav::Read;

#usage: audio-duration.pl path-or-file1 path-or-file2 ...

my @files;
for my $arg (@ARGV) {
    my $findresult = `find $arg`;
    push @files, grep /.wav$/, split " ", $findresult;
}
#print join " ", @files;
my $duration = 0.0;
my $wav = new Audio::Wav;
for my $file (@files) {
    my $read = $wav->read($file);
    $duration += $read->length_seconds();
}
# convert to something readable
my $readableDuration = "";
if ($duration > 600) {
    my $seconds = int($duration + .5);
    my $minutes = int($duration / 60);
    $seconds -= $minutes * 60;
    my $hours = int($minutes / 60);
    $minutes -= $hours * 60;
    $readableDuration = "(" . ($hours > 0 ? "$hours:" : "") . "$minutes'$seconds\") ";
}
print "$duration seconds ", $readableDuration, "in ", ($#files + 1), " wave files.\n";

Running this in any directory wil yield the duration of audio (only .wav files) of all the files in this directory. If you supply arguments, it will look into the given directories (or files) and tell you the summed duration.

A must-have for any corpus-linguist dealing with loads of audio files!

Keywords: audio, perl

Posted by Timo Baumann | 0 comment(s)