Skip to main content
added 93 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

You can do this using the id3v2 tool, which should be in the repositories for your operating system (note that this solution assumes GNU grep, the default if you are running Linux):

## Iterate over all file/dir names ending in mp3
for file in /path/to/dir/with/mp3/files/*mp3; do 
    ## read the title and save in the variable $title
    title=$(id3v2 -l "$file" | grep -oP '^Title\s*'^(Title\s*|TIT2\s*.*\)):\K(.*?)(?=Artist:)'); 
    ## check if this title matches ytversion
    if [[ "$title" =~ "ytversion" ]]; then 
        ## If it does, replace ytversion with mqversion and 
        ## save in the new variable $newTitle
        newTitle=$(sed 's/ytversion/mqversion/g' <<<"$title") 
        ## Set the tite tag for this file to the value in $newTitle
        id3v2 -t "$newTitle" "$file" 
    fi
done

This si slightly complicated because the id3v2 tool prints the title and artist on the same line:

$ id3v2 -l foo.mp3 
id3v1 tag info for foo.mp3:
Title  : : title with mqversion string   Artist:                               
Album  :                                 Year:     , Genre: Unknown (255)
Comment:                                 Track: 0
id3v2 tag info for foo.mp3:
TENC (Encoded by): iTunes v7.0
TIT2 (Title/songname/content description): : title with mqversion string     

The -o flag tells grep to only print the matching portion of a line, and the -P enables PCRE regular expressions. The regex is searching for either a line starting with Title followed by 0 or more whitespace characters and then an : (^Title\s*:) or a line starting with TIT2, and then having a ): (^TIT2\s*.*\)). Everything matched up to that point is then discarded by the \K. it then searches for the shortest string of characters (.*?) followed by Artist:.((?=Artist:); this is called a positive lookahead and matches the string you are looking for without having it count in the match, so it isn't printed by grep).

You can do this using the id3v2 tool, which should be in the repositories for your operating system (note that this solution assumes GNU grep, the default if you are running Linux):

## Iterate over all file/dir names ending in mp3
for file in /path/to/dir/with/mp3/files/*mp3; do 
    ## read the title and save in the variable $title
    title=$(id3v2 -l "$file" | grep -oP '^Title\s*:\K(.*?)(?=Artist:)'); 
    ## check if this title matches ytversion
    if [[ "$title" =~ "ytversion" ]]; then 
        ## If it does, replace ytversion with mqversion and 
        ## save in the new variable $newTitle
        newTitle=$(sed 's/ytversion/mqversion/g' <<<"$title") 
        ## Set the tite tag for this file to the value in $newTitle
        id3v2 -t "$newTitle" "$file" 
    fi
done

This si slightly complicated because the id3v2 tool prints the title and artist on the same line:

$ id3v2 -l foo.mp3 
id3v1 tag info for foo.mp3:
Title  : : title with mqversion string   Artist:                               
Album  :                                 Year:     , Genre: Unknown (255)
Comment:                                 Track: 0
id3v2 tag info for foo.mp3:
TENC (Encoded by): iTunes v7.0
TIT2 (Title/songname/content description): : title with mqversion string     

The -o flag tells grep to only print the matching portion of a line, and the -P enables PCRE regular expressions. The regex is searching for a line starting with Title followed by 0 or more whitespace characters and then an : (^Title\s*:). Everything matched up to that point is then discarded by the \K. it then searches for the shortest string of characters (.*?) followed by Artist:.((?=Artist:); this is called a positive lookahead and matches the string you are looking for without having it count in the match, so it isn't printed by grep).

You can do this using the id3v2 tool, which should be in the repositories for your operating system (note that this solution assumes GNU grep, the default if you are running Linux):

## Iterate over all file/dir names ending in mp3
for file in /path/to/dir/with/mp3/files/*mp3; do 
    ## read the title and save in the variable $title
    title=$(id3v2 -l "$file" | grep -oP '^(Title\s*|TIT2\s*.*\)):\K(.*?)(?=Artist:)'); 
    ## check if this title matches ytversion
    if [[ "$title" =~ "ytversion" ]]; then 
        ## If it does, replace ytversion with mqversion and 
        ## save in the new variable $newTitle
        newTitle=$(sed 's/ytversion/mqversion/g' <<<"$title") 
        ## Set the tite tag for this file to the value in $newTitle
        id3v2 -t "$newTitle" "$file" 
    fi
done

This si slightly complicated because the id3v2 tool prints the title and artist on the same line:

$ id3v2 -l foo.mp3 
id3v1 tag info for foo.mp3:
Title  : : title with mqversion string   Artist:                               
Album  :                                 Year:     , Genre: Unknown (255)
Comment:                                 Track: 0
id3v2 tag info for foo.mp3:
TENC (Encoded by): iTunes v7.0
TIT2 (Title/songname/content description): : title with mqversion string     

The -o flag tells grep to only print the matching portion of a line, and the -P enables PCRE regular expressions. The regex is searching for either a line starting with Title followed by 0 or more whitespace characters and then an : (^Title\s*:) or a line starting with TIT2, and then having a ): (^TIT2\s*.*\)). Everything matched up to that point is then discarded by the \K. it then searches for the shortest string of characters (.*?) followed by Artist:.((?=Artist:); this is called a positive lookahead and matches the string you are looking for without having it count in the match, so it isn't printed by grep).

Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

You can do this using the id3v2 tool, which should be in the repositories for your operating system (note that this solution assumes GNU grep, the default if you are running Linux):

## Iterate over all file/dir names ending in mp3
for file in /path/to/dir/with/mp3/files/*mp3; do 
    ## read the title and save in the variable $title
    title=$(id3v2 -l "$file" | grep -oP '^Title\s*:\K(.*?)(?=Artist:)'); 
    ## check if this title matches ytversion
    if [[ "$title" =~ "ytversion" ]]; then 
        ## If it does, replace ytversion with mqversion and 
        ## save in the new variable $newTitle
        newTitle=$(sed 's/ytversion/mqversion/g' <<<"$title") 
        ## Set the tite tag for this file to the value in $newTitle
        id3v2 -t "$newTitle" "$file" 
    fi
done

This si slightly complicated because the id3v2 tool prints the title and artist on the same line:

$ id3v2 -l foo.mp3 
id3v1 tag info for foo.mp3:
Title  : : title with mqversion string   Artist:                               
Album  :                                 Year:     , Genre: Unknown (255)
Comment:                                 Track: 0
id3v2 tag info for foo.mp3:
TENC (Encoded by): iTunes v7.0
TIT2 (Title/songname/content description): : title with mqversion string     

The -o flag tells grep to only print the matching portion of a line, and the -P enables PCRE regular expressions. The regex is searching for a line starting with Title followed by 0 or more whitespace characters and then an : (^Title\s*:). Everything matched up to that point is then discarded by the \K. it then searches for the shortest string of characters (.*?) followed by Artist:.((?=Artist:); this is called a positive lookahead and matches the string you are looking for without having it count in the match, so it isn't printed by grep).