mdisk: Remove color from "Already mounted" message
[sunny256-utils.git] / to-opus
blobeb924f17a4ad4ea243a7b1208c19b62778ef60e2
1 #!/usr/bin/env perl
3 #==============================================================================
4 # to-opus
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
13 # legal stuff.
14 #==============================================================================
16 use strict;
17 use warnings;
18 use File::Basename;
19 use File::Path;
20 use Getopt::Long;
22 local $| = 1;
24 my $STD_DEST = 'opusfiles';
26 my $EXIT_OK = 0;
27 my $EXIT_ERROR = 1;
29 our %Opt = (
31 'dest' => $STD_DEST,
32 'dry-run' => 0,
33 'force' => 0,
34 'help' => 0,
35 'quiet' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.2.0';
45 my $CMD_OPUSENC = 'opusenc';
47 Getopt::Long::Configure('bundling');
48 GetOptions(
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'}) {
63 print_version();
64 exit($EXIT_OK);
67 exit(main());
69 sub main {
70 my $Retval = $EXIT_OK;
71 my $dest = $Opt{'dest'};
72 my $f;
74 while (defined($f = <>) && $Retval == $EXIT_OK) {
75 chomp($f);
76 next if (!-f $f);
77 $Retval = convert_file($f, $dest);
80 return $Retval;
83 sub convert_file {
84 my ($f, $dest) = @_;
85 my $wav = '';
86 my ($noext, $subdir, $base, $outfile);
87 my $result;
89 if ($f =~ /\.(wav|flac)$/i) {
90 $wav = $f;
91 } else {
92 return $EXIT_OK;
95 $noext = $f;
96 $noext =~ s/(^.*)\.[^\.\/]+?$/$1/;
97 $subdir = "$dest/$f";
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");
104 return $EXIT_OK;
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");
112 return $EXIT_ERROR;
114 $result = system($CMD_OPUSENC, $f, "$outfile.tmp");
115 if ($result & 127) {
116 warn("\n$progname: Child process interrupted, aborting\n");
117 return $EXIT_ERROR;
119 rename("$outfile.tmp", $outfile);
121 return $EXIT_OK;
124 sub print_version {
125 # Print program version
126 print("$progname $VERSION\n");
127 return;
130 sub usage {
131 # Send the help message to stdout
132 my $Retval = shift;
134 if ($Opt{'verbose'}) {
135 print("\n");
136 print_version();
138 print(<<"END");
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 [...]]]
145 Options:
147 -d DIR, --dest DIR
148 Store the generated files under directory tree DIR while keeping the
149 original directory structure relative to DIR.
150 Default directory: "$STD_DEST"
151 -f, --force
152 Overwrite existing files.
153 -h, --help
154 Show this help.
155 -n, --dry-run
156 Don't do anything, only print what would happen.
157 -q, --quiet
158 Be more quiet. Can be repeated to increase silence.
159 -v, --verbose
160 Increase level of verbosity. Can be repeated.
161 --version
162 Print version information.
165 exit($Retval);
168 sub msg {
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");
175 return;
178 __END__
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
183 # version.
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
191 # this program.
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 :