std/c/README.md: Add section skeletons and don't indent lists
[sunny256-utils.git] / wav_to_flac
blob268ccf23ee9775e6e168ca4e5c4ea740f1408181
1 #!/usr/bin/env perl
3 #==============================================================================
4 # wav_to_flac
5 # File ID: 4507db62-f744-11dd-bbaa-000475e441b9
7 # [Description]
9 # Character set: UTF-8
10 # ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
12 # legal stuff.
13 #==============================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'album' => "",
24 'artist' => "",
25 'force' => 0,
26 'genre' => "",
27 'help' => 0,
28 'quiet' => 0,
29 'releasedate' => "",
30 'title' => "",
31 'tracknumber' => "",
32 'verbose' => 0,
33 'version' => 0,
37 our $progname = $0;
38 $progname =~ s/^.*\/(.*?)$/$1/;
39 our $VERSION = '0.1.0';
41 Getopt::Long::Configure('bundling');
42 GetOptions(
44 'album|a=s' => \$Opt{'album'},
45 'artist|A=s' => \$Opt{'artist'},
46 'force|f' => \$Opt{'force'},
47 'genre|g=s' => \$Opt{'genre'},
48 'help|h' => \$Opt{'help'},
49 'quiet|q+' => \$Opt{'quiet'},
50 'releasedate|r=s' => \$Opt{'releasedate'},
51 'title|t=s' => \$Opt{'title'},
52 'tracknumber|n=s' => \$Opt{'tracknumber'},
53 'verbose|v+' => \$Opt{'verbose'},
54 'version' => \$Opt{'version'},
56 ) || die("$progname: Option error. Use -h for help.\n");
58 $Opt{'verbose'} -= $Opt{'quiet'};
59 $Opt{'help'} && usage(0);
60 if ($Opt{'version'}) {
61 print_version();
62 exit(0);
65 exit(main());
67 sub main {
68 my $Retval = 0;
70 my $Lh = "[0-9a-fA-F]";
71 my $v1_templ = "$Lh\{8}-$Lh\{4}-1$Lh\{3}-$Lh\{4}-$Lh\{12}";
73 my $wav_file;
75 if (defined($ARGV[0])) {
76 $wav_file = $ARGV[0];
77 } else {
78 die("$progname: No .wav file specified\n");
81 (!-r $wav_file) &&
82 die("$progname: $wav_file: Cannot read file: $!\n");
84 (-d $wav_file) &&
85 die("$progname: $wav_file: Is a directory\n");
87 my $out_file = $wav_file;
88 $out_file =~ s/^(.*)\.wav$/$1.flac/i;
90 if (-e $out_file) {
91 if ($Opt{'force'}) {
92 unlink($out_file) || die("$progname: $out_file: " .
93 "Cannot delete file: $!\n");
94 } else {
95 die("$progname: $out_file: File already exists, " .
96 "use -f / --force to overwrite\n");
100 my $flac_version = `flac --version`;
101 $flac_version =~ s/^\s+//;
102 $flac_version =~ s/\s+$//;
104 my @Params = "flac";
106 length($Opt{'album'}) &&
107 push(@Params, tag_string("ALBUM", $Opt{'album'}));
108 length($Opt{'artist'}) &&
109 push(@Params, tag_string("ARTIST", $Opt{'artist'}));
111 my $esc_wav_file = shell_escape($wav_file);
112 my $suuid_cmd = "suuid -m -t encode -w eo " .
113 "-c \"$progname $esc_wav_file - $flac_version\"";
114 chomp(my $suuid_str = `$suuid_cmd`);
115 if (!defined($suuid_str) || $suuid_str !~ /^$v1_templ$/) {
116 die("$progname: suuid error\n");
118 push(@Params, "-TENC_ID=$suuid_str");
119 push(@Params, "-TENCODED_WITH=$flac_version");
121 length($Opt{'genre'}) &&
122 push(@Params, tag_string("GENRE", $Opt{'genre'}));
123 length($Opt{'releasedate'}) &&
124 push(@Params, tag_string("RELEASEDATE", $Opt{'releasedate'}));
125 length($Opt{'title'}) &&
126 push(@Params, tag_string("TITLE", $Opt{'title'}));
127 length($Opt{'tracknumber'}) &&
128 push(@Params, tag_string("TRACKNUMBER", $Opt{'tracknumber'}));
130 push(@Params, sprintf("%s", $wav_file));
132 msg(1, join(" ", @Params));
133 system(@Params);
135 return $Retval;
138 sub tag_string {
139 # Return parameter for use with flac(1)
140 my ($Label, $Val) = @_;
141 my $Retval = "-T$Label=$Val";
142 return($Retval);
145 sub shell_escape {
146 my $Txt = shift;
147 $Txt =~ s/"/\\"/g;
148 $Txt =~ s/\$/\\\$/g;
149 $Txt =~ s/!/\\!/g;
150 $Txt =~ s/`/\\`/g;
151 return($Txt);
154 sub print_version {
155 # Print program version
156 print("$progname $VERSION\n");
157 return;
160 sub usage {
161 # Send the help message to stdout
162 my $Retval = shift;
164 if ($Opt{'verbose'}) {
165 print("\n");
166 print_version();
168 print(<<"END");
170 Usage: $progname [options] wav_file
172 Convert .wav to .flac .
174 Options:
176 -a X, --album X
177 Name of music album.
178 -A X, --artist X
179 Name of artist/group.
180 -f, --force
181 Overwrite existing files.
182 -g X, --genre X
183 Music genre.
184 -h, --help
185 Show this help.
186 -n X, --tracknumber X
187 Track number of this song.
188 -q, --quiet
189 Be more quiet. Can be repeated to increase silence.
190 -r X, --releasedate X
191 The date the album was released.
192 -t X, --title X
193 Title of song.
194 -v, --verbose
195 Increase level of verbosity. Can be repeated.
196 --version
197 Print version information.
200 exit($Retval);
203 sub msg {
204 # Print a status message to stderr based on verbosity level
205 my ($verbose_level, $Txt) = @_;
207 if ($Opt{'verbose'} >= $verbose_level) {
208 print(STDERR "$progname: $Txt\n");
210 return;
213 __END__
215 # This program is free software; you can redistribute it and/or modify it under
216 # the terms of the GNU General Public License as published by the Free Software
217 # Foundation; either version 2 of the License, or (at your option) any later
218 # version.
220 # This program is distributed in the hope that it will be useful, but WITHOUT
221 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
222 # FOR A PARTICULAR PURPOSE.
223 # See the GNU General Public License for more details.
225 # You should have received a copy of the GNU General Public License along with
226 # this program.
227 # If not, see L<http://www.gnu.org/licenses/>.
229 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :