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