[Flac] FLAC: joining source files

porte64 at free.fr porte64 at free.fr
Wed Apr 1 21:56:05 PDT 2009


Hello,

Here's a script i use to join multiple PCM source files and feed them
to FLAC. In the future i plan to add tag support from static CDDB (files).

It would be nice if this feature could be incorporated to the native encoder.

Another idea is to have flac/metaflac support joining FLAC files
themselves (but this is much heavier to implement)
-- and may pose issues with meta info like seekpoints, cuepoints ...

========= snip snip ==========

#!/local/bin/perl
use strict;

our( @spt, at tr );

sub usage {
print "USAGES:\n\n$0  cddb_id [...]\n\n$0  cddb_id  start_track  end_track\n";
exit 1;
}

sub addtracks { my( @y, $k,$nfo,$nsp,$p,$z );
	usage() if $_[0]!~/^[0-9a-f]{8}$/;
	# build list of PCM files
	if ($#_>0) {
		for ($k=$_[1];$k<=$_[2];$k++) {
			if ($k>9) { $p='' } else { $p='0' }
			push @y,$_[0].'_'.$p.$k.'.wav';
		}
	}
	else {
		opendir D,'.';
		@y= sort(grep /^${_[0]}_\d+\.wav$/,readdir(D));
		closedir D;
	}
	# get stats
	$z=0; push @spt,0;
	foreach $k (@y) {
		$nfo= qx(sox -V $k -n 2>&1);
		die "[FATAL] no info from file: $k" if $nfo!~/duration[^=]*=\s*(\d+) samples/mi;
		$z+= $1;
		push @spt,$z;
		push @tr,$k;
	}
	return;
}

#-- MAIN

my( $discid,$discid0,$fgen,$nbsamples,$nfo,$seekpts,$t1,$t2 );

usage() if $#ARGV<0 or $ARGV[0]!~/^[0-9a-f]{8}$/; #yy or not(inpath 'sox')
$discid0= $ARGV[0];
$fgen='out_';

if ($#ARGV>0 and $ARGV[1]!~/^[0-9a-f]{8}$/) {
	($discid,$t1,$t2)= @ARGV;
	usage() unless $#ARGV==2 and $t1.$t2=~/^\d+$/ and $t1<$t2;
	$fgen.= "${discid}_${a}_${b}";
	addtracks($discid,$t1,$t2);
}
else {
	undef $t1;
	while ($discid=shift @ARGV) { $fgen.=$discid.'_'; addtracks($discid) }
	$fgen=~s/_$//;
}

$nbsamples=$spt[-1]; #yy unshift @spt; # provide seek to end
$seekpts= join ',', @spt;
die unless open I,"> $fgen.seekpts"; print I $seekpts; close I;

# join PCM files
system 'sox', @tr, "$fgen.wav";

# check sample count
$nfo= qx(sox -V $fgen.wav -n 2>&1);
$nfo!~/duration[^=]*=\s*(\d+) samples/mi;
if ($nbsamples==$1) { print STDERR "[ok] $nbsamples samples\n" }
else { print STDERR "[KO] frame count mismatch: $nbsamples vs $1\n" }

# encode to FLAC
system 'flac', '--silent', '--verify', '--warnings-as-errors', '--replay-gain', "--seekpoint=$seekpts", "-T CDDBID=$discid0";

#yy+ fetch tags from CDDB: GENRE AUTHOR WORK TTITLE[t1] INTERPRETER YEAR
exit 0;


More information about the Flac mailing list