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