3 #==============================================================================
5 # File ID: d4a9dfc6-3fd5-11e7-9638-f74d993421b0
7 # Convert .wav and .flac to .opus and place them under a destination directory
8 # while keeping the original directory structure.
10 # Character set: UTF-8
11 # ©opyleft 2017– Øyvind A. Holm <sunny@sunbase.org>
12 # License: GNU General Public License version 2 or later, see end of file for
14 #==============================================================================
24 my $STD_DEST = 'opusfiles';
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.2.0';
45 my $CMD_OPUSENC = 'opusenc';
47 Getopt
::Long
::Configure
('bundling');
50 'dest|d=s' => \
$Opt{'dest'},
51 'dry-run|n' => \
$Opt{'dry-run'},
52 'force|f' => \
$Opt{'force'},
53 'help|h' => \
$Opt{'help'},
54 'quiet|q+' => \
$Opt{'quiet'},
55 'verbose|v+' => \
$Opt{'verbose'},
56 'version' => \
$Opt{'version'},
58 ) || die("$progname: Option error. Use -h for help.\n");
60 $Opt{'verbose'} -= $Opt{'quiet'};
61 $Opt{'help'} && usage
($EXIT_OK);
62 if ($Opt{'version'}) {
70 my $Retval = $EXIT_OK;
71 my $dest = $Opt{'dest'};
74 while (defined($f = <>) && $Retval == $EXIT_OK) {
77 $Retval = convert_file
($f, $dest);
86 my ($noext, $subdir, $base, $outfile);
89 if ($f =~ /\.(wav|flac)$/i) {
96 $noext =~ s/(^.*)\.[^\.\/]+?$/$1/;
98 $subdir = dirname
($subdir);
99 $base = basename
($noext);
101 $outfile = "$subdir/$base.opus";
102 if (-e
$outfile && !$Opt{'force'}) {
103 msg
(0, "$f exists, use --force to overwrite");
106 msg
(0, sprintf("Converting $f%s",
107 $Opt{'dry-run'} ?
' (simulating)' : ''));
108 return $EXIT_OK if ($Opt{'dry-run'});
110 if (!-e
$subdir && !mkpath
($subdir)) {
111 warn("$progname: $subdir: Could not create directory: $!\n");
114 $result = system($CMD_OPUSENC, $f, "$outfile.tmp");
116 warn("\n$progname: Child process interrupted, aborting\n");
119 rename("$outfile.tmp", $outfile);
125 # Print program version
126 print("$progname $VERSION\n");
131 # Send the help message to stdout
134 if ($Opt{'verbose'}) {
140 Convert .wav and .flac to .opus and place them under a destination
141 directory while keeping the original directory structure.
143 Usage: $progname [options] [file [files [...]]]
148 Store the generated files under directory tree DIR while keeping the
149 original directory structure relative to DIR.
150 Default directory: "$STD_DEST"
152 Overwrite existing files.
156 Don't do anything, only print what would happen.
158 Be more quiet. Can be repeated to increase silence.
160 Increase level of verbosity. Can be repeated.
162 Print version information.
169 # Print a status message to stderr based on verbosity level
170 my ($verbose_level, $Txt) = @_;
172 if ($Opt{'verbose'} >= $verbose_level) {
173 print(STDERR
"$progname: $Txt\n");
180 # This program is free software; you can redistribute it and/or modify it under
181 # the terms of the GNU General Public License as published by the Free Software
182 # Foundation; either version 2 of the License, or (at your option) any later
185 # This program is distributed in the hope that it will be useful, but WITHOUT
186 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
187 # FOR A PARTICULAR PURPOSE.
188 # See the GNU General Public License for more details.
190 # You should have received a copy of the GNU General Public License along with
192 # If not, see L<http://www.gnu.org/licenses/>.
194 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :