move point to raise exception
[sunny256-utils.git] / konvflac
blobb67fc8eb4aa9681f936bc02c355a62ebc3c1a6ba
1 #!/usr/bin/env bash
3 #=======================================================================
4 # konvflac
5 # File ID: dde2fab8-3dfd-11e0-9b12-1b57187fd9d3
7 # Convert .wav files to .flac and keep metadata
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=konvflac
14 VERSION=0.2.0
16 CMD_FLAC=flac
18 ARGS="$(getopt -o "\
22 " -l "\
23 help,\
24 quiet,\
25 verbose,\
26 version,\
27 " -n "$progname" -- "$@")"
28 test "$?" = "0" || exit 1
29 eval set -- "$ARGS"
31 opt_help=0
32 opt_quiet=0
33 opt_verbose=0
34 while :; do
35 case "$1" in
36 -h|--help) opt_help=1; shift ;;
37 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
38 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
39 --version) echo $progname $VERSION; exit 0 ;;
40 --) shift; break ;;
41 *) echo $progname: Internal error >&2; exit 1 ;;
42 esac
43 done
44 opt_verbose=$(($opt_verbose - $opt_quiet))
46 if test "$opt_help" = "1"; then
47 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
48 cat <<END
50 Convert .wav files to .flac and keep all metadata + verify that the file
51 uncompresses to an identical .wav file. If the tiniest difference is
52 found, the file is skipped and an error message is written to
53 konvflac-error.txt .
55 Usage: $progname [options] wav_file [...]
57 Options:
59 -h, --help
60 Show this help.
61 -q, --quiet
62 Be more quiet. Can be repeated to increase silence.
63 -v, --verbose
64 Increase level of verbosity. Can be repeated.
65 --version
66 Print version information.
68 END
69 exit 0
72 if test -z "$1"; then
73 echo $progname: No files specified >&2
74 exit 1
77 tmpuuid=$(suuid -m -t c_konvflac -wa -c "Starting conversion")
78 test -z "$tmpuuid" && { echo "konvflac: Broken suuid(1)" >&2; exit 1; }
79 chkfile="/tmp/konvflac.$tmpuuid.tmp"
80 for curr in "$@"; do
81 echo
82 echo ======== $(ls -l "$curr")
83 rm -f "$chkfile"
84 noext="$(basename "$curr" .wav)"
85 wavfile="$noext.wav"
86 flacfile="$noext.flac"
87 test -e "$flacfile" && {
88 echo $flacfile: File already exists >&2
89 continue
91 wavsha=$(sha1sum "$wavfile" | cut -f1 -d ' ')
92 wavid=$(finduuid "$wavfile")
93 wavid_str=
94 test -n "$wavid" && wavid_str=" (File ID $wavid)"
95 # FIXME: Kludgy grep
96 $CMD_FLAC -T FILEID=$(
97 suuid -m -t encode,c_konvflac -wa \
98 -c "Converting $wavfile$wavid_str to flac"
99 ) --keep-foreign-metadata "$wavfile" 2>&1 |
100 grep -v \
101 -e "keep-foreign-metadata is a new feature" \
102 -e "2000,2001,2002,2003,2004,2005,2006,2007" \
103 -e "2000-2009, 2011-2013" \
104 -e "flac comes with ABSOLUTELY NO WARRANTY" \
105 -e "welcome to redistribute it under certain conditions" \
106 -e '^$'
107 $CMD_FLAC -d -o "$chkfile" --keep-foreign-metadata "$flacfile" 2>&1 |
108 grep -v \
109 -e "keep-foreign-metadata is a new feature" \
110 -e "2000,2001,2002,2003,2004,2005,2006,2007" \
111 -e "2000-2009, 2011-2013" \
112 -e "flac comes with ABSOLUTELY NO WARRANTY" \
113 -e "welcome to redistribute it under certain conditions" \
114 -e '^$'
115 flacsha=$(sha1sum "$chkfile" | cut -f 1 -d ' ')
116 if test "$wavsha" = "$flacsha"; then
117 echo Verification OK
118 rm -vf "$wavfile"
119 else
120 echo ==== ENCODING ERROR ====
121 echo "$noext" >>konvflac-error.txt
123 done