#!/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; }