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
                    metaflac --dont-use-padding --remove --block-type=SEEKTABLE,PICTURE,CUESHEET,PADDING "$file"
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                    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))
                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 325 326 327 328 329 330
        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
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 392 393 394 395
        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))
396

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

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

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

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

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

440
# -----------------------------------------------------------------------------
441
# seta
442 443
# -----------------------------------------------------------------------------

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

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

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

469
    confirm_to_continue
470

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

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

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

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

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

497 498
    fi
    exit 0
499 500
fi

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

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

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

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

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

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

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

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

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

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

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

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

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

552 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
    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
604 605
fi

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

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

612 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
    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
653

654 655
    fi
    exit 0
656 657
fi

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

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

664
    multi_disc=0
665

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

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

671 672 673 674 675 676
        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"
677
        else
678 679
            tag_tracktotal=`ls -1 *.flac | grep -c flac`
            print_header "Set track numbers from filename and track total in all tracks"
680 681
        fi

682 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
        # 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"
725
        else
726 727
            tag_tracktotal=`ls -1 *.mp3 | grep -c mp3`
            print_header "Set track numbers from filename in all tracks"
728
        fi
729 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

        # 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
762 763
fi

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

print_usage
exit 1