Linux script to transfer (ID3) tags from FLAC to MP3

5.5k views Asked by At

For my media server, I am looking for ways to transfer tags from my FLAC files to MP3.

In a bash script, I can extract tags using metaflac to local vars, but when tagging mp3 with id3v2, I seem to lose national characters (guess it must be unicode?)

Also I need to be able to set replay gain tags, and album art (all present in the FLAC's).

I am looking for a scripted solution to run unattended.

5

There are 5 answers

3
Neox On BEST ANSWER

Try this tool eyed3. It supports album art embedding, text encoding in latin1, utf8, utf16-BE and utf16-LE. However the replay gain is not supported. As far as I understand it is not widely supported.

1
Victor Roetman On

If you are interested in a Python solution, the mutagen library looks really good.

It could be as easy as:

from mutagen.flac import FLAC
from mutagen.easyid3 import EasyID3

flacfile = FLAC("flacfile.flac")
mp3file = EasyID3("mp3file.mp3")

for tag in flacfile:
    if tag in EasyID3.valid_keys.keys():
        mp3file[tag] = flacfile[tag]

mp3file.save()

I found this solution for copying mp3 id3 tags into FLAC files.

0
smammy On

Quod Libet comes with a command line tool called operon that does this and more:

operon copy song.flac song.mp3

Since Quod Libet is built on Mutagen, it knows about a bunch of obscure tags and how to translate them among the various tagging formats, which is important for some workflows. The only quirk I noticed is that it doesn't copy tags with empty values, but that doesn't bother me.

0
user1904085 On

Here is another solution using ffmpeg. Eg. just define a bash function in $HOME/.bashrc:

flac2mp3() 
{ 
  ffmpeg -i "$1" -ab 320k -map_metadata 0 -id3v2_version 3 "$(basename "$1" flac)mp3"
}
0
markling On

Victor's solution showed me the way. It may fail, however, if copying tags to a file you've just converted, for example, from flac to mp3. That is, it will fail if the file you are copying tags to doesn't already have any tags.

So you may need to prime the destination file first, giving it the means to have tags.

from mutagen import File
from mutagen.flac import FLAC
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, ID3NoHeaderError

def convert_tags(f1,f2):
    # f1: full path to file copying tags from
    # f2: full path to file copying tags to
    # http://stackoverflow.com/questions/8873364/linux-script-to-transfer-id3-tags-from-flac-to-mp3
    # http://stackoverflow.com/a/18369606/2455413
    try:
        meta = EasyID3(f2)
    except ID3NoHeaderError:
        meta = File(f2, easy=True)
        meta.add_tags()
        meta.save()
    from_f = FLAC(f1)
    to_f = EasyID3(f2)
    for tag in from_f:
        if tag in EasyID3.valid_keys.keys(): to_f[tag] = from_f[tag]
    to_f.save()
    return