myaudiotags 25.1 KB
Newer Older
Cédric Marie's avatar
Cédric Marie committed
1 2
#!/bin/sh

3
# myaudiotags - Clean audio tags
Cédric Marie's avatar
Cédric Marie committed
4 5
# This file is part of Painted Shell - Shell configuration and scripts.
#
6
# Written in 2014-2018 by Cédric Marie
Cédric Marie's avatar
Cédric Marie committed
7 8 9 10 11 12 13 14 15 16 17
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software.
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.

print_usage()
{
18 19 20 21 22 23 24 25
    command=$(basename $0)
    printf "Usage: $command [-h] [COMMAND] [OPTION]... [FILE]\n"
    printf "Apply COMMAND to audio files in current directory, or to FILE\n"
    printf "if COMMAND is clean-single or set-single.\n\n"
    printf "Options:\n"
    printf "  -h        display this message\n\n"
    printf "Commands applied to all audio files in current directory (album):\n"
    printf "  show      show tags\n"
26
    printf "  clean     set only following tags:\n"
27 28 29 30 31 32 33 34 35 36
    printf "            - ARTIST (from first track if not multi-artist)\n"
    printf "            - TITLE\n"
    printf "            - ALBUM (from first track)\n"
    printf "            - DATE (from first track)\n"
    printf "            - TRACKNUMBER\n"
    printf "            - TRACKTOTAL (from first track if not multi-disc)\n"
    printf "            - DISCNUMBER (if multi-disc)\n"
    printf "            - DISCTOTAL (from first track if multi-disc)\n"
    printf "            - COMMENT (from first track, if present)\n"
    printf "            - LICENSE (from first track, if present)\n"
37
    printf "            (remove seek table, picture, cue sheet, and padding blocks)\n"
38 39 40 41 42 43 44
    printf "            Options for \"clean\" command:\n"
    printf "              -d  keep full date (default: keep only the year)\n"
    printf "              -a  multi-artist (read artist in each track)\n"
    printf "  seta ARTIST ALBUM DATE\n"
    printf "            set artist, album, and date tags in the first track\n"
    printf "  setc COMMENT\n"
    printf "            set comment tag in the first track\n"
45
    printf "  update    update title and track/disc number/total tags from filenames\n\n"
46 47 48 49 50 51 52 53 54 55 56 57
    printf "Commands applied to a single audio file:\n"
    printf "  clean-single FILE\n"
    printf "            set only following tags (and remove padding) in file FILE:\n"
    printf "            - ARTIST\n"
    printf "            - TITLE\n"
    printf "            - DATE\n"
    printf "            - COMMENT (if present)\n"
    printf "            - LICENSE (if present)\n"
    printf "            Options for \"clean-single\" command:\n"
    printf "              -d  keep full date (default: keep only the year)\n"
    printf "  set-single FILE ARTIST TITLE DATE\n"
    printf "            set artist, title, and date tags in file FILE\n"
Cédric Marie's avatar
Cédric Marie committed
58 59 60 61
}

print_header()
{
62
    printf "\n\033[1;32m$1\033[0m\n"
Cédric Marie's avatar
Cédric Marie committed
63 64 65 66
}

confirm_to_continue()
{
67 68 69 70 71
    printf "Do you want to continue (y/N)? "
    read answer
    if [ "$answer" != "y" ]; then
        exit 0
    fi
Cédric Marie's avatar
Cédric Marie committed
72 73 74 75
}

while getopts h OPT
do
76 77 78 79
    case $OPT in
        h) print_usage; exit 0;;
        *) print_usage; exit 1;;
    esac
Cédric Marie's avatar
Cédric Marie committed
80 81 82 83 84
done

# Find audio file type
flac_files=`ls *.flac 2> /dev/null`
if [ "$flac_files" = "" ]; then
85 86 87 88 89
    mp3_files=`ls *.mp3 2> /dev/null`
    if [ "$mp3_files" = "" ]; then
        printf "Could not find either FLAC or MP3 files in current directory\n"
        exit 1
    fi
Cédric Marie's avatar
Cédric Marie committed
90 91 92 93 94 95 96 97 98 99
fi

command=$1

# -----------------------------------------------------------------------------
# show
# -----------------------------------------------------------------------------

if [ "$command" = "show" ]; then

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    # FLAC files
    if [ "$flac_files" != "" ]; then
        ls -1 *.flac |
            {
                while read file
                do
                    printf "$file\n"
                    printf "\033[7m"
                    metaflac --list "$file"
                    printf "\033[0m\n"
                done
            }
        # MP3 files
    elif [ "$mp3_files" != "" ]; then
        ls -1 *.mp3 |
            {
                while read file
                do
                    printf "$file\n"
                    printf "\033[7m"
                    id3v2 --list "$file"
                    printf "\033[0m\n"
                done
            }
    fi
    exit 0
Cédric Marie's avatar
Cédric Marie committed
126 127 128 129 130 131 132 133
fi

# -----------------------------------------------------------------------------
# clean
# -----------------------------------------------------------------------------

if [ "$command" = "clean" ]; then

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    year_only=0
    multi_artist=0
    shift 1
    while getopts ya OPT
    do
        case $OPT in
            y) year_only=1;;
            a) multi_artist=1;;
            *) print_usage; exit 1;;
        esac
    done
    multi_disc=0

    tags_filename=/tmp/tag-sieve-clean.tags

    # FLAC files
    if [ "$flac_files" != "" ]; then

        # Get album information from the first track
        file=`ls -1 *.flac | grep -m 1 flac`
154
        print_header "Get album information from the first track ($file)"
155 156 157 158 159 160 161 162 163
        metaflac --export-tags-to=$tags_filename "$file"
        tag_artist=`grep "^ARTIST=" $tags_filename | tail -n 1`
        tag_artist=${tag_artist#ARTIST=}
        tag_album=`grep "^ALBUM=" $tags_filename | tail -n 1`
        tag_album=${tag_album#ALBUM=}
        tag_date=`grep "^DATE=" $tags_filename | tail -n 1`
        tag_date=${tag_date#DATE=}
        if [ "$year_only" = "1" ]; then
            tag_date=${tag_date%%-*}
164
        fi
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        tag_tracktotal=`grep "^TRACKTOTAL=" $tags_filename | tail -n 1`
        tag_tracktotal=${tag_tracktotal#TRACKTOTAL=}
        if [ "${file%%.*}" = "1" ]; then
            multi_disc=1
            tag_discnumber=`grep "^DISCNUMBER=" $tags_filename | tail -n 1`
            tag_discnumber=${tag_discnumber#DISCNUMBER=}
            if [ "$tag_discnumber" != "" ]; then
                tag_disctotal=`grep "^DISCTOTAL=" $tags_filename | tail -n 1`
                tag_disctotal=${tag_disctotal#DISCTOTAL=}
                if [ "$tag_disctotal" = "" ]; then
                    printf "DISCTOTAL is missing, please enter a value for this tag: "
                    read tag_disctotal
                fi
            fi
        fi
        tag_comment=`grep "^COMMENT=" $tags_filename | tail -n 1`
        tag_comment=${tag_comment#COMMENT=}
        tag_license=`grep "^LICENSE=" $tags_filename | tail -n 1`
        tag_license=${tag_license#LICENSE=}

        # Print album information
        printf "\033[7m"
        if [ "$multi_artist" = "0" ]; then
            printf "ARTIST=$tag_artist\n"
        fi
        printf "ALBUM=$tag_album\n"
        printf "DATE=$tag_date\n"
        if [ "$multi_disc" = "1" ]; then
            printf "DISCTOTAL=$tag_disctotal\n"
        else
            printf "TRACKTOTAL=$tag_tracktotal\n"
        fi
        if [ "$tag_comment" != "" ]; then
            printf "COMMENT=$tag_comment\n"
        fi
        if [ "$tag_license" != "" ]; then
            printf "LICENSE=$tag_license\n"
        fi
        printf "\033[0m"

        confirm_to_continue

        print_header "Set tags in all tracks"

        # Set read-write permission
        chmod 644 *.flac

        # Set tags in all tracks
        ls -1 *.flac |
            {
                while read file
                do
                    metaflac --export-tags-to=$tags_filename "$file"
                    if [ "$multi_artist" = "1" ]; then
                        tag_artist=`grep "^ARTIST=" $tags_filename | tail -n 1`
                        tag_artist=${tag_artist#ARTIST=}
                    fi
                    tag_title=`grep "^TITLE=" $tags_filename | tail -n 1`
                    tag_title=${tag_title#TITLE=}
                    tag_tracknumber=`grep "^TRACKNUMBER=" $tags_filename | tail -n 1`
                    tag_tracknumber=${tag_tracknumber#TRACKNUMBER=}
                    if [ "$multi_disc" = "1" ]; then
                        tag_tracktotal=`grep "^TRACKTOTAL=" $tags_filename | tail -n 1`
                        tag_tracktotal=${tag_tracktotal#TRACKTOTAL=}
                        tag_discnumber=`grep "^DISCNUMBER=" $tags_filename | tail -n 1`
                        tag_discnumber=${tag_discnumber#DISCNUMBER=}
                    fi
                    printf "ARTIST=$tag_artist\n" > $tags_filename
                    printf "TITLE=$tag_title\n" >> $tags_filename
                    printf "ALBUM=$tag_album\n" >> $tags_filename
                    printf "DATE=$tag_date\n" >> $tags_filename
                    printf "TRACKNUMBER=$tag_tracknumber\n" >> $tags_filename
                    printf "TRACKTOTAL=$tag_tracktotal\n" >> $tags_filename
                    if [ "$multi_disc" = "1" ]; then
                        printf "DISCNUMBER=$tag_discnumber\n" >> $tags_filename
                        printf "DISCTOTAL=$tag_disctotal\n" >> $tags_filename
                    fi
                    if [ "$tag_comment" != "" ]; then
                        printf "COMMENT=$tag_comment\n" >> $tags_filename
                    fi
                    if [ "$tag_license" != "" ]; then
                        printf "LICENSE=$tag_license\n" >> $tags_filename
                    fi
                    old_size=`stat -c %s "$file"`
249 250
                    metaflac --remove --block-type=SEEKTABLE,PICTURE,CUESHEET "$file"
                    metaflac --remove-all-tags "$file"
251
                    metaflac --import-tags-from=$tags_filename "$file"
252
                    metaflac --dont-use-padding --remove --block-type=PADDING "$file"
253
                    new_size=`stat -c %s "$file"`
254
                    printf "(saved %d bytes) $file\n" $(($old_size - $new_size))
255 256 257 258 259 260 261 262 263 264 265
                done
            }
        rm -f $tags_filename
        # Set read-only permission
        chmod 444 *.flac

        # MP3 files
    elif [ "$mp3_files" != "" ]; then

        # Get album information from the first track
        file=`ls -1 *.mp3 | grep -m 1 mp3`
266
        print_header "Get album information from the first track ($file)"
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        id3v2 --list "$file" > $tags_filename
        tag_artist=`grep "^TPE1 " $tags_filename`
        tag_artist=${tag_artist#*: }
        tag_album=`grep "^TALB " $tags_filename`
        tag_album=${tag_album#*: }
        tag_date=`grep "^TYER " $tags_filename`
        tag_date=${tag_date#*: }
        if [ "${file%%.*}" = "1" ]; then
            multi_disc=1
        fi
        tag_comment=`grep "^COMM " $tags_filename | tail -n 1`
        tag_comment=${tag_comment#*: *: }

        # Print album information
        printf "\033[7m"
        printf "ARTIST=$tag_artist\n"
        printf "ALBUM=$tag_album\n"
        printf "DATE=$tag_date\n"
        if [ "$tag_comment" != "" ]; then
            printf "COMMENT=$tag_comment\n"
        fi
        printf "\033[0m"

        confirm_to_continue

        print_header "Set tags in all tracks"

        # Set read-write permission
        chmod 644 *.mp3

        # Set tags in all tracks
        ls -1 *.mp3 |
            {
                while read file
                do
                    id3v2 --list "$file" > $tags_filename
                    tag_title=`grep "^TIT2 " $tags_filename`
                    tag_title=${tag_title#*: }
                    tag_tracknumber=`grep "^TRCK " $tags_filename`
                    tag_tracknumber=${tag_tracknumber#*: }
                    if [ "$multi_disc" = "1" ]; then
                        tag_discnumber=`grep "^TPOS " $tags_filename`
                        tag_discnumber=${tag_discnumber#*: }
                    fi
                    old_size=`stat -c %s "$file"`
                    id3v2 --delete-all "$file" > /dev/null
                    id3v2 --id3v2-only --artist "$tag_artist" "$file"
                    id3v2 --id3v2-only --song "$tag_title" "$file"
                    id3v2 --id3v2-only --album "$tag_album" "$file"
                    id3v2 --id3v2-only --year "$tag_date" "$file"
                    id3v2 --id3v2-only --track "$tag_tracknumber" "$file"
                    if [ "$multi_disc" = "1" ]; then
                        id3v2 --id3v2-only --TPOS "$tag_discnumber" "$file"
                    fi
                    if [ "$tag_comment" != "" ]; then
                        id3v2 --id3v2-only --comment "$tag_comment" "$file"
                    fi
                    new_size=`stat -c %s "$file"`
325
                    printf "(saved %d bytes) $file\n" $(($old_size - $new_size))
326 327 328 329 330
                done
            }
        rm -f $tags_filename
        # Set read-only permission
        chmod 444 *.mp3
Cédric Marie's avatar
Cédric Marie committed
331 332

    fi
333 334
    exit 0
fi
Cédric Marie's avatar
Cédric Marie committed
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
# -----------------------------------------------------------------------------
# clean-single
# -----------------------------------------------------------------------------

if [ "$command" = "clean-single" ]; then

    year_only=0
    shift 1
    while getopts y OPT
    do
        case $OPT in
            y) year_only=1;;
            *) print_usage; exit 1;;
        esac
    done
    shift $(($OPTIND - 1))
    file=$1
Cédric Marie's avatar
Cédric Marie committed
353

354
    tags_filename=/tmp/tag-sieve-clean.tags
Cédric Marie's avatar
Cédric Marie committed
355

356 357
    # FLAC file
    if [ "${file%.flac}" != "$file" ]; then
Cédric Marie's avatar
Cédric Marie committed
358

359
        # Get information
Cédric Marie's avatar
Cédric Marie committed
360
        metaflac --export-tags-to=$tags_filename "$file"
361 362
        tag_artist=`grep "^ARTIST=" $tags_filename | tail -n 1`
        tag_artist=${tag_artist#ARTIST=}
363 364
        tag_title=`grep "^TITLE=" $tags_filename | tail -n 1`
        tag_title=${tag_title#TITLE=}
365 366 367 368
        tag_date=`grep "^DATE=" $tags_filename | tail -n 1`
        tag_date=${tag_date#DATE=}
        if [ "$year_only" = "1" ]; then
            tag_date=${tag_date%%-*}
Cédric Marie's avatar
Cédric Marie committed
369
        fi
370 371 372 373 374 375 376 377 378 379 380 381
        tag_comment=`grep "^COMMENT=" $tags_filename | tail -n 1`
        tag_comment=${tag_comment#COMMENT=}
        tag_license=`grep "^LICENSE=" $tags_filename | tail -n 1`
        tag_license=${tag_license#LICENSE=}

        print_header "Set tags in this file"

        # Set read-write permission
        chmod 644 "$file"

        # Set tags in this file

Cédric Marie's avatar
Cédric Marie committed
382 383 384 385
        printf "ARTIST=$tag_artist\n" > $tags_filename
        printf "TITLE=$tag_title\n" >> $tags_filename
        printf "DATE=$tag_date\n" >> $tags_filename
        if [ "$tag_comment" != "" ]; then
386
            printf "COMMENT=$tag_comment\n" >> $tags_filename
Cédric Marie's avatar
Cédric Marie committed
387 388
        fi
        if [ "$tag_license" != "" ]; then
389
            printf "LICENSE=$tag_license\n" >> $tags_filename
Cédric Marie's avatar
Cédric Marie committed
390 391
        fi
        old_size=`stat -c %s "$file"`
392 393
        metaflac --remove --block-type=SEEKTABLE,PICTURE,CUESHEET "$file"
        metaflac --remove-all-tags "$file"
Cédric Marie's avatar
Cédric Marie committed
394
        metaflac --import-tags-from=$tags_filename "$file"
395
        metaflac --dont-use-padding --remove --block-type=PADDING "$file"
Cédric Marie's avatar
Cédric Marie committed
396
        new_size=`stat -c %s "$file"`
397
        printf "(saved %d bytes) $file\n" $(($old_size - $new_size))
398

399 400 401
        rm -f $tags_filename
        # Set read-only permission
        chmod 444 "$file"
402

403 404
        # MP3 file
    elif [ "${file%.mp3}" != "$file" ]; then
405

406
        # Get information
407
        id3v2 --list "$file" > $tags_filename
408 409
        tag_artist=`grep "^TPE1 " $tags_filename`
        tag_artist=${tag_artist#*: }
410 411
        tag_title=`grep "^TIT2 " $tags_filename`
        tag_title=${tag_title#*: }
412 413 414 415 416 417 418 419 420 421 422
        tag_date=`grep "^TYER " $tags_filename`
        tag_date=${tag_date#*: }
        tag_comment=`grep "^COMM " $tags_filename | tail -n 1`
        tag_comment=${tag_comment#*: *: }

        print_header "Set tags in this file"

        # Set read-write permission
        chmod 644 "$file"

        id3v2 --list "$file" > $tags_filename
423 424 425 426 427
        old_size=`stat -c %s "$file"`
        id3v2 --delete-all "$file" > /dev/null
        id3v2 --id3v2-only --artist "$tag_artist" "$file"
        id3v2 --id3v2-only --song "$tag_title" "$file"
        id3v2 --id3v2-only --year "$tag_date" "$file"
428 429
        if [ "$tag_comment" != "" ]; then
            id3v2 --id3v2-only --comment "$tag_comment" "$file"
430
        fi
431
        new_size=`stat -c %s "$file"`
432
        printf "(saved %d bytes) $file\n" $(($old_size - $new_size))
433 434 435 436 437 438 439

        rm -f $tags_filename
        # Set read-only permission
        chmod 444 "$file"

    fi
    exit 0
Cédric Marie's avatar
Cédric Marie committed
440 441
fi

442
# -----------------------------------------------------------------------------
443
# seta
444 445
# -----------------------------------------------------------------------------

446
if [ "$command" = "seta" ]; then
447

448 449 450 451 452 453 454 455 456 457 458 459 460 461
    tag_artist=$2
    tag_album=$3
    tag_date=$4
    if [ "$tag_artist" = "" ]; then
        printf "ARTIST parameter is missing\n"
        exit 1
    fi
    if [ "$tag_album" = "" ]; then
        printf "ALBUM parameter is missing\n"
        exit 1
    fi
    if [ "$tag_date" = "" ]; then
        printf "DATE parameter is missing\n"
        exit 1
462 463
    fi

464 465 466 467 468 469
    # Print album information
    printf "\033[7m"
    printf "ARTIST=$tag_artist\n"
    printf "ALBUM=$tag_album\n"
    printf "DATE=$tag_date\n"
    printf "\033[0m"
470

471
    confirm_to_continue
472

473
    print_header "Set artist, album, and date in the first track"
474

475 476
    # FLAC files
    if [ "$flac_files" != "" ]; then
477

478 479 480 481 482 483 484 485
        file=`ls -1 *.flac | grep -m 1 flac`
        # Set read-write permission
        chmod 644 "$file"
        metaflac --set-tag="ARTIST=$tag_artist" "$file"
        metaflac --set-tag="ALBUM=$tag_album" "$file"
        metaflac --set-tag="DATE=$tag_date" "$file"
        # Set read-only permission
        chmod 444 "$file"
486

487 488
        # MP3 files
    elif [ "$mp3_files" != "" ]; then
489

490 491 492 493 494 495 496 497
        file=`ls -1 *.mp3 | grep -m 1 mp3`
        # Set read-write permission
        chmod 644 "$file"
        id3v2 --id3v2-only --artist "$tag_artist" "$file"
        id3v2 --id3v2-only --album "$tag_album" "$file"
        id3v2 --id3v2-only --year "$tag_date" "$file"
        # Set read-only permission
        chmod 444 "$file"
498

499 500
    fi
    exit 0
501 502
fi

Cédric Marie's avatar
Cédric Marie committed
503 504 505 506 507 508
# -----------------------------------------------------------------------------
# setc
# -----------------------------------------------------------------------------

if [ "$command" = "setc" ]; then

509 510 511 512 513
    tag_comment=$2
    if [ "$tag_comment" = "" ]; then
        printf "COMMENT parameter is missing\n"
        exit 1
    fi
Cédric Marie's avatar
Cédric Marie committed
514

515 516 517 518
    # Print comment
    printf "\033[7m"
    printf "COMMENT=$tag_comment\n"
    printf "\033[0m"
Cédric Marie's avatar
Cédric Marie committed
519

520
    confirm_to_continue
Cédric Marie's avatar
Cédric Marie committed
521

522
    print_header "Set comment in the first track"
Cédric Marie's avatar
Cédric Marie committed
523

524 525
    # FLAC files
    if [ "$flac_files" != "" ]; then
Cédric Marie's avatar
Cédric Marie committed
526

527 528 529 530 531 532
        file=`ls -1 *.flac | grep -m 1 flac`
        # Set read-write permission
        chmod 644 "$file"
        metaflac --set-tag="COMMENT=$tag_comment" "$file"
        # Set read-only permission
        chmod 444 "$file"
Cédric Marie's avatar
Cédric Marie committed
533

534 535
        # MP3 files
    elif [ "$mp3_files" != "" ]; then
Cédric Marie's avatar
Cédric Marie committed
536

537 538 539 540 541 542
        file=`ls -1 *.mp3 | grep -m 1 mp3`
        # Set read-write permission
        chmod 644 "$file"
        id3v2 --id3v2-only --comment "$tag_comment" "$file"
        # Set read-only permission
        chmod 444 "$file"
Cédric Marie's avatar
Cédric Marie committed
543

544 545
    fi
    exit 0
Cédric Marie's avatar
Cédric Marie committed
546 547
fi

548 549 550 551 552 553
# -----------------------------------------------------------------------------
# set-single
# -----------------------------------------------------------------------------

if [ "$command" = "set-single" ]; then

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    file=$2
    tag_artist=$3
    tag_title=$4
    tag_date=$5
    if [ "$tag_artist" = "" ]; then
        printf "ARTIST parameter is missing\n"
        exit 1
    fi
    if [ "$tag_title" = "" ]; then
        printf "TITLE parameter is missing\n"
        exit 1
    fi
    if [ "$tag_date" = "" ]; then
        printf "DATE parameter is missing\n"
        exit 1
    fi

    # Print album information
    printf "\033[7m"
    printf "ARTIST=$tag_artist\n"
    printf "TITLE=$tag_title\n"
    printf "DATE=$tag_date\n"
    printf "\033[0m"

    confirm_to_continue

    print_header "Set artist, title, and date in this file"

    # FLAC files
    if [ "$flac_files" != "" ]; then

        # Set read-write permission
        chmod 644 "$file"
        metaflac --set-tag="ARTIST=$tag_artist" "$file"
        metaflac --set-tag="TITLE=$tag_title" "$file"
        metaflac --set-tag="DATE=$tag_date" "$file"
        # Set read-only permission
        chmod 444 "$file"

        # MP3 files
    elif [ "$mp3_files" != "" ]; then

        # Set read-write permission
        chmod 644 "$file"
        id3v2 --id3v2-only --artist "$tag_artist" "$file"
        id3v2 --id3v2-only --song "$tag_title" "$file"
        id3v2 --id3v2-only --year "$tag_date" "$file"
        # Set read-only permission
        chmod 444 "$file"

    fi
    exit 0
606 607
fi

Cédric Marie's avatar
Cédric Marie committed
608
# -----------------------------------------------------------------------------
609
# update
Cédric Marie's avatar
Cédric Marie committed
610 611
# -----------------------------------------------------------------------------

612
if [ "$command" = "update" ]; then
613

614
    multi_disc=0
615

616 617
    # FLAC files
    if [ "$flac_files" != "" ]; then
618

619
        first_file=`ls -1 *.flac | grep -m 1 flac`
620

621 622 623 624 625
        if [ "${first_file%%.*}" = "1" ]; then
            multi_disc=1
            current_disc=
            last_file=`ls -1r *.flac | grep -m 1 flac`
            tag_disctotal=${last_file%%.*}
626
            print_header "Set title and track/disc number/total in all tracks (from filenames)"
627
        else
628
            tag_tracktotal=`ls -1 *.flac | grep -c flac`
629
            print_header "Set title and track number/total in all tracks (from filenames)"
630 631
        fi

632 633 634 635 636 637 638
        # Set read-write permission
        chmod 644 *.flac

        ls -1 *.flac |
            {
                while read file
                do
639 640 641
                    # Remove .flac and leading disc and track numbers
                    tag_title=`echo ${file%.flac} | sed 's/^[1-9]\.//' | sed 's/^[0-9]\+\. //'`
                    metaflac --set-tag="TITLE=$tag_title" "$file"
642 643 644 645 646 647 648 649 650 651 652 653 654 655
                    # Remove .flac and leading disc number (if present)
                    tag_tracknumber=`echo ${file%.flac} | sed 's/^[1-9]\.//'`
                    # Get the track number without leading 0
                    tag_tracknumber=${tag_tracknumber%%.*}
                    tag_tracknumber=${tag_tracknumber#0}
                    if [ "$multi_disc" = "1" ]; then
                        tag_discnumber=${file%%.*}
                        if [ "$tag_discnumber" != "$current_disc" ]; then
                            # Get the number of tracks for this disc
                            current_disc=$tag_discnumber
                            tag_tracktotal=`ls -1 ${tag_discnumber}.*.flac | grep -c flac`
                        fi
                        metaflac --set-tag="DISCNUMBER=$tag_discnumber" "$file"
                        metaflac --set-tag="DISCTOTAL=$tag_disctotal" "$file"
656
                        printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m, disc \033[7m$tag_discnumber\033[0m/\033[7m$tag_disctotal\033[0m: "
657
                    else
658
                        printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m: "
659
                    fi
660
                    printf "\033[7m$tag_title\033[0m ($file)\n"
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                    metaflac --set-tag="TRACKNUMBER=$tag_tracknumber" "$file"
                    metaflac --set-tag="TRACKTOTAL=$tag_tracktotal" "$file"
                done
            }
        # Set read-only permission
        chmod 444 *.flac

        # MP3 files
    elif [ "$mp3_files" != "" ]; then

        first_file=`ls -1 *.mp3 | grep -m 1 mp3`

        if [ "${first_file%%.*}" = "1" ]; then
            multi_disc=1
            current_disc=
            last_file=`ls -1r *.mp3 | grep -m 1 mp3`
            tag_disctotal=${last_file%%.*}
678
            print_header "Set title and track/disc number/total in all tracks (from filenames)"
679
        else
680
            tag_tracktotal=`ls -1 *.mp3 | grep -c mp3`
681
            print_header "Set title and track number/total in all tracks (from filenames)"
682
        fi
683 684 685 686 687 688 689 690

        # Set read-write permission
        chmod 644 *.mp3

        ls -1 *.mp3 |
            {
                while read file
                do
691 692 693
                    # Remove .mp3 and leading disc and track numbers
                    tag_title=`echo ${file%.mp3} | sed 's/^[1-9]\.//' | sed 's/^[0-9]\+\. //'`
                    id3v2 --id3v2-only --song "$tag_title" "$file"
694 695 696 697 698 699 700 701 702 703 704 705 706
                    # Remove .mp3 and leading disc number
                    tag_tracknumber=`echo ${file%.mp3} | sed 's/^[1-9]\.//'`
                    # Get the track number without leading 0
                    tag_tracknumber=${tag_tracknumber%%.*}
                    tag_tracknumber=${tag_tracknumber#0}
                    if [ "$multi_disc" = "1" ]; then
                        tag_discnumber=${file%%.*}
                        if [ "$tag_discnumber" != "$current_disc" ]; then
                            # Get the number of tracks for this disc
                            current_disc=$tag_discnumber
                            tag_tracktotal=`ls -1 ${tag_discnumber}.*.mp3 | grep -c mp3`
                        fi
                        id3v2 --id3v2-only --TPOS "$tag_discnumber/$tag_disctotal" "$file"
707
                        printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m, disc \033[7m$tag_discnumber\033[0m/\033[7m$tag_disctotal\033[0m: "
708
                    else
709
                        printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m: "
710
                    fi
711
                    printf "\033[7m$tag_title\033[0m ($file)\n"
712 713 714 715 716 717 718 719
                    id3v2 --id3v2-only --track "$tag_tracknumber/$tag_tracktotal" "$file"
                done
            }
        # Set read-only permission
        chmod 444 *.mp3

    fi
    exit 0
720 721
fi

Cédric Marie's avatar
Cédric Marie committed
722 723 724 725 726 727
# -----------------------------------------------------------------------------
# unknown command
# -----------------------------------------------------------------------------

print_usage
exit 1