Skip to content

Instantly share code, notes, and snippets.

@netj
Last active March 12, 2019 09:47
Show Gist options
  • Save netj/47e6e55e313c75a4cf0133e5ab92e603 to your computer and use it in GitHub Desktop.
Save netj/47e6e55e313c75a4cf0133e5ab92e603 to your computer and use it in GitHub Desktop.

Revisions

  1. netj revised this gist Mar 12, 2019. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions rename-audio-files.sh
    Original file line number Diff line number Diff line change
    @@ -8,26 +8,33 @@
    # Created: 2019-03-10
    set -euo pipefail

    {
    type ffprobe
    type jq
    } >/dev/null

    [[ $# -gt 0 ]] || set -- *

    RENAME() { local "$@"; [[ "$FROM" -ef "$TO" ]] || mv -vf "$FROM" "$TO"; }
    declare -p -f RENAME

    find "$@" \( -name "*.m4a" -o -name "*.mp3" \) -print0 |
    xargs -0 -L1 ffprobe -v quiet -print_format json -show_format -i |
    #jq -c . | head | # for debugging/quickiterations
    # jq -c . | head | jq; exit #| # for debugging/quickiterations
    jq '
    .format |
    (".\(.filename | sub(".*\\."; ""))") as $fileext |
    (.tags |
    [ .album
    , ([(.disc, .track) | sub("/.*"; "")?] | join("."))
    [ .album // empty
    , ([( (.disc | sub("/.*"; "")?)
    , (.track | sub("/.*"; "")? | if tonumber < 10 then "0\(.)" else . end)
    )] | join("."))
    , .title
    , .artist
    ] | map( gsub("[/]"; "-")
    , .artist // empty
    ] | map(( gsub("[/]"; "-")
    | sub("^[[:space:]]+"; "")
    | sub("[[:space:]]+$"; "")
    ) | join("—") + $fileext
    )?) | join("—") + $fileext
    ) as $normalized_name |
    select(.filename != $normalized_name) |
    @sh "RENAME TO=\($normalized_name)\tFROM=\(.filename)"
  2. netj revised this gist Mar 11, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions rename-audio-files.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    #!/usr/bin/env bash
    # rename-audio-files.sh -- prints a shell script to rename .m4a, .mp3 files according to its tags
    #
    # Why/where would anyone use this:
    # My car's USB audio (iDrive) works much better if the files are named with their metadata than the terse names (track-title) given by iTunes.
    #
    # Author: Jaeho Shin <[email protected]>
    # Created: 2019-03-10
    set -euo pipefail
  3. netj created this gist Mar 11, 2019.
    31 changes: 31 additions & 0 deletions rename-audio-files.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env bash
    # rename-audio-files.sh -- prints a shell script to rename .m4a, .mp3 files according to its tags
    #
    # Author: Jaeho Shin <[email protected]>
    # Created: 2019-03-10
    set -euo pipefail

    [[ $# -gt 0 ]] || set -- *

    RENAME() { local "$@"; [[ "$FROM" -ef "$TO" ]] || mv -vf "$FROM" "$TO"; }
    declare -p -f RENAME

    find "$@" \( -name "*.m4a" -o -name "*.mp3" \) -print0 |
    xargs -0 -L1 ffprobe -v quiet -print_format json -show_format -i |
    #jq -c . | head | # for debugging/quickiterations
    jq '
    .format |
    (".\(.filename | sub(".*\\."; ""))") as $fileext |
    (.tags |
    [ .album
    , ([(.disc, .track) | sub("/.*"; "")?] | join("."))
    , .title
    , .artist
    ] | map( gsub("[/]"; "-")
    | sub("^[[:space:]]+"; "")
    | sub("[[:space:]]+$"; "")
    ) | join("—") + $fileext
    ) as $normalized_name |
    select(.filename != $normalized_name) |
    @sh "RENAME TO=\($normalized_name)\tFROM=\(.filename)"
    ' -cr