Unified · Split
| @@ -49,37 +49,50 @@ unset CUE · 48 unmodified lines | |||
| 49 | unset CHARSET | 49 | unset CHARSET |
| 50 | FORCE=0 | 50 | FORCE=0 |
| 51 | 51 | ||
| 52 | HELP="Usage: split2\${FORMAT}.sh [OPTIONS] FILE | 52 | cR="\033[31m" |
| 53 | -o DIRECTORY * - set output directory | 53 | cG="\033[32m" |
| 54 | -cue FILE - use file as a cue sheet | 54 | cP="\033[36m" |
| 55 | -f FORMAT - use specified output format (current is \${FORMAT}) | 55 | cU="\033[4m" |
| 56 | -c FILE * - use file as a cover image | 56 | cZ="\033[0m" |
| 57 | -cuecharset CHARSET - convert cue sheet from CHARSET to UTF-8 (no conversion by default) | 57 | |
| 58 | -nc * - do not set any cover images | 58 | HELP="${cG}split2flac splits one big ${cU}APE/FLAC/WV$cZ$cG file to ${cU}FLAC/MP3/OGG$cZ$cG tracks with tagging and renaming. |
| 59 | -cs WxH * - set cover image size (current is \${PIC_SIZE}) | 59 | |
| 60 | -d * - create artist/album subdirs | 60 | Usage: ${cZ}split2${FORMAT}.sh [${cU}OPTIONS$cZ] ${cU}FILE$cZ [${cU}OPTIONS$cZ]$cZ |
| 61 | -nd * - do not create any subdirs | 61 | $cG-p$cZ - dry run |
| 62 | -r * - rename tracks to include title | 62 | $cG-o ${cU}DIRECTORY$cZ $cR*$cZ - set output directory |
| 63 | -nr * - do not rename tracks (numbers only, e.g. '01.\${FORMAT}') | 63 | $cG-cue ${cU}FILE$cZ - use file as a cue sheet |
| 64 | -p - dry run | 64 | $cG-f ${cU}FORMAT$cZ - use specified output format $cP(current is ${FORMAT})$cZ |
| 65 | -D * - delete original file | 65 | $cG-c ${cU}FILE$cZ $cR*$cZ - use file as a cover image |
| 66 | -nD * - do not remove the original | 66 | $cG-cuecharset ${cU}CHARSET$cZ - convert cue sheet from CHARSET to UTF-8 (no conversion by default) |
| 67 | -f - force deletion without asking | 67 | $cG-nc ${cR}*$cZ - do not set any cover images |
| 68 | -s - save configuration to \"\${CONFIG}\" | 68 | $cG-cs ${cU}WxH$cZ $cR*$cZ - set cover image size $cP(current is ${PIC_SIZE})$cZ |
| 69 | $cG-d $cR*$cZ - create artist/album subdirs | ||
| 70 | $cG-nd $cR*$cZ - do not create any subdirs | ||
| 71 | $cG-r $cR*$cZ - rename tracks to include title | ||
| 72 | $cG-nr $cR*$cZ - do not rename tracks (numbers only, e.g. '01.${FORMAT}') | ||
| 73 | $cG-D $cR*$cZ - delete original file | ||
| 74 | $cG-nD $cR*$cZ - do not remove the original | ||
| 75 | $cG-F$cZ - force deletion without asking | ||
| 76 | -s - save configuration to $cP\"${CONFIG}\"$cZ | ||
| 69 | -h - print this message | 77 | -h - print this message |
| 70 | -H - print README | ||
| 71 | 78 | ||
| 72 | * - option has effect on configuration if -s option passed. | 79 | $cR*$cZ - option has effect on configuration if $cP'-s'$cZ option passed. |
| 73 | NOTE: '-c some_file.jpg -s' only allows cover images, it doesn't set a default one. | 80 | ${cP}NOTE: $cG'-c some_file.jpg -s'$cP only ${cU}allows$cZ$cP cover images, it doesn't set a default one. |
| 74 | Supported FORMATs: flac, mp3, ogg." | 81 | ${cZ}Supported $cU${cG}FORMATs${cZ}: flac, mp3, ogg. |
| 75 | 82 | ||
| 76 | README="split2flac splits one big APE/FLAC/WV file to FLAC/MP3/OGG tracks with tagging and renaming. | 83 | It's better to pass $cP'-p'$cZ option to see what will happen when actually splitting tracks. |
| 77 | It's better to pass '-p' option to see what will happen when actually splitting tracks. | 84 | You may want to pass $cP'-s'$cZ option for the first run to save default configuration |
| 78 | You may want to pass '-s' option for the first run to save default configuration | ||
| 79 | (output dir, cover image size, etc.) so you won't need to pass a lot of options | 85 | (output dir, cover image size, etc.) so you won't need to pass a lot of options |
| 80 | every time, just a filename. | 86 | every time, just a filename. |
| 81 | Script will try to find CUE sheet if it wasn't specified. It also supports internal CUE sheets." | 87 | Script will try to find CUE sheet if it wasn't specified. It also supports internal CUE sheets." |
| 82 | 88 | ||
| 89 | msg="echo -e" | ||
| 90 | |||
| 91 | fatal ( ) { | ||
| 92 | $msg "${cR}$1${cZ}" | ||
| 93 | exit 1 | ||
| 94 | } | ||
| 95 | |||
| 83 | # parse arguments | 96 | # parse arguments |
| 84 | while [ "$1" ]; do | 97 | while [ "$1" ]; do |
| 85 | case "$1" in | 98 | case "$1" in |
| @@ -97,18 +110,15 @@ while [ "$1" ]; do · 11 unmodified lines | |||
| 97 | -p) DRY=1;; | 110 | -p) DRY=1;; |
| 98 | -D) REMOVE=1;; | 111 | -D) REMOVE=1;; |
| 99 | -nD) REMOVE=0;; | 112 | -nD) REMOVE=0;; |
| 100 | -f) FORCE=1;; | 113 | -F) FORCE=1;; |
| 101 | -s) SAVE=1;; | 114 | -s) SAVE=1;; |
| 102 | -h) eval "echo \"${HELP}\""; exit 0;; | 115 | -h|--help|-help) eval "$msg \"${HELP}\""; exit 0;; |
| 103 | -H) echo "${README}"; exit 0;; | ||
| 104 | *) | 116 | *) |
| 105 | if [ -r "${FILE}" ]; then | 117 | if [ -r "${FILE}" ]; then |
| 106 | echo "Unknown option $1" | 118 | eval "$msg \"${HELP}\"" |
| 107 | eval "echo \"${HELP}\"" | 119 | fatal "\nUnknown option $1" |
| 108 | exit 1 | ||
| 109 | elif [ ! -r "$1" ]; then | 120 | elif [ ! -r "$1" ]; then |
| 110 | echo "Unable to read $1" | 121 | fatal "Unable to read $1" |
| 111 | exit 1 | ||
| 112 | else | 122 | else |
| 113 | FILE="$1" | 123 | FILE="$1" |
| 114 | fi;; | 124 | fi;; |
| @@ -120,6 +130,18 @@ METAFLAC="metaflac --no-utf8-convert" · 5 unmodified lines | |||
| 120 | VORBISCOMMENT="vorbiscomment -R -a" | 130 | VORBISCOMMENT="vorbiscomment -R -a" |
| 121 | ID3TAG="id3tag -2" | 131 | ID3TAG="id3tag -2" |
| 122 | 132 | ||
| 133 | # print input filename | ||
| 134 | $msg "${cG}Input file :$cZ ${FILE:?No input filename given. Use -h for help.}$cZ" | ||
| 135 | |||
| 136 | # check & print output format | ||
| 137 | msg_format="${cG}Output format :$cZ" | ||
| 138 | case ${FORMAT} in | ||
| 139 | flac) $msg "$msg_format FLAC";; | ||
| 140 | mp3) $msg "$msg_format MP3";; | ||
| 141 | ogg) $msg "$msg_format OGG VORBIS";; | ||
| 142 | *) fatal "Unknown output format \"${FORMAT}\"";; | ||
| 143 | esac | ||
| 144 | |||
| 123 | # search for a cue sheet if not specified | 145 | # search for a cue sheet if not specified |
| 124 | if [ -z "${CUE}" ]; then | 146 | if [ -z "${CUE}" ]; then |
| 125 | CUE="${FILE}.cue" | 147 | CUE="${FILE}.cue" |
| @@ -138,8 +160,7 @@ if [ -z "${CUE}" ]; then · 12 unmodified lines | |||
| 138 | echo "${CUESHEET}" > "${CUE}" | 160 | echo "${CUESHEET}" > "${CUE}" |
| 139 | 161 | ||
| 140 | if [ $? -ne 0 ]; then | 162 | if [ $? -ne 0 ]; then |
| 141 | echo "Unable to save internal cue sheet" | 163 | fatal "Unable to save internal cue sheet" |
| 142 | exit 1 | ||
| 143 | fi | 164 | fi |
| 144 | else | 165 | else |
| 145 | unset CUE | 166 | unset CUE |
| @@ -148,23 +169,20 @@ if [ -z "${CUE}" ]; then · 2 unmodified lines | |||
| 148 | fi | 169 | fi |
| 149 | fi | 170 | fi |
| 150 | 171 | ||
| 151 | # print some info and check arguments | 172 | # print cue sheet filename |
| 152 | echo "Input file : ${FILE:?No input filename given. Use -h for help.}" | 173 | $msg "${cG}Cue sheet :$cZ ${CUE:?No cue sheet}$cZ" |
| 153 | echo "Cue sheet : ${CUE:?No cue sheet}" | ||
| 154 | 174 | ||
| 155 | if [ -n "${CHARSET}" ]; then | 175 | if [ -n "${CHARSET}" ]; then |
| 156 | echo "Cue charset : ${CHARSET} -> utf-8" | 176 | $msg "${cG}Cue charset : $cP${CHARSET} -> utf-8$cZ" |
| 157 | CUESHEET=$(iconv -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) | 177 | CUESHEET=$(iconv -f "${CHARSET}" -t utf-8 "${CUE}" 2>/dev/null) |
| 158 | if [ $? -ne 0 ]; then | 178 | if [ $? -ne 0 ]; then |
| 159 | echo "Unable to convert cue sheet from ${CHARSET} to utf-8" | 179 | fatal "Unable to convert cue sheet from ${CHARSET} to utf-8" |
| 160 | exit 1 | ||
| 161 | fi | 180 | fi |
| 162 | CUE="${TMPCUE}" | 181 | CUE="${TMPCUE}" |
| 163 | echo "${CUESHEET}" > "${CUE}" | 182 | echo "${CUESHEET}" > "${CUE}" |
| 164 | 183 | ||
| 165 | if [ $? -ne 0 ]; then | 184 | if [ $? -ne 0 ]; then |
| 166 | echo "Unable to save converted cue sheet" | 185 | fatal "Unable to save converted cue sheet" |
| 167 | exit 1 | ||
| 168 | fi | 186 | fi |
| 169 | fi | 187 | fi |
| 170 | 188 | ||
| @@ -193,16 +211,16 @@ elif [ -z "${PIC}" ]; then · 22 unmodified lines | |||
| 193 | fi | 211 | fi |
| 194 | fi | 212 | fi |
| 195 | 213 | ||
| 196 | echo "Cover image : ${PIC:-not set}" | 214 | $msg "${cG}Cover image :$cZ ${PIC:-not set}" |
| 197 | echo "Output dir : ${DIR:?Output directory was not set}" | 215 | $msg "${cG}Output dir :$cZ ${DIR:?Output directory was not set}" |
| 198 | 216 | ||
| 199 | # file removal warning | 217 | # file removal warning |
| 200 | if [ ${REMOVE} -eq 1 ]; then | 218 | if [ ${REMOVE} -eq 1 ]; then |
| 201 | echo -n "Also remove original" | 219 | msg_removal="\n${cR}Also remove original" |
| 202 | if [ ${FORCE} -eq 1 ]; then | 220 | if [ ${FORCE} -eq 1 ]; then |
| 203 | echo | 221 | $msg "$msg_removal (WITHOUT ASKING)$cZ" |
| 204 | else | 222 | else |
| 205 | echo " if user says 'y'" | 223 | $msg "$msg_removal if user says 'y'$cZ" |
| 206 | fi | 224 | fi |
| 207 | fi | 225 | fi |
| 208 | 226 | ||
| @@ -214,7 +232,7 @@ if [ ${SAVE} -eq 1 ]; then · 5 unmodified lines | |||
| 214 | echo "NOPIC=${NOPIC}" >> "${CONFIG}" | 232 | echo "NOPIC=${NOPIC}" >> "${CONFIG}" |
| 215 | echo "REMOVE=${REMOVE}" >> "${CONFIG}" | 233 | echo "REMOVE=${REMOVE}" >> "${CONFIG}" |
| 216 | echo "PIC_SIZE=${PIC_SIZE}" >> "${CONFIG}" | 234 | echo "PIC_SIZE=${PIC_SIZE}" >> "${CONFIG}" |
| 217 | echo "Configuration saved" | 235 | $msg "${cP}Configuration saved$cZ" |
| 218 | fi | 236 | fi |
| 219 | 237 | ||
| 220 | GETTAG="cueprint -n 1 -t" | 238 | GETTAG="cueprint -n 1 -t" |
| @@ -236,14 +254,12 @@ if [ -n "${YEAR}" ]; then · 15 unmodified lines | |||
| 236 | fi | 254 | fi |
| 237 | fi | 255 | fi |
| 238 | 256 | ||
| 239 | echo | 257 | $msg "\n${cG}Artist :$cZ ${TAG_ARTIST}" |
| 240 | echo "Artist : ${TAG_ARTIST}" | 258 | $msg "${cG}Album :$cZ ${TAG_ALBUM}" |
| 241 | echo "Album : ${TAG_ALBUM}" | ||
| 242 | if [ -n "${TAG_DATE}" ]; then | 259 | if [ -n "${TAG_DATE}" ]; then |
| 243 | echo "Year : ${TAG_DATE}" | 260 | $msg "${cG}Year :$cZ ${TAG_DATE}" |
| 244 | fi | 261 | fi |
| 245 | echo "Tracks : ${TRACKS_NUM}" | 262 | $msg "${cG}Tracks :$cZ ${TRACKS_NUM}\n" |
| 246 | echo | ||
| 247 | 263 | ||
| 248 | # prepare output directory | 264 | # prepare output directory |
| 249 | OUT="${DIR}" | 265 | OUT="${DIR}" |
| @@ -259,30 +275,28 @@ if [ ${NOSUBDIRS} -ne 1 ]; then · 9 unmodified lines | |||
| 259 | OUT="${OUT}/${DIR_ARTIST}/${DIR_ALBUM}" | 275 | OUT="${OUT}/${DIR_ARTIST}/${DIR_ALBUM}" |
| 260 | fi | 276 | fi |
| 261 | 277 | ||
| 262 | echo "Saving tracks to \"${OUT}\"" | 278 | $msg "${cP}Saving tracks to $cZ\"${OUT}\"" |
| 263 | 279 | ||
| 264 | if [ ${DRY} -ne 1 ]; then | 280 | if [ ${DRY} -ne 1 ]; then |
| 265 | # create output dir | 281 | # create output dir |
| 266 | mkdir -p "${OUT}" | 282 | mkdir -p "${OUT}" |
| 267 | 283 | ||
| 268 | if [ $? -ne 0 ]; then | 284 | if [ $? -ne 0 ]; then |
| 269 | echo "Failed to create output directory" | 285 | fatal "Failed to create output directory" |
| 270 | exit 1 | ||
| 271 | fi | 286 | fi |
| 272 | 287 | ||
| 273 | case ${FORMAT} in | 288 | case ${FORMAT} in |
| 274 | flac) ENC="flac flac -8 - -o %f";; | 289 | flac) ENC="flac flac -8 - -o %f";; |
| 275 | mp3) ENC="cust ext=mp3 lame --preset extreme - %f";; | 290 | mp3) ENC="cust ext=mp3 lame --preset extreme - %f";; |
| 276 | ogg) ENC="cust ext=ogg oggenc -q 10 - -o %f";; | 291 | ogg) ENC="cust ext=ogg oggenc -q 10 - -o %f";; |
| 277 | *) echo "Unknown output format ${FORMAT}"; exit 1;; | 292 | *) fatal "Unknown output format ${FORMAT}";; |
| 278 | esac | 293 | esac |
| 279 | 294 | ||
| 280 | # split to tracks | 295 | # split to tracks |
| 281 | cuebreakpoints "${CUE}" | \ | 296 | cuebreakpoints "${CUE}" | \ |
| 282 | shnsplit -O never -o "${ENC}" -d "${OUT}" -t "%n" "${FILE}" | 297 | shnsplit -O never -o "${ENC}" -d "${OUT}" -t "%n" "${FILE}" |
| 283 | if [ $? -ne 0 ]; then | 298 | if [ $? -ne 0 ]; then |
| 284 | echo "Failed to split" | 299 | fatal "Failed to split" |
| 285 | exit 1 | ||
| 286 | fi | 300 | fi |
| 287 | 301 | ||
| 288 | # prepare cover image | 302 | # prepare cover image |
| @@ -291,15 +305,14 @@ if [ ${DRY} -ne 1 ]; then · 2 unmodified lines | |||
| 291 | if [ $? -eq 0 ]; then | 305 | if [ $? -eq 0 ]; then |
| 292 | PIC="${TMPPIC}" | 306 | PIC="${TMPPIC}" |
| 293 | else | 307 | else |
| 294 | echo "Failed to convert cover image" | 308 | $msg "${cR}Failed to convert cover image$cZ" |
| 295 | unset PIC | 309 | unset PIC |
| 296 | fi | 310 | fi |
| 297 | fi | 311 | fi |
| 298 | fi | 312 | fi |
| 299 | 313 | ||
| 300 | # set tags and rename | 314 | # set tags and rename |
| 301 | echo | 315 | $msg "\n${cP}Setting tags$cZ" |
| 302 | echo "Setting tags" | ||
| 303 | 316 | ||
| 304 | i=1 | 317 | i=1 |
| 305 | while [ $i -le ${TRACKS_NUM} ]; do | 318 | while [ $i -le ${TRACKS_NUM} ]; do |
| @@ -308,15 +321,14 @@ while [ $i -le ${TRACKS_NUM} ]; do · 2 unmodified lines | |||
| 308 | FILE_TITLE=$(echo ${TAG_TITLE} | ${VALIDATE}) | 321 | FILE_TITLE=$(echo ${TAG_TITLE} | ${VALIDATE}) |
| 309 | f="${OUT}/${FILE_TRACK}.${FORMAT}" | 322 | f="${OUT}/${FILE_TRACK}.${FORMAT}" |
| 310 | 323 | ||
| 311 | echo "$i: ${TAG_TITLE}" | 324 | $msg "$i: $cG${TAG_TITLE}$cZ" |
| 312 | 325 | ||
| 313 | if [ ${NORENAME} -ne 1 ]; then | 326 | if [ ${NORENAME} -ne 1 ]; then |
| 314 | FINAL="${OUT}/${FILE_TRACK} - ${FILE_TITLE}.${FORMAT}" | 327 | FINAL="${OUT}/${FILE_TRACK} - ${FILE_TITLE}.${FORMAT}" |
| 315 | if [ ${DRY} -ne 1 ]; then | 328 | if [ ${DRY} -ne 1 ]; then |
| 316 | mv "$f" "${FINAL}" | 329 | mv "$f" "${FINAL}" |
| 317 | if [ $? -ne 0 ]; then | 330 | if [ $? -ne 0 ]; then |
| 318 | echo "Failed to rename track file" | 331 | fatal "Failed to rename track file" |
| 319 | exit 1 | ||
| 320 | fi | 332 | fi |
| 321 | fi | 333 | fi |
| 322 | else | 334 | else |
| @@ -373,16 +385,16 @@ while [ $i -le ${TRACKS_NUM} ]; do · 50 unmodified lines | |||
| 373 | RES=$((${RES} + $?)) | 385 | RES=$((${RES} + $?)) |
| 374 | fi | 386 | fi |
| 375 | ;; | 387 | ;; |
| 376 | *) echo "Unknown output format ${FORMAT}"; exit 1;; | 388 | *) |
| 389 | fatal "Unknown output format ${FORMAT}";; | ||
| 377 | esac | 390 | esac |
| 378 | 391 | ||
| 379 | if [ ${RES} -ne 0 ]; then | 392 | if [ ${RES} -ne 0 ]; then |
| 380 | echo "Failed to set tags for track" | 393 | fatal "Failed to set tags for track" |
| 381 | exit 1 | ||
| 382 | fi | 394 | fi |
| 383 | fi | 395 | fi |
| 384 | 396 | ||
| 385 | echo " -> ${FINAL}" | 397 | $msg " -> ${cP}${FINAL}$cZ" |
| 386 | 398 | ||
| 387 | i=$(($i + 1)) | 399 | i=$(($i + 1)) |
| 388 | done | 400 | done |
| @@ -403,5 +415,4 @@ if [ ${DRY} -ne 1 -a ${REMOVE} -eq 1 ]; then · 14 unmodified lines | |||
| 403 | fi | 415 | fi |
| 404 | fi | 416 | fi |
| 405 | 417 | ||
| 406 | echo | 418 | $msg "\n${cP}Finished$cZ" |
| 407 | echo "Finished" | ||