Unified · Split
| @@ -44,13 +44,14 @@ eval $(cat "${CONFIG}" 2>/dev/null) · 43 unmodified lines | ||
| 44 | 44 | DRY=0 |
| 45 | 45 | SAVE=0 |
| 46 | 46 | unset PIC |
| 47 | −unset FILE | |
| 47 | +unset INPATH | |
| 48 | 48 | unset CUE |
| 49 | 49 | unset CHARSET |
| 50 | 50 | FORCE=0 |
| 51 | 51 | |
| 52 | 52 | cR="\033[31m" |
| 53 | 53 | cG="\033[32m" |
| 54 | +cC="\033[35m" | |
| 54 | 55 | cP="\033[36m" |
| 55 | 56 | cU="\033[4m" |
| 56 | 57 | cZ="\033[0m" |
| @@ -58,11 +59,12 @@ cZ="\033[0m" · 1 unmodified line | ||
| 58 | 59 | HELP="${cG}split2flac splits one big ${cU}APE/FLAC/WV$cZ$cG file to ${cU}FLAC/MP3/OGG_VORBIS$cZ$cG tracks with tagging and renaming. |
| 59 | 60 | |
| 60 | 61 | Usage: ${cZ}split2${FORMAT}.sh [${cU}OPTIONS$cZ] ${cU}FILE$cZ [${cU}OPTIONS$cZ]$cZ |
| 62 | + ${cZ}split2${FORMAT}.sh [${cU}OPTIONS$cZ] ${cU}DIR$cZ [${cU}OPTIONS$cZ]$cZ | |
| 61 | 63 | $cG-p$cZ - dry run |
| 62 | 64 | $cG-o ${cU}DIRECTORY$cZ $cR*$cZ - set output directory |
| 63 | − $cG-cue ${cU}FILE$cZ - use file as a cue sheet | |
| 65 | + $cG-cue ${cU}FILE$cZ - use file as a cue sheet (does not work with ${cU}DIR$cZ) | |
| 64 | 66 | $cG-f ${cU}FORMAT$cZ - use specified output format $cP(current is ${FORMAT})$cZ |
| 65 | − $cG-c ${cU}FILE$cZ $cR*$cZ - use file as a cover image | |
| 67 | + $cG-c ${cU}FILE$cZ $cR*$cZ - use file as a cover image (does not work with ${cU}DIR$cZ) | |
| 66 | 68 | $cG-cuecharset ${cU}CHARSET$cZ - convert cue sheet from CHARSET to UTF-8 (no conversion by default) |
| 67 | 69 | $cG-nc ${cR}*$cZ - do not set any cover images |
| 68 | 70 | $cG-cs ${cU}WxH$cZ $cR*$cZ - set cover image size $cP(current is ${PIC_SIZE})$cZ |
| @@ -88,9 +90,8 @@ Script will try to find CUE sheet if it wasn't specified. It also supports inter · 19 unmodified lines | ||
| 88 | 90 | |
| 89 | 91 | msg="echo -e" |
| 90 | 92 | |
| 91 | −fatal ( ) { | |
| 93 | +emsg ( ) { | |
| 92 | 94 | $msg "${cR}$1${cZ}" |
| 93 | − exit 1 | |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | # parse arguments |
| @@ -113,15 +114,17 @@ while [ "$1" ]; do · 16 unmodified lines | ||
| 113 | 114 | -F) FORCE=1;; |
| 114 | 115 | -s) SAVE=1;; |
| 115 | 116 | -h|--help|-help) eval "$msg \"${HELP}\""; exit 0;; |
| 116 | − -*) eval "$msg \"${HELP}\""; fatal "\nUnknown option $1";; | |
| 117 | + -*) eval "$msg \"${HELP}\""; emsg "\nUnknown option $1"; exit 1;; | |
| 117 | 118 | *) |
| 118 | − if [ -r "${FILE}" ]; then | |
| 119 | + if [ -n "${INPATH}" ]; then | |
| 119 | 120 | eval "$msg \"${HELP}\"" |
| 120 | − fatal "\nUnknown option $1" | |
| 121 | + emsg "\nUnknown option $1" | |
| 122 | + exit 1 | |
| 121 | 123 | elif [ ! -r "$1" ]; then |
| 122 | − fatal "Unable to read $1" | |
| 124 | + emsg "Unable to read $1" | |
| 125 | + exit 1 | |
| 123 | 126 | else |
| 124 | − FILE="$1" | |
| 127 | + INPATH="$1" | |
| 125 | 128 | fi;; |
| 126 | 129 | esac |
| 127 | 130 | shift |
| @@ -131,289 +134,350 @@ METAFLAC="metaflac --no-utf8-convert" · 3 unmodified lines | ||
| 131 | 134 | VORBISCOMMENT="vorbiscomment -R -a" |
| 132 | 135 | ID3TAG="id3tag -2" |
| 133 | 136 | |
| 134 | −# print input filename | |
| 135 | −$msg "${cG}Input file :$cZ ${FILE:?No input filename given. Use -h for help.}$cZ" | |
| 136 | − | |
| 137 | 137 | # check & print output format |
| 138 | 138 | msg_format="${cG}Output format :$cZ" |
| 139 | 139 | case ${FORMAT} in |
| 140 | 140 | flac) $msg "$msg_format FLAC";; |
| 141 | 141 | mp3) $msg "$msg_format MP3";; |
| 142 | 142 | ogg) $msg "$msg_format OGG VORBIS";; |
| 143 | − *) fatal "Unknown output format \"${FORMAT}\"";; | |
| 143 | + *) emsg "Unknown output format \"${FORMAT}\""; exit 1;; | |
| 144 | 144 | esac |
| 145 | 145 | |
| 146 | −# search for a cue sheet if not specified | |
| 147 | −if [ -z "${CUE}" ]; then | |
| 148 | − CUE="${FILE}.cue" | |
| 149 | − if [ ! -r "${CUE}" ]; then | |
| 150 | − CUE="${FILE%.*}.cue" | |
| 146 | +# splits a file | |
| 147 | +split_file ( ) { | |
| 148 | + FILE="$1" | |
| 149 | + | |
| 150 | + if [ ! -r "${FILE}" ]; then | |
| 151 | + emsg "Can not read the file" | |
| 152 | + return 1 | |
| 153 | + fi | |
| 154 | + | |
| 155 | + # search for a cue sheet if not specified | |
| 156 | + if [ -z "${CUE}" ]; then | |
| 157 | + CUE="${FILE}.cue" | |
| 151 | 158 | if [ ! -r "${CUE}" ]; then |
| 152 | − # try to extract internal one | |
| 153 | − CUESHEET=$(${METAFLAC} --show-tag=CUESHEET "${FILE}" 2>/dev/null | sed 's/cuesheet=//;s/CUESHEET=//') | |
| 159 | + CUE="${FILE%.*}.cue" | |
| 160 | + if [ ! -r "${CUE}" ]; then | |
| 161 | + # try to extract internal one | |
| 162 | + CUESHEET=$(${METAFLAC} --show-tag=CUESHEET "${FILE}" 2>/dev/null | sed 's/cuesheet=//;s/CUESHEET=//') | |
| 154 | 163 | |
| 155 | − if [ -z "${CUESHEET}" ]; then | |
| 156 | − CUESHEET=$(wvunpack -q -c "${FILE}" 2>/dev/null) | |
| 157 | − fi | |
| 164 | + if [ -z "${CUESHEET}" ]; then | |
| 165 | + CUESHEET=$(wvunpack -q -c "${FILE}" 2>/dev/null) | |
| 166 | + fi | |
| 158 | 167 | |
| 159 | − if [ "${CUESHEET}" ]; then | |
| 160 | − CUE="${TMPCUE}" | |
| 161 | − echo "${CUESHEET}" > "${CUE}" | |
| 168 | + if [ "${CUESHEET}" ]; then | |
| 169 | + CUE="${TMPCUE}" | |
| 170 | + echo "${CUESHEET}" > "${CUE}" | |
| 162 | 171 | |
| 163 | − if [ $? -ne 0 ]; then | |
| 164 | − fatal "Unable to save internal cue sheet" | |
| 172 | + if [ $? -ne 0 ]; then | |
| 173 | + emsg "Unable to save internal cue sheet" | |
| 174 | + return 1 | |
| 175 | + fi | |
| 176 | + else | |
| 177 | + unset CUE | |
| 165 | 178 | fi |
| 166 | − else | |
| 167 | − unset CUE | |
| 168 | 179 | fi |
| 169 | 180 | fi |
| 170 | 181 | fi |
| 171 | −fi | |
| 172 | − | |
| 173 | −# print cue sheet filename | |
| 174 | −$msg "${cG}Cue sheet :$cZ ${CUE:?No cue sheet}$cZ" | |
| 175 | 182 | |
| 176 | −if [ -n "${CHARSET}" ]; then | |
| 177 | − $msg "${cG}Cue charset : $cP${CHARSET} -> utf-8$cZ" | |
| 178 | − CUESHEET=$(iconv -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) | |
| 179 | − if [ $? -ne 0 ]; then | |
| 180 | − fatal "Unable to convert cue sheet from ${CHARSET} to utf-8" | |
| 183 | + # print cue sheet filename | |
| 184 | + if [ -z "${CUE}" ]; then | |
| 185 | + emsg "No cue sheet" | |
| 186 | + return 1 | |
| 181 | 187 | fi |
| 182 | − CUE="${TMPCUE}" | |
| 183 | − echo "${CUESHEET}" > "${CUE}" | |
| 184 | 188 | |
| 185 | − if [ $? -ne 0 ]; then | |
| 186 | − fatal "Unable to save converted cue sheet" | |
| 189 | + if [ -n "${CHARSET}" ]; then | |
| 190 | + $msg "${cG}Cue charset : $cP${CHARSET} -> utf-8$cZ" | |
| 191 | + CUESHEET=$(iconv -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) | |
| 192 | + if [ $? -ne 0 ]; then | |
| 193 | + emsg "Unable to convert cue sheet from ${CHARSET} to utf-8" | |
| 194 | + return 1 | |
| 195 | + fi | |
| 196 | + CUE="${TMPCUE}" | |
| 197 | + echo "${CUESHEET}" > "${CUE}" | |
| 198 | + | |
| 199 | + if [ $? -ne 0 ]; then | |
| 200 | + emsg "Unable to save converted cue sheet" | |
| 201 | + return 1 | |
| 202 | + fi | |
| 187 | 203 | fi |
| 188 | −fi | |
| 189 | 204 | |
| 190 | −# search for a front cover image | |
| 191 | −if [ ${NOPIC} -eq 1 ]; then | |
| 192 | − unset PIC | |
| 193 | −elif [ -z "${PIC}" ]; then | |
| 194 | − # try common names | |
| 195 | − SDIR=$(dirname "${FILE}") | |
| 205 | + # search for a front cover image | |
| 206 | + if [ ${NOPIC} -eq 1 ]; then | |
| 207 | + unset PIC | |
| 208 | + elif [ -z "${PIC}" ]; then | |
| 209 | + # try common names | |
| 210 | + SDIR=$(dirname "${FILE}") | |
| 211 | + | |
| 212 | + for i in cover.jpg front_cover.jpg folder.jpg; do | |
| 213 | + if [ -r "${SDIR}/$i" ]; then | |
| 214 | + PIC="${SDIR}/$i" | |
| 215 | + break | |
| 216 | + fi | |
| 217 | + done | |
| 196 | 218 | |
| 197 | − for i in cover.jpg front_cover.jpg folder.jpg; do | |
| 198 | − if [ -r "${SDIR}/$i" ]; then | |
| 199 | − PIC="${SDIR}/$i" | |
| 200 | − break | |
| 219 | + # try to extract internal one | |
| 220 | + if [ -z "${PIC}" ]; then | |
| 221 | + ${METAFLAC} --export-picture-to="${TMPPIC}" "${FILE}" 2>/dev/null | |
| 222 | + if [ $? -ne 0 ]; then | |
| 223 | + unset PIC | |
| 224 | + else | |
| 225 | + PIC="${TMPPIC}" | |
| 226 | + fi | |
| 201 | 227 | fi |
| 202 | − done | |
| 228 | + fi | |
| 203 | 229 | |
| 204 | − # try to extract internal one | |
| 205 | − if [ -z "${PIC}" ]; then | |
| 206 | − ${METAFLAC} --export-picture-to="${TMPPIC}" "${FILE}" 2>/dev/null | |
| 207 | − if [ $? -ne 0 ]; then | |
| 208 | − unset PIC | |
| 230 | + $msg "${cG}Cover image :$cZ ${PIC:-not set}" | |
| 231 | + $msg "${cG}Output dir :$cZ ${DIR:?Output directory was not set}" | |
| 232 | + | |
| 233 | + # file removal warning | |
| 234 | + if [ ${REMOVE} -eq 1 ]; then | |
| 235 | + msg_removal="\n${cR}Also remove original" | |
| 236 | + if [ ${FORCE} -eq 1 ]; then | |
| 237 | + $msg "$msg_removal (WITHOUT ASKING)$cZ" | |
| 209 | 238 | else |
| 210 | − PIC="${TMPPIC}" | |
| 239 | + $msg "$msg_removal if user says 'y'$cZ" | |
| 211 | 240 | fi |
| 212 | 241 | fi |
| 213 | −fi | |
| 214 | − | |
| 215 | −$msg "${cG}Cover image :$cZ ${PIC:-not set}" | |
| 216 | −$msg "${cG}Output dir :$cZ ${DIR:?Output directory was not set}" | |
| 217 | 242 | |
| 218 | −# file removal warning | |
| 219 | −if [ ${REMOVE} -eq 1 ]; then | |
| 220 | − msg_removal="\n${cR}Also remove original" | |
| 221 | − if [ ${FORCE} -eq 1 ]; then | |
| 222 | − $msg "$msg_removal (WITHOUT ASKING)$cZ" | |
| 223 | − else | |
| 224 | − $msg "$msg_removal if user says 'y'$cZ" | |
| 243 | + # save configuration if needed | |
| 244 | + if [ ${SAVE} -eq 1 ]; then | |
| 245 | + echo "DIR=\"${DIR}\"" > "${CONFIG}" | |
| 246 | + echo "NOSUBDIRS=${NOSUBDIRS}" >> "${CONFIG}" | |
| 247 | + echo "NORENAME=${NORENAME}" >> "${CONFIG}" | |
| 248 | + echo "NOPIC=${NOPIC}" >> "${CONFIG}" | |
| 249 | + echo "REMOVE=${REMOVE}" >> "${CONFIG}" | |
| 250 | + echo "PIC_SIZE=${PIC_SIZE}" >> "${CONFIG}" | |
| 251 | + $msg "${cP}Configuration saved$cZ" | |
| 225 | 252 | fi |
| 226 | −fi | |
| 227 | 253 | |
| 228 | −# save configuration if needed | |
| 229 | −if [ ${SAVE} -eq 1 ]; then | |
| 230 | − echo "DIR=\"${DIR}\"" > "${CONFIG}" | |
| 231 | − echo "NOSUBDIRS=${NOSUBDIRS}" >> "${CONFIG}" | |
| 232 | − echo "NORENAME=${NORENAME}" >> "${CONFIG}" | |
| 233 | − echo "NOPIC=${NOPIC}" >> "${CONFIG}" | |
| 234 | − echo "REMOVE=${REMOVE}" >> "${CONFIG}" | |
| 235 | − echo "PIC_SIZE=${PIC_SIZE}" >> "${CONFIG}" | |
| 236 | − $msg "${cP}Configuration saved$cZ" | |
| 237 | −fi | |
| 254 | + GETTAG="cueprint -n 1 -t" | |
| 255 | + VALIDATE="sed s/[^-[:space:][:alnum:]&_#,.'\"]//g" | |
| 238 | 256 | |
| 239 | −GETTAG="cueprint -n 1 -t" | |
| 240 | −VALIDATE="sed s/[^-[:space:][:alnum:]&_#,.'\"]//g" | |
| 257 | + # get common tags | |
| 258 | + TAG_ARTIST=$(${GETTAG} %P "${CUE}") | |
| 259 | + TAG_ALBUM=$(${GETTAG} %T "${CUE}") | |
| 260 | + TRACKS_NUM=$(${GETTAG} %N "${CUE}") | |
| 241 | 261 | |
| 242 | −# get common tags | |
| 243 | −TAG_ARTIST=$(${GETTAG} %P "${CUE}") | |
| 244 | −TAG_ALBUM=$(${GETTAG} %T "${CUE}") | |
| 245 | −TRACKS_NUM=$(${GETTAG} %N "${CUE}") | |
| 262 | + YEAR=$(awk '{ if (/REM[ \t]+DATE/) { printf "%i", $3; exit } }' < "${CUE}") | |
| 263 | + YEAR=$(echo ${YEAR} | tr -d -C '[:digit:]') | |
| 246 | 264 | |
| 247 | −YEAR=$(awk '{ if (/REM[ \t]+DATE/) { printf "%i", $3; exit } }' < "${CUE}") | |
| 248 | −YEAR=$(echo ${YEAR} | tr -d -C '[:digit:]') | |
| 265 | + unset TAG_DATE | |
| 249 | 266 | |
| 250 | −unset TAG_DATE | |
| 267 | + if [ -n "${YEAR}" ]; then | |
| 268 | + if [ ${YEAR} -ne 0 ]; then | |
| 269 | + TAG_DATE="${YEAR}" | |
| 270 | + fi | |
| 271 | + fi | |
| 251 | 272 | |
| 252 | −if [ -n "${YEAR}" ]; then | |
| 253 | − if [ ${YEAR} -ne 0 ]; then | |
| 254 | − TAG_DATE="${YEAR}" | |
| 273 | + $msg "\n${cG}Artist :$cZ ${TAG_ARTIST}" | |
| 274 | + $msg "${cG}Album :$cZ ${TAG_ALBUM}" | |
| 275 | + if [ -n "${TAG_DATE}" ]; then | |
| 276 | + $msg "${cG}Year :$cZ ${TAG_DATE}" | |
| 255 | 277 | fi |
| 256 | −fi | |
| 278 | + $msg "${cG}Tracks :$cZ ${TRACKS_NUM}\n" | |
| 257 | 279 | |
| 258 | −$msg "\n${cG}Artist :$cZ ${TAG_ARTIST}" | |
| 259 | −$msg "${cG}Album :$cZ ${TAG_ALBUM}" | |
| 260 | −if [ -n "${TAG_DATE}" ]; then | |
| 261 | − $msg "${cG}Year :$cZ ${TAG_DATE}" | |
| 262 | −fi | |
| 263 | −$msg "${cG}Tracks :$cZ ${TRACKS_NUM}\n" | |
| 280 | + # prepare output directory | |
| 281 | + OUT="${DIR}" | |
| 264 | 282 | |
| 265 | −# prepare output directory | |
| 266 | −OUT="${DIR}" | |
| 283 | + if [ ${NOSUBDIRS} -ne 1 ]; then | |
| 284 | + DIR_ARTIST=$(echo ${TAG_ARTIST} | ${VALIDATE}) | |
| 285 | + DIR_ALBUM=$(echo ${TAG_ALBUM} | ${VALIDATE}) | |
| 267 | 286 | |
| 268 | −if [ ${NOSUBDIRS} -ne 1 ]; then | |
| 269 | − DIR_ARTIST=$(echo ${TAG_ARTIST} | ${VALIDATE}) | |
| 270 | − DIR_ALBUM=$(echo ${TAG_ALBUM} | ${VALIDATE}) | |
| 287 | + if [ "${TAG_DATE}" ]; then | |
| 288 | + DIR_ALBUM="${TAG_DATE} - ${DIR_ALBUM}" | |
| 289 | + fi | |
| 271 | 290 | |
| 272 | − if [ "${TAG_DATE}" ]; then | |
| 273 | − DIR_ALBUM="${TAG_DATE} - ${DIR_ALBUM}" | |
| 291 | + OUT="${OUT}/${DIR_ARTIST}/${DIR_ALBUM}" | |
| 274 | 292 | fi |
| 275 | 293 | |
| 276 | − OUT="${OUT}/${DIR_ARTIST}/${DIR_ALBUM}" | |
| 277 | −fi | |
| 278 | − | |
| 279 | −$msg "${cP}Saving tracks to $cZ\"${OUT}\"" | |
| 294 | + $msg "${cP}Saving tracks to $cZ\"${OUT}\"" | |
| 280 | 295 | |
| 281 | −if [ ${DRY} -ne 1 ]; then | |
| 282 | − # create output dir | |
| 283 | − mkdir -p "${OUT}" | |
| 296 | + if [ ${DRY} -ne 1 ]; then | |
| 297 | + # create output dir | |
| 298 | + mkdir "${OUT}" | |
| 284 | 299 | |
| 285 | − if [ $? -ne 0 ]; then | |
| 286 | − fatal "Failed to create output directory" | |
| 287 | − fi | |
| 300 | + if [ $? -ne 0 -a ${NOSUBDIRS} -ne 1 ]; then | |
| 301 | + emsg "Failed to create output directory (already splitted?)" | |
| 302 | + return 1 | |
| 303 | + fi | |
| 288 | 304 | |
| 289 | − case ${FORMAT} in | |
| 290 | − flac) ENC="flac flac -8 - -o %f";; | |
| 291 | − mp3) ENC="cust ext=mp3 lame --preset extreme - %f";; | |
| 292 | − ogg) ENC="cust ext=ogg oggenc -q 10 - -o %f";; | |
| 293 | − *) fatal "Unknown output format ${FORMAT}";; | |
| 294 | − esac | |
| 305 | + case ${FORMAT} in | |
| 306 | + flac) ENC="flac flac -8 - -o %f";; | |
| 307 | + mp3) ENC="cust ext=mp3 lame --preset extreme - %f";; | |
| 308 | + ogg) ENC="cust ext=ogg oggenc -q 10 - -o %f";; | |
| 309 | + *) emsg "Unknown output format ${FORMAT}"; exit 1;; | |
| 310 | + esac | |
| 295 | 311 | |
| 296 | − # split to tracks | |
| 297 | − cuebreakpoints "${CUE}" | \ | |
| 298 | − shnsplit -O never -o "${ENC}" -d "${OUT}" -t "%n" "${FILE}" | |
| 299 | − if [ $? -ne 0 ]; then | |
| 300 | − fatal "Failed to split" | |
| 301 | − fi | |
| 312 | + # split to tracks | |
| 313 | + cuebreakpoints "${CUE}" | \ | |
| 314 | + shnsplit -O never -o "${ENC}" -d "${OUT}" -t "%n" "${FILE}" | |
| 315 | + if [ $? -ne 0 ]; then | |
| 316 | + emsg "Failed to split" | |
| 317 | + return 1 | |
| 318 | + fi | |
| 302 | 319 | |
| 303 | − # prepare cover image | |
| 304 | − if [ "${PIC}" ]; then | |
| 305 | − convert "${PIC}" -resize "${PIC_SIZE}" "${TMPPIC}" | |
| 306 | − if [ $? -eq 0 ]; then | |
| 307 | − PIC="${TMPPIC}" | |
| 308 | − else | |
| 309 | − $msg "${cR}Failed to convert cover image$cZ" | |
| 310 | − unset PIC | |
| 320 | + # prepare cover image | |
| 321 | + if [ "${PIC}" ]; then | |
| 322 | + convert "${PIC}" -resize "${PIC_SIZE}" "${TMPPIC}" | |
| 323 | + if [ $? -eq 0 ]; then | |
| 324 | + PIC="${TMPPIC}" | |
| 325 | + else | |
| 326 | + $msg "${cR}Failed to convert cover image$cZ" | |
| 327 | + unset PIC | |
| 328 | + fi | |
| 311 | 329 | fi |
| 312 | 330 | fi |
| 313 | −fi | |
| 314 | 331 | |
| 315 | −# set tags and rename | |
| 316 | −$msg "\n${cP}Setting tags$cZ" | |
| 332 | + # set tags and rename | |
| 333 | + $msg "\n${cP}Setting tags$cZ" | |
| 317 | 334 | |
| 318 | −i=1 | |
| 319 | −while [ $i -le ${TRACKS_NUM} ]; do | |
| 320 | − TAG_TITLE=$(cueprint -n $i -t %t "${CUE}") | |
| 321 | − FILE_TRACK="$(printf %02i $i)" | |
| 322 | − FILE_TITLE=$(echo ${TAG_TITLE} | ${VALIDATE}) | |
| 323 | − f="${OUT}/${FILE_TRACK}.${FORMAT}" | |
| 335 | + i=1 | |
| 336 | + while [ $i -le ${TRACKS_NUM} ]; do | |
| 337 | + TAG_TITLE=$(cueprint -n $i -t %t "${CUE}") | |
| 338 | + FILE_TRACK="$(printf %02i $i)" | |
| 339 | + FILE_TITLE=$(echo ${TAG_TITLE} | ${VALIDATE}) | |
| 340 | + f="${OUT}/${FILE_TRACK}.${FORMAT}" | |
| 324 | 341 | |
| 325 | − $msg "$i: $cG${TAG_TITLE}$cZ" | |
| 342 | + $msg "$i: $cG${TAG_TITLE}$cZ" | |
| 343 | + | |
| 344 | + if [ ${NORENAME} -ne 1 ]; then | |
| 345 | + FINAL="${OUT}/${FILE_TRACK} - ${FILE_TITLE}.${FORMAT}" | |
| 346 | + if [ ${DRY} -ne 1 ]; then | |
| 347 | + mv "$f" "${FINAL}" | |
| 348 | + if [ $? -ne 0 ]; then | |
| 349 | + emsg "Failed to rename track file" | |
| 350 | + return 1 | |
| 351 | + fi | |
| 352 | + fi | |
| 353 | + else | |
| 354 | + FINAL="$f" | |
| 355 | + fi | |
| 326 | 356 | |
| 327 | − if [ ${NORENAME} -ne 1 ]; then | |
| 328 | − FINAL="${OUT}/${FILE_TRACK} - ${FILE_TITLE}.${FORMAT}" | |
| 329 | 357 | if [ ${DRY} -ne 1 ]; then |
| 330 | − mv "$f" "${FINAL}" | |
| 331 | − if [ $? -ne 0 ]; then | |
| 332 | − fatal "Failed to rename track file" | |
| 358 | + case ${FORMAT} in | |
| 359 | + flac) | |
| 360 | + ${METAFLAC} --remove-all-tags \ | |
| 361 | + --set-tag="ARTIST=${TAG_ARTIST}" \ | |
| 362 | + --set-tag="ALBUM=${TAG_ALBUM}" \ | |
| 363 | + --set-tag="TITLE=${TAG_TITLE}" \ | |
| 364 | + --set-tag="TRACKNUMBER=$i" \ | |
| 365 | + "${FINAL}" >/dev/null | |
| 366 | + RES=$? | |
| 367 | + | |
| 368 | + if [ -n "${TAG_DATE}" ]; then | |
| 369 | + ${METAFLAC} --set-tag="DATE=${TAG_DATE}" "${FINAL}" >/dev/null | |
| 370 | + RES=$((${RES} + $?)) | |
| 371 | + fi | |
| 372 | + | |
| 373 | + if [ -n "${PIC}" ]; then | |
| 374 | + ${METAFLAC} --import-picture-from="${PIC}" "${FINAL}" >/dev/null | |
| 375 | + RES=$((${RES} + $?)) | |
| 376 | + fi | |
| 377 | + ;; | |
| 378 | + | |
| 379 | + mp3) | |
| 380 | + ${ID3TAG} "-a${TAG_ARTIST}" \ | |
| 381 | + "-A${TAG_ALBUM}" \ | |
| 382 | + "-s${TAG_TITLE}" \ | |
| 383 | + "-t$i" \ | |
| 384 | + "-T${TRACKS_NUM}" \ | |
| 385 | + "${FINAL}" >/dev/null | |
| 386 | + RES=$? | |
| 387 | + | |
| 388 | + if [ -n "${TAG_DATE}" ]; then | |
| 389 | + ${ID3TAG} -y"${TAG_DATE}" "${FINAL}" >/dev/null | |
| 390 | + RES=$((${RES} + $?)) | |
| 391 | + fi | |
| 392 | + ;; | |
| 393 | + | |
| 394 | + ogg) | |
| 395 | + ${VORBISCOMMENT} "${FINAL}" \ | |
| 396 | + -t "ARTIST=${TAG_ARTIST}" \ | |
| 397 | + -t "ALBUM=${TAG_ALBUM}" \ | |
| 398 | + -t "TITLE=${TAG_TITLE}" \ | |
| 399 | + -t "TRACKNUMBER=$i" >/dev/null | |
| 400 | + RES=$? | |
| 401 | + | |
| 402 | + if [ -n "${TAG_DATE}" ]; then | |
| 403 | + ${VORBISCOMMENT} "${FINAL}" -t "DATE=${TAG_DATE}" >/dev/null | |
| 404 | + RES=$((${RES} + $?)) | |
| 405 | + fi | |
| 406 | + ;; | |
| 407 | + *) | |
| 408 | + emsg "Unknown output format ${FORMAT}" | |
| 409 | + return 1 | |
| 410 | + ;; | |
| 411 | + esac | |
| 412 | + | |
| 413 | + if [ ${RES} -ne 0 ]; then | |
| 414 | + emsg "Failed to set tags for track" | |
| 415 | + return 1 | |
| 333 | 416 | fi |
| 334 | 417 | fi |
| 335 | − else | |
| 336 | − FINAL="$f" | |
| 337 | − fi | |
| 338 | 418 | |
| 339 | − if [ ${DRY} -ne 1 ]; then | |
| 340 | − case ${FORMAT} in | |
| 341 | − flac) | |
| 342 | − ${METAFLAC} --remove-all-tags \ | |
| 343 | − --set-tag="ARTIST=${TAG_ARTIST}" \ | |
| 344 | − --set-tag="ALBUM=${TAG_ALBUM}" \ | |
| 345 | − --set-tag="TITLE=${TAG_TITLE}" \ | |
| 346 | − --set-tag="TRACKNUMBER=$i" \ | |
| 347 | − "${FINAL}" >/dev/null | |
| 348 | − RES=$? | |
| 349 | − | |
| 350 | − if [ -n "${TAG_DATE}" ]; then | |
| 351 | − ${METAFLAC} --set-tag="DATE=${TAG_DATE}" "${FINAL}" >/dev/null | |
| 352 | − RES=$((${RES} + $?)) | |
| 353 | − fi | |
| 419 | + $msg " -> ${cP}${FINAL}$cZ" | |
| 354 | 420 | |
| 355 | − if [ -n "${PIC}" ]; then | |
| 356 | − ${METAFLAC} --import-picture-from="${PIC}" "${FINAL}" >/dev/null | |
| 357 | − RES=$((${RES} + $?)) | |
| 358 | − fi | |
| 359 | − ;; | |
| 360 | − | |
| 361 | − mp3) | |
| 362 | − ${ID3TAG} "-a${TAG_ARTIST}" \ | |
| 363 | − "-A${TAG_ALBUM}" \ | |
| 364 | − "-s${TAG_TITLE}" \ | |
| 365 | − "-t$i" \ | |
| 366 | − "-T${TRACKS_NUM}" \ | |
| 367 | − "${FINAL}" >/dev/null | |
| 368 | − RES=$? | |
| 369 | − | |
| 370 | − if [ -n "${TAG_DATE}" ]; then | |
| 371 | − ${ID3TAG} -y"${TAG_DATE}" "${FINAL}" >/dev/null | |
| 372 | − RES=$((${RES} + $?)) | |
| 373 | − fi | |
| 374 | − ;; | |
| 375 | − | |
| 376 | − ogg) | |
| 377 | − ${VORBISCOMMENT} "${FINAL}" \ | |
| 378 | − -t "ARTIST=${TAG_ARTIST}" \ | |
| 379 | − -t "ALBUM=${TAG_ALBUM}" \ | |
| 380 | − -t "TITLE=${TAG_TITLE}" \ | |
| 381 | − -t "TRACKNUMBER=$i" >/dev/null | |
| 382 | − RES=$? | |
| 383 | − | |
| 384 | − if [ -n "${TAG_DATE}" ]; then | |
| 385 | − ${VORBISCOMMENT} "${FINAL}" -t "DATE=${TAG_DATE}" >/dev/null | |
| 386 | − RES=$((${RES} + $?)) | |
| 387 | − fi | |
| 388 | − ;; | |
| 389 | − *) | |
| 390 | − fatal "Unknown output format ${FORMAT}";; | |
| 391 | − esac | |
| 421 | + i=$(($i + 1)) | |
| 422 | + done | |
| 423 | + | |
| 424 | + rm -f "${TMPPIC}" | |
| 425 | + rm -f "${TMPCUE}" | |
| 392 | 426 | |
| 393 | − if [ ${RES} -ne 0 ]; then | |
| 394 | − fatal "Failed to set tags for track" | |
| 427 | + if [ ${DRY} -ne 1 -a ${REMOVE} -eq 1 ]; then | |
| 428 | + YEP="n" | |
| 429 | + | |
| 430 | + if [ ${FORCE} -ne 1 ]; then | |
| 431 | + echo -n "Are you sure you want to delete original? [y] >" | |
| 432 | + read YEP | |
| 433 | + fi | |
| 434 | + | |
| 435 | + if [ "${YEP}" = "y" -o ${FORCE} -eq 1 ]; then | |
| 436 | + rm -f "${FILE}" | |
| 395 | 437 | fi |
| 396 | 438 | fi |
| 397 | 439 | |
| 398 | − $msg " -> ${cP}${FINAL}$cZ" | |
| 440 | + return 0 | |
| 441 | +} | |
| 399 | 442 | |
| 400 | − i=$(($i + 1)) | |
| 401 | −done | |
| 443 | +split_collection ( ) { | |
| 444 | + while [ 1 ]; do | |
| 445 | + $(read -r FILE) | |
| 402 | 446 | |
| 403 | −rm -f "${TMPPIC}" | |
| 404 | −rm -f "${TMPCUE}" | |
| 447 | + if [ ! $? -eq 0 ]; then | |
| 448 | + break | |
| 449 | + fi | |
| 405 | 450 | |
| 406 | −if [ ${DRY} -ne 1 -a ${REMOVE} -eq 1 ]; then | |
| 407 | − YEP="n" | |
| 451 | + $msg "$cG>> $cC\"${FILE}\"$cZ" | |
| 452 | + unset PIC | |
| 453 | + unset CUE | |
| 454 | + split_file "${FILE}" | |
| 408 | 455 | |
| 409 | − if [ ${FORCE} -ne 1 ]; then | |
| 410 | − echo -n "Are you sure you want to delete original? [y] >" | |
| 411 | − read YEP | |
| 412 | − fi | |
| 456 | + if [ ! $? -eq 0 ]; then | |
| 457 | + emsg "Failed to split \"${FILE}\"" | |
| 458 | + fi | |
| 459 | + | |
| 460 | + echo | |
| 461 | + done | |
| 462 | +} | |
| 413 | 463 | |
| 414 | − if [ "${YEP}" = "y" -o ${FORCE} -eq 1 ]; then | |
| 415 | − rm -f "${FILE}" | |
| 464 | +# searches for files in a directory and splits them | |
| 465 | +split_dir ( ) { | |
| 466 | + find "$1" -name '*.flac' -o -name '*.ape' -o -name '*.wv' | split_collection | |
| 467 | +} | |
| 468 | + | |
| 469 | +if [ -d "${INPATH}" ]; then | |
| 470 | + if [ ! -x "${INPATH}" ]; then | |
| 471 | + emsg "Directory \"${INPATH}\" is not accessible" | |
| 472 | + exit 1 | |
| 416 | 473 | fi |
| 474 | + $msg "${cG}Input dir :$cZ ${INPATH}$cZ" | |
| 475 | + split_dir "${INPATH}" | |
| 476 | +elif [ -n "${INPATH}" ]; then | |
| 477 | + split_file "${INPATH}" | |
| 478 | +else | |
| 479 | + emsg "No input filename given. Use -h for help." | |
| 480 | + exit 1 | |
| 417 | 481 | fi |
| 418 | 482 | |
| 419 | 483 | $msg "\n${cP}Finished$cZ" |