myaudiotags 25.9 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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"
    printf "  upt       update title tags from filenames\n"
    printf "  uptn      update track number tags from filenames\n\n"
    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
59 60 61 62
}

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

confirm_to_continue()
{
68 69 70 71 72
    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
73 74 75 76
}

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

# Find audio file type
flac_files=`ls *.flac 2> /dev/null`
if [ "$flac_files" = "" ]; then
86 87 88 89 90
    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
91 92 93 94 95 96 97 98 99 100
fi

command=$1

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

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

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 126
    # 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
127 128 129 130 131 132 133 134
fi

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

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

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    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`
155
        print_header "Get album information from the first track ($file)"
156 157 158 159 160 161 162 163 164
        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%%-*}
165
        fi
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 249
        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"`
250 251
                    metaflac --remove --block-type=SEEKTABLE,PICTURE,CUESHEET "$file"
                    metaflac --remove-all-tags "$file"
252
                    metaflac --import-tags-from=$tags_filename "$file"
253
                    metaflac --dont-use-padding --remove --block-type=PADDING "$file"
254 255 256 257 258 259 260 261 262 263 264 265 266
                    new_size=`stat -c %s "$file"`
                    printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
                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`
267
        print_header "Get album information from the first track ($file)"
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 325 326 327 328 329 330 331
        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"`
                    printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
                done
            }
        rm -f $tags_filename
        # Set read-only permission
        chmod 444 *.mp3
Cédric Marie's avatar
Cédric Marie committed
332 333

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

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
# -----------------------------------------------------------------------------
# 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
354

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

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

360
        # Get information
Cédric Marie's avatar
Cédric Marie committed
361
        metaflac --export-tags-to=$tags_filename "$file"
362 363
        tag_artist=`grep "^ARTIST=" $tags_filename | tail -n 1`
        tag_artist=${tag_artist#ARTIST=}
364 365
        tag_title=`grep "^TITLE=" $tags_filename | tail -n 1`
        tag_title=${tag_title#TITLE=}
366 367 368 369
        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
370
        fi
371 372 373 374 375 376 377 378 379 380 381 382
        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
383 384 385 386
        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
387
            printf "COMMENT=$tag_comment\n" >> $tags_filename
Cédric Marie's avatar
Cédric Marie committed
388 389
        fi
        if [ "$tag_license" != "" ]; then
390
            printf "LICENSE=$tag_license\n" >> $tags_filename
Cédric Marie's avatar
Cédric Marie committed
391 392 393 394 395 396
        fi
        old_size=`stat -c %s "$file"`
        metaflac --remove-all-tags --dont-use-padding "$file"
        metaflac --import-tags-from=$tags_filename "$file"
        new_size=`stat -c %s "$file"`
        printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
397

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

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

405
        # Get information
406
        id3v2 --list "$file" > $tags_filename
407 408
        tag_artist=`grep "^TPE1 " $tags_filename`
        tag_artist=${tag_artist#*: }
409 410
        tag_title=`grep "^TIT2 " $tags_filename`
        tag_title=${tag_title#*: }
411 412 413 414 415 416 417 418 419 420 421
        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
422 423 424 425 426
        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"
427 428
        if [ "$tag_comment" != "" ]; then
            id3v2 --id3v2-only --comment "$tag_comment" "$file"
429
        fi
430 431
        new_size=`stat -c %s "$file"`
        printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
432 433 434 435 436 437 438

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

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

441
# -----------------------------------------------------------------------------
442
# seta
443 444
# -----------------------------------------------------------------------------

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

447 448 449 450 451 452 453 454 455 456 457 458 459 460
    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
461 462
    fi

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

470
    confirm_to_continue
471

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

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

477 478 479 480 481 482 483 484
        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"
485

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

489 490 491 492 493 494 495 496
        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"
497

498 499
    fi
    exit 0
500 501
fi

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

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

508 509 510 511 512
    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
513

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

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

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

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

526 527 528 529 530 531
        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
532

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

536 537 538 539 540 541
        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
542

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

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

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

553 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
    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
605 606
fi

Cédric Marie's avatar
Cédric Marie committed
607 608 609 610 611 612
# -----------------------------------------------------------------------------
# upt
# -----------------------------------------------------------------------------

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

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    print_header "Set title from filename in all tracks"

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

        # Set read-write permission
        chmod 644 *.flac

        # Update title tag in all tracks
        ls -1 *.flac |
            {
                while read file
                do
                    # Remove .flac and leading disc and track numbers
                    tag_title=`echo ${file%.flac} | sed 's/^[1-9]\.//' | sed 's/^[0-9]\+\. //'`
                    printf "$file: \033[7m$tag_title\033[0m\n"
                    metaflac --set-tag="TITLE=$tag_title" "$file"
                done
            }
        # Set read-only permission
        chmod 444 *.flac

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

        # Set read-write permission
        chmod 644 *.mp3

        # Update title tag in all tracks
        ls -1 *.mp3 |
            {
                while read file
                do
                    # Remove .mp3 and leading disc and track numbers
                    tag_title=`echo ${file%.mp3} | sed 's/^[1-9]\.//' | sed 's/^[0-9]\+\. //'`
                    printf "$file: \033[7m$tag_title\033[0m\n"
                    id3v2 --id3v2-only --song "$tag_title" "$file"
                done
            }
        # Set read-only permission
        chmod 444 *.mp3
Cédric Marie's avatar
Cédric Marie committed
654

655 656
    fi
    exit 0
657 658
fi

659 660 661 662 663 664
# -----------------------------------------------------------------------------
# uptn
# -----------------------------------------------------------------------------

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

665
    multi_disc=0
666

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

670
        first_file=`ls -1 *.flac | grep -m 1 flac`
671

672 673 674 675 676 677
        if [ "${first_file%%.*}" = "1" ]; then
            multi_disc=1
            current_disc=
            last_file=`ls -1r *.flac | grep -m 1 flac`
            tag_disctotal=${last_file%%.*}
            print_header "Set track/disc numbers from filename and track/disc total in all tracks"
678
        else
679 680
            tag_tracktotal=`ls -1 *.flac | grep -c flac`
            print_header "Set track numbers from filename and track total in all tracks"
681 682
        fi

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
        # Set read-write permission
        chmod 644 *.flac

        ls -1 *.flac |
            {
                while read file
                do
                    # 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"
                        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: $file\n"
                    else
                        printf "track \033[7m$tag_tracknumber\033[0m / \033[7m$tag_tracktotal\033[0m: $file\n"
                    fi
                    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%%.*}
            print_header "Set track/disc numbers from filename in all tracks"
726
        else
727 728
            tag_tracktotal=`ls -1 *.mp3 | grep -c mp3`
            print_header "Set track numbers from filename in all tracks"
729
        fi
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

        # Set read-write permission
        chmod 644 *.mp3

        ls -1 *.mp3 |
            {
                while read file
                do
                    # 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"
                        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: $file\n"
                    else
                        printf "track \033[7m$tag_tracknumber\033[0m / \033[7m$tag_tracktotal\033[0m: $file\n"
                    fi
                    id3v2 --id3v2-only --track "$tag_tracknumber/$tag_tracktotal" "$file"
                done
            }
        # Set read-only permission
        chmod 444 *.mp3

    fi
    exit 0
763 764
fi

Cédric Marie's avatar
Cédric Marie committed
765 766 767 768 769 770
# -----------------------------------------------------------------------------
# unknown command
# -----------------------------------------------------------------------------

print_usage
exit 1