From 41c817f1fccb285fe67c474a3cb3b0235a9140e2 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 2 Oct 2023 11:28:01 +0100 Subject: [PATCH] Add a perl implementation of regain as regains --- regains | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 regains diff --git a/regains b/regains new file mode 100644 index 0000000..ca4fa3c --- /dev/null +++ b/regains @@ -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; +}