Add a perl implementation of regain as regains

This commit is contained in:
Fred Boniface 2023-10-02 11:28:01 +01:00
parent c067689e14
commit 41c817f1fc
1 changed files with 81 additions and 0 deletions

81
regains Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
use Getopt::Long;
my @FORMATS = ("flac", "m4a", "aac", "alac", "mp3", "m3a", "ogg", "opus", "oga");
my $PATH_TO_REGAINER = "./regainer.py";
my $debug = 0;
# Parse command-line arguments
GetOptions("debug" => \$debug);
# Configure logging
if ($debug) {
$| = 1; # Disable buffering for STDOUT
print "Debug logs enabled\n";
}
my $workdir = getcwd();
print "regain.pl WorkDir: $workdir\n";
my @file_lists = find_files_by_extension($workdir);
print "Files for processing: ", countFiles(\@file_lists), "\n";
my @cmd = ("python", $PATH_TO_REGAINER, "-f");
foreach my $sublist (@file_lists) {
push @cmd, "-a";
push @cmd, @$_ foreach @$sublist;
}
print "Running command: @cmd\n";
print "Running regainer.py\n";
if ($debug) {
open my $stdout_saved, ">&STDOUT";
open my $stderr_saved, ">&STDERR";
open STDOUT, ">", undef;
open STDERR, ">", undef;
my $result = system(@cmd);
open STDOUT, ">&", $stdout_saved;
open STDERR, ">&", $stderr_saved;
print "regainer.py has exited with code $result\n";
if ($result != 0) {
print "You can re-run with --debug to view the output from regainer\n";
} else {
print "Files successfully tagged\n";
}
} else {
system(@cmd);
}
sub find_files_by_extension {
my ($workdir) = @_;
my @file_lists;
find(sub {
if (-f && /\.(@FORMATS)$/) {
push @{$file_lists[-1]}, $File::Find::name;
}
}, $workdir);
return \@file_lists;
}
sub countFiles {
my ($file_list_of_lists) = @_;
my $count = 0;
foreach my $item (@$file_list_of_lists) {
$count += scalar(@$item);
}
return $count;
}