Unified · Split
| @@ -54,6 +54,7 @@ DIR="." · 53 unmodified lines | ||
| 54 | 54 | eval $(cat "${CONFIG}" 2>/dev/null) |
| 55 | 55 | DRY=0 |
| 56 | 56 | SAVE=0 |
| 57 | +NASK=0 | |
| 57 | 58 | unset PIC INPATH CUE CHARSET |
| 58 | 59 | FORCE=0 |
| 59 | 60 | |
| @@ -68,6 +69,7 @@ Usage: \${cZ}split2\${FORMAT}.sh [\${cU}OPTIONS\$cZ] \${cU}FILE\$cZ [\${cU}OPTIO · 8 unmodified lines | ||
| 68 | 69 | \$cG-o \${cU}DIRECTORY\$cZ \$cR*\$cZ - set output directory (current is \$cP\${DIR}\$cZ) |
| 69 | 70 | \$cG-cue \${cU}FILE\$cZ - use file as a cue sheet (does not work with \${cU}DIR\$cZ) |
| 70 | 71 | \$cG-cuecharset \${cU}CHARSET\$cZ - convert cue sheet from CHARSET to UTF-8 (no conversion by default) |
| 72 | + \$cG-nask\$cZ - do not ask to enter proper charset of a cue sheet (default is to ask) | |
| 71 | 73 | \$cG-f \${cU}FORMAT\$cZ - use specified output format \$cP(current is \${FORMAT})\$cZ |
| 72 | 74 | \$cG-c \${cU}FILE\$cZ \$cR*\$cZ - use file as a cover image (does not work with \${cU}DIR\$cZ) |
| 73 | 75 | \$cG-nc \${cR}*\$cZ - do not set any cover images |
| @@ -124,6 +126,7 @@ while [ "$1" ]; do · 50 unmodified lines | ||
| 124 | 126 | -o) DIR=$2; shift;; |
| 125 | 127 | -cue) CUE=$2; shift;; |
| 126 | 128 | -cuecharset) CHARSET=$2; shift;; |
| 129 | + -nask) NASK=1;; | |
| 127 | 130 | -f) FORMAT=$2; shift;; |
| 128 | 131 | -c) NOPIC=0; PIC=$2; shift;; |
| 129 | 132 | -nc) NOPIC=1;; |
| @@ -211,9 +214,7 @@ split_file () { · 81 unmodified lines | ||
| 211 | 214 | CUESHEET=$(${METAFLAC} --show-tag=CUESHEET "${FILE}" 2>/dev/null | sed 's/^cuesheet=//;s/^CUESHEET=//') |
| 212 | 215 | |
| 213 | 216 | # try WV internal cue sheet |
| 214 | − if [ -z "${CUESHEET}" ]; then | |
| 215 | − CUESHEET=$(wvunpack -q -c "${FILE}" 2>/dev/null) | |
| 216 | − fi | |
| 217 | + [ -z "${CUESHEET}" ] && CUESHEET=$(wvunpack -q -c "${FILE}" 2>/dev/null) | |
| 217 | 218 | |
| 218 | 219 | # try APE internal cue sheet (omfg!) |
| 219 | 220 | if [ -z "${CUESHEET}" ]; then |
| @@ -223,9 +224,7 @@ split_file () { · 3 unmodified lines | ||
| 223 | 224 | tail -c ${LENGTH} "$1" | grep -a CUESHEET >/dev/null 2>&1 |
| 224 | 225 | if [ $? -eq 0 ]; then |
| 225 | 226 | CUESHEET=$(tail -c ${LENGTH} "$1" | sed 's/.*CUESHEET.//g' 2>/dev/null) |
| 226 | − if [ $? -ne 0 ]; then | |
| 227 | − CUESHEET="" | |
| 228 | − fi | |
| 227 | + [ $? -ne 0 ] && CUESHEET="" | |
| 229 | 228 | fi |
| 230 | 229 | fi |
| 231 | 230 | fi |
| @@ -252,13 +251,35 @@ split_file () { · 20 unmodified lines | ||
| 252 | 251 | return 1 |
| 253 | 252 | fi |
| 254 | 253 | |
| 255 | − if [ "${CHARSET}" ]; then | |
| 256 | − $msg "${cG}Cue charset : $cP${CHARSET} -> utf-8$cZ\n" | |
| 257 | − CUESHEET=$(iconv -c -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) | |
| 258 | − if [ $? -ne 0 ]; then | |
| 259 | − emsg "Unable to convert cue sheet from ${CHARSET} to utf-8\n" | |
| 260 | − return 1 | |
| 254 | + # cue sheet charset | |
| 255 | + [ -z "${CHARSET}" ] && CHARSET="utf-8" || $msg "${cG}Cue charset : $cP${CHARSET} -> utf-8$cZ\n" | |
| 256 | + | |
| 257 | + CUESHEET=$(iconv -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) | |
| 258 | + | |
| 259 | + if [ $? -ne 0 ]; then | |
| 260 | + [ "${CHARSET}" = "utf-8" ] \ | |
| 261 | + && emsg "Cue sheet is not utf-8\n" \ | |
| 262 | + || emsg "Unable to convert cue sheet from ${CHARSET} to utf-8\n" | |
| 263 | + | |
| 264 | + if [ ${NASK} -eq 0 ]; then | |
| 265 | + while [ 1 ]; do | |
| 266 | + echo -n "Please enter the charset (or just press ENTER to ignore) > " | |
| 267 | + read CHARSET | |
| 268 | + | |
| 269 | + [ -z "${CHARSET}" ] && break | |
| 270 | + $msg "${cG}Converted cue sheet:$cZ\n" | |
| 271 | + iconv -f "${CHARSET}" -t utf-8 "${CUE}" || continue | |
| 272 | + | |
| 273 | + echo -n "Is this right? [Y/n] > " | |
| 274 | + read YEP | |
| 275 | + [ -z "${YEP}" -o "${YEP}" = "y" -o "${YEP}" = "Y" ] && break | |
| 276 | + done | |
| 277 | + | |
| 278 | + CUESHEET=$(iconv -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) | |
| 261 | 279 | fi |
| 280 | + fi | |
| 281 | + | |
| 282 | + if [ "${CHARSET}" -a "${CHARSET}" != "utf-8" ]; then | |
| 262 | 283 | CUE="${TMPCUE}" |
| 263 | 284 | echo "${CUESHEET}" > "${CUE}" |
| 264 | 285 | |
| @@ -299,11 +320,9 @@ split_file () { · 34 unmodified lines | ||
| 299 | 320 | # file removal warning |
| 300 | 321 | if [ ${REMOVE} -eq 1 ]; then |
| 301 | 322 | msg_removal="\n${cR}Also remove original" |
| 302 | − if [ ${FORCE} -eq 1 ]; then | |
| 303 | − $msg "$msg_removal (WITHOUT ASKING)$cZ\n" | |
| 304 | − else | |
| 305 | − $msg "$msg_removal if user says 'y'$cZ\n" | |
| 306 | − fi | |
| 323 | + [ ${FORCE} -eq 1 ] \ | |
| 324 | + && $msg "$msg_removal (WITHOUT ASKING)$cZ\n" \ | |
| 325 | + || $msg "$msg_removal if user says 'y'$cZ\n" | |
| 307 | 326 | fi |
| 308 | 327 | |
| 309 | 328 | # get common tags |
| @@ -328,19 +347,13 @@ split_file () { · 18 unmodified lines | ||
| 328 | 347 | unset TAG_DATE |
| 329 | 348 | |
| 330 | 349 | if [ "${YEAR}" ]; then |
| 331 | − if [ ${YEAR} -ne 0 ]; then | |
| 332 | − TAG_DATE="${YEAR}" | |
| 333 | − fi | |
| 350 | + [ ${YEAR} -ne 0 ] && TAG_DATE="${YEAR}" | |
| 334 | 351 | fi |
| 335 | 352 | |
| 336 | 353 | $msg "\n${cG}Artist :$cZ ${TAG_ARTIST}\n" |
| 337 | 354 | $msg "${cG}Album :$cZ ${TAG_ALBUM}\n" |
| 338 | − if [ "${TAG_GENRE}" ]; then | |
| 339 | − $msg "${cG}Genre :$cZ ${TAG_GENRE}\n" | |
| 340 | − fi | |
| 341 | − if [ "${TAG_DATE}" ]; then | |
| 342 | − $msg "${cG}Year :$cZ ${TAG_DATE}\n" | |
| 343 | − fi | |
| 355 | + [ "${TAG_GENRE}" ] && $msg "${cG}Genre :$cZ ${TAG_GENRE}\n" | |
| 356 | + [ "${TAG_DATE}" ] && $msg "${cG}Year :$cZ ${TAG_DATE}\n" | |
| 344 | 357 | $msg "${cG}Tracks :$cZ ${TRACKS_NUM}\n\n" |
| 345 | 358 | |
| 346 | 359 | # prepare output directory |
| @@ -350,13 +363,9 @@ split_file () { · 3 unmodified lines | ||
| 350 | 363 | DIR_ARTIST=$(echo ${TAG_ARTIST} | ${VALIDATE}) |
| 351 | 364 | DIR_ALBUM=$(echo ${TAG_ALBUM} | ${VALIDATE}) |
| 352 | 365 | |
| 353 | − if [ "${TAG_DATE}" ]; then | |
| 354 | − DIR_ALBUM="${TAG_DATE} - ${DIR_ALBUM}" | |
| 355 | − fi | |
| 366 | + [ "${TAG_DATE}" ] && DIR_ALBUM="${TAG_DATE} - ${DIR_ALBUM}" | |
| 367 | + [ ${DRY} -ne 1 ] && mkdir -p "${OUT}/${DIR_ARTIST}" | |
| 356 | 368 | |
| 357 | − if [ ${DRY} -ne 1 ]; then | |
| 358 | − mkdir -p "${OUT}/${DIR_ARTIST}" | |
| 359 | − fi | |
| 360 | 369 | OUT="${OUT}/${DIR_ARTIST}/${DIR_ALBUM}" |
| 361 | 370 | fi |
| 362 | 371 | |
| @@ -544,11 +553,11 @@ split_file () { · 181 unmodified lines | ||
| 544 | 553 | |
| 545 | 554 | if [ ${DRY} -ne 1 ]; then |
| 546 | 555 | ${RG} "${OUT}/"*.${FORMAT} >/dev/null |
| 547 | − fi | |
| 548 | 556 | |
| 549 | − if [ $? -ne 0 ]; then | |
| 550 | − emsg "Failed to adjust gain for track\n" | |
| 551 | − return 1 | |
| 557 | + if [ $? -ne 0 ]; then | |
| 558 | + emsg "Failed to adjust gain for track\n" | |
| 559 | + return 1 | |
| 560 | + fi | |
| 552 | 561 | fi |
| 553 | 562 | fi |
| 554 | 563 | |
| @@ -559,22 +568,29 @@ split_file () { · 4 unmodified lines | ||
| 559 | 568 | YEP="n" |
| 560 | 569 | |
| 561 | 570 | if [ ${FORCE} -ne 1 ]; then |
| 562 | − echo -n "Are you sure you want to delete original? [y] >" | |
| 571 | + echo -n "Are you sure you want to delete original? [y/N] > " | |
| 563 | 572 | read YEP |
| 564 | 573 | fi |
| 565 | 574 | |
| 566 | − if [ "${YEP}" = "y" -o ${FORCE} -eq 1 ]; then | |
| 567 | − rm -f "${FILE}" | |
| 568 | − fi | |
| 575 | + [ "${YEP}" = "y" -o "${YEP}" = "Y" -o ${FORCE} -eq 1 ] && rm -f "${FILE}" | |
| 569 | 576 | fi |
| 570 | 577 | |
| 571 | 578 | return 0 |
| 572 | 579 | } |
| 573 | 580 | |
| 581 | +# searches for files in a directory and splits them | |
| 574 | 582 | split_collection () { |
| 583 | + rm -f "${FAILED}" | |
| 575 | 584 | NUM_FAILED=0 |
| 576 | − | |
| 577 | − while read -r FILE; do | |
| 585 | + OLDIFS=${IFS} | |
| 586 | + OLDCHARSET="${CHARSET}" | |
| 587 | + # set IFS to newline. we do not use 'read' here because we may want to ask user for input | |
| 588 | + IFS=" | |
| 589 | +" | |
| 590 | + | |
| 591 | + for FILE in `find "$1" -iname '*.flac' -o -iname '*.ape' -o -iname '*.wv'`; do | |
| 592 | + IFS=${OLDIFS} | |
| 593 | + CHARSET=${OLDCHARSET} | |
| 578 | 594 | $msg "$cG>> $cC\"${FILE}\"$cZ\n" |
| 579 | 595 | unset PIC CUE |
| 580 | 596 | split_file "${FILE}" |
| @@ -600,19 +616,13 @@ split_collection () { · 19 unmodified lines | ||
| 600 | 616 | return 0 |
| 601 | 617 | } |
| 602 | 618 | |
| 603 | −# searches for files in a directory and splits them | |
| 604 | −split_dir () { | |
| 605 | − rm -f "${FAILED}" | |
| 606 | − find "$1" -iname '*.flac' -o -iname '*.ape' -o -iname '*.wv' | split_collection | |
| 607 | −} | |
| 608 | − | |
| 609 | 619 | if [ -d "${INPATH}" ]; then |
| 610 | 620 | if [ ! -x "${INPATH}" ]; then |
| 611 | 621 | emsg "Directory \"${INPATH}\" is not accessible\n" |
| 612 | 622 | exit 2 |
| 613 | 623 | fi |
| 614 | 624 | $msg "${cG}Input dir :$cZ ${INPATH}$cZ\n\n" |
| 615 | − split_dir "${INPATH}" | |
| 625 | + split_collection "${INPATH}" | |
| 616 | 626 | elif [ "${INPATH}" ]; then |
| 617 | 627 | split_file "${INPATH}" |
| 618 | 628 | else |
| @@ -620,11 +630,9 @@ else · 1 unmodified line | ||
| 620 | 630 | exit 1 |
| 621 | 631 | fi |
| 622 | 632 | |
| 623 | −# exit code of split_dir or split_file | |
| 633 | +# exit code of split_collection or split_file | |
| 624 | 634 | STATUS=$? |
| 625 | 635 | |
| 626 | 636 | $msg "\n${cP}Finished$cZ\n" |
| 627 | 637 | |
| 628 | −if [ ${STATUS} -ne 0 ]; then | |
| 629 | − exit 3 | |
| 630 | −fi | |
| 638 | +[ ${STATUS} -ne 0 ] && exit 3 | |