summaryrefslogtreecommitdiffstats
path: root/drg2flac.sh
blob: e01bc07f85cbfca2f0c184ed426010cffa1e178d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
# vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :

# Copyright (C) 2012 Florian Bruhin <drg2flac@the-compiler.org>

# drg2flac is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# drg2flac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with drg2flac  If not, see <http://www.gnu.org/licenses/>.
cleanup() {
    [[ "$tmp" ]] && rm -rf "$tmp"
}
trap cleanup EXIT


# Check if the tools we need exist
for bin in convert drg2sbg sbagen flac tput; do
    if ! which "$bin" &>/dev/null; then
        echo "Error: the tool \"$bin\" is needed for this script to work, but"
        echo "is not installed."
        exit 1
    fi
done

# Colors!
cred="$(tput setaf 1)"
cres="$(tput sgr0)"
cgreen="$(tput setaf 2)"
cblue="$(tput setaf 4)"
cbold="$(tput bold)"


usage() {
    cat << EOT
drg2flac - Convert I-Doser drg files to flac
Copyright (C) 2012 Florian Bruhin <drg2flac@the-compiler.org>

Usage: $0 [options] file [file ...]

Options:
  -h/--help               Displays this help
  -o/--output <file>      Use <file> as output file (can only be used
                          when there is only one input file)
  -O/--options <options>  Options to pass to flac
  -a/--artist <artist>    Artist to set in the FLAC tags.
                          Defaults to "I-Doser"
EOT
}

error() {
    echo "${cred}An error occured while $2${cres}" >&2
    echo "${cred}Skipping file $1${cres}" >&2
}

# Parse options
while :; do
    case $1 in
        -h|--help)
            usage
            exit 0
            ;;
        -o|--output)
            if [[ -z "$2" || "$2" == -* ]]; then
                usage
                exit 1
            fi
            outfile="$2"
            shift 2
            ;;
        -O|--options)
            flacopts="$2"
            shift 2
            ;;
        -a|--artist)
            if [[ -z "$2" || "$2" == -* ]]; then
                usage
                exit 1
            fi
            artist="$2"
            shift 2
            ;;
        --) # end of options
            shift
            break
            ;;
        -*) # unknown option
            usage
            exit 1
            ;;
        *) # no more options
            break
            ;;
    esac
done
if (($# == 0)); then
    echo "${cred}Error:${cres} No input file given." >&2
    echo
    usage
    exit 1
fi
if [[ "$outfile" && $# > 1 ]]; then
    echo "${cred}Error:${cres} --outfile may only be specified with one input file." >&2
    exit 1
fi

tmp=$(mktemp -d)

# Convert
for infile in "$@"; do
    flacargs=()
    echo "${cbold}===== $infile =====${cres}"
    echo
    echo "${cblue}Getting metadata${cres}"

    [[ -z "$artist" ]] && artist="I-Doser"
    flacargs+=("-T" "ARTIST=$artist")

    title=$(drg2sbg -r 2 "$infile")
    if (( $? != 0 )); then
        error "$infile" "getting the title"
        continue
    else
        flacargs+=("-T" "TITLE=$title")
    fi

    desc=$(drg2sbg -r 4 "$infile")
    if (( $? != 0 )); then
        error "$infile" "getting the description"
        continue
    fi

    drg2sbg -r 3 -o "$tmp/image" "$infile"
    if (( $? != 0 )); then
        error "$infile" "getting the image"
    else
        echo
        echo "${cblue}Converting image${cres}"
        convert "$tmp/image" "$tmp/image.png"
        if (( $? != 0 )); then
            error "$infile" "converting the image"
        else
            flacargs+=("--picture=$tmp/image.png")
        fi
    fi

    flacargs+=("-T" "ENCODING=converted by drg3flac using flac -8 --no-mid-side")
    flacargs+=("-8")
    flacargs+=("--no-mid-side")
    [[ -z "$outfile" ]] && outfile="${infile%.*}.flac"
    flacargs+=("-o" "$outfile")
    flacargs+=("$tmp/wav")

    echo
    echo "${cblue}Converting drg to sbg${cres}"
    drg2sbg -o "$tmp/sbg" "$infile"
    (( $? != 0 )) && { error "$infile" "converting drg to sbg"; continue ;}

    echo
    echo "${cblue}Converting sbg to wav${cres}"
    sbagen -W -o "$tmp/wav" "$tmp/sbg"
    (( $? != 0 )) && { error "$infile" "converting sbg to wav"; continue ;}

    echo
    echo
    echo "${cblue}Converting wav to flac${cres}"
    flac "${flacargs[@]}"
    (( $? != 0 )) && { error "$infile" "converting wav to flac"; continue ;}

    echo "${cgreen}Done!${cres}"
done