myaudiotags 26.7 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    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"
    printf "  rmblocks  remove seek table, picture, cue sheet, and padding blocks\n"
    printf "            (FLAC files only)\n"
    printf "  clean     set only following tags (and remove padding):\n"
    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"
    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
60 61 62 63
}

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

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

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

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

command=$1

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

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

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 127
    # 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
128 129 130
fi

# -----------------------------------------------------------------------------
131
# rmblocks (FLAC only)
Cédric Marie's avatar
Cédric Marie committed
132 133 134 135
# -----------------------------------------------------------------------------

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    # FLAC files
    if [ "$flac_files" != "" ]; then

        print_header "Remove seek table, picture, cue sheet, and padding blocks in all tracks"
        # Set read-write permission
        chmod 644 *.flac
        # Remove seek table, picture, cue sheet, and padding blocks
        metaflac --remove --block-type=SEEKTABLE *.flac
        metaflac --remove --block-type=PICTURE *.flac
        metaflac --remove --block-type=CUESHEET *.flac
        metaflac --dont-use-padding --remove --block-type=PADDING *.flac
        # Set read-only permission
        chmod 444 *.flac

    fi
    exit 0
Cédric Marie's avatar
Cédric Marie committed
152 153 154 155 156 157 158 159
fi

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

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

160 161 162 163 164 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
    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
        print_header "Get album information from the first track ($file)"
        file=`ls -1 *.flac | grep -m 1 flac`
        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%%-*}
190
        fi
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        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"`
                    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
        print_header "Get album information from the first track ($file)"
        file=`ls -1 *.mp3 | grep -m 1 mp3`
        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
355 356

    fi
357 358
    exit 0
fi
Cédric Marie's avatar
Cédric Marie committed
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
# -----------------------------------------------------------------------------
# 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
377

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

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

383
        # Get information
Cédric Marie's avatar
Cédric Marie committed
384
        metaflac --export-tags-to=$tags_filename "$file"
385 386
        tag_artist=`grep "^ARTIST=" $tags_filename | tail -n 1`
        tag_artist=${tag_artist#ARTIST=}
387 388
        tag_title=`grep "^TITLE=" $tags_filename | tail -n 1`
        tag_title=${tag_title#TITLE=}
389 390 391 392
        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
393
        fi
394 395 396 397 398 399 400 401 402 403 404 405
        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
406 407 408 409
        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
410
            printf "COMMENT=$tag_comment\n" >> $tags_filename
Cédric Marie's avatar
Cédric Marie committed
411 412
        fi
        if [ "$tag_license" != "" ]; then
413
            printf "LICENSE=$tag_license\n" >> $tags_filename
Cédric Marie's avatar
Cédric Marie committed
414 415 416 417 418 419
        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))
420

421 422 423
        rm -f $tags_filename
        # Set read-only permission
        chmod 444 "$file"
424

425 426
        # MP3 file
    elif [ "${file%.mp3}" != "$file" ]; then
427

428
        # Get information
429
        id3v2 --list "$file" > $tags_filename
430 431
        tag_artist=`grep "^TPE1 " $tags_filename`
        tag_artist=${tag_artist#*: }
432 433
        tag_title=`grep "^TIT2 " $tags_filename`
        tag_title=${tag_title#*: }
434 435 436 437 438 439 440 441 442 443 444
        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
445 446 447 448 449
        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"
450 451
        if [ "$tag_comment" != "" ]; then
            id3v2 --id3v2-only --comment "$tag_comment" "$file"
452
        fi
453 454
        new_size=`stat -c %s "$file"`
        printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
455 456 457 458 459 460 461

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

    fi
    exit 0
Cédric Marie's avatar
Cédric Marie committed
462 463
fi

464
# -----------------------------------------------------------------------------
465
# seta
466 467
# -----------------------------------------------------------------------------

468
if [ "$command" = "seta" ]; then
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483
    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
484 485
    fi

486 487 488 489 490 491
    # Print album information
    printf "\033[7m"
    printf "ARTIST=$tag_artist\n"
    printf "ALBUM=$tag_album\n"
    printf "DATE=$tag_date\n"
    printf "\033[0m"
492

493
    confirm_to_continue
494

495
    print_header "Set artist, album, and date in the first track"
496

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

500 501 502 503 504 505 506 507
        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"
508

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

512 513 514 515 516 517 518 519
        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"
520

521 522
    fi
    exit 0
523 524
fi

Cédric Marie's avatar
Cédric Marie committed
525 526 527 528 529 530
# -----------------------------------------------------------------------------
# setc
# -----------------------------------------------------------------------------

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

531 532 533 534 535
    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
536

537 538 539 540
    # Print comment
    printf "\033[7m"
    printf "COMMENT=$tag_comment\n"
    printf "\033[0m"
Cédric Marie's avatar
Cédric Marie committed
541

542
    confirm_to_continue
Cédric Marie's avatar
Cédric Marie committed
543

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

546 547
    # FLAC files
    if [ "$flac_files" != "" ]; then
Cédric Marie's avatar
Cédric Marie committed
548

549 550 551 552 553 554
        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
555

556 557
        # MP3 files
    elif [ "$mp3_files" != "" ]; then
Cédric Marie's avatar
Cédric Marie committed
558

559 560 561 562 563 564
        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
565

566 567
    fi
    exit 0
Cédric Marie's avatar
Cédric Marie committed
568 569
fi

570 571 572 573 574 575
# -----------------------------------------------------------------------------
# set-single
# -----------------------------------------------------------------------------

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

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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    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
628 629
fi

Cédric Marie's avatar
Cédric Marie committed
630 631 632 633 634 635
# -----------------------------------------------------------------------------
# upt
# -----------------------------------------------------------------------------

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

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    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
677

678 679
    fi
    exit 0
680 681
fi

682 683 684 685 686 687
# -----------------------------------------------------------------------------
# uptn
# -----------------------------------------------------------------------------

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

688
    multi_disc=0
689

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

693
        first_file=`ls -1 *.flac | grep -m 1 flac`
694

695 696 697 698 699 700
        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"
701
        else
702 703
            tag_tracktotal=`ls -1 *.flac | grep -c flac`
            print_header "Set track numbers from filename and track total in all tracks"
704 705
        fi

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
        # 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"
749
        else
750 751
            tag_tracktotal=`ls -1 *.mp3 | grep -c mp3`
            print_header "Set track numbers from filename in all tracks"
752
        fi
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

        # 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
786 787
fi

Cédric Marie's avatar
Cédric Marie committed
788 789 790 791 792 793
# -----------------------------------------------------------------------------
# unknown command
# -----------------------------------------------------------------------------

print_usage
exit 1