Search code examples
stringgoid3

How to remove binary content from the strings?


I was trying to read ID3 tags from mp3 files using golang.
I get the results. But they also contain some binary content as my database viewer suggests.

picture

Any way to remove that content in golang?


Solution

  • This is a current issue in id3-go: PR 8 fixes it for id3v1

    But that isn't enough for id3v2, as show in this commit, which does have to trim those null characters. See the use of cutset := string(rune(0)), and of TrimRight(s string, cutset string), as in for instance strings.TrimRight(fd.Title(), cutset):

    fd, err := id3.Open(path)
    if err != nil {
        item.Title = f.Name()
    } else {
        defer fd.Close()
        cutset := string(rune(0))
        title := strings.TrimRight(fd.Title(), cutset)
        author := strings.TrimRight(fd.Artist(), cutset)
        if len(title) > 0 {
            item.Title = title
        } else {
            item.Title = author
            if len(author) > 0 {
                item.Title += " - "
            }
            item.Title += strings.TrimRight(f.Name(), cutset)
        }
        item.Subtitle = author
        tcon := fd.Frame("TCON")
        if tcon != nil {
            item.Categories = append(item.Categories, Text{Value: strings.TrimRight(tcon.String(), cutset)})
        }
        item.PubDate = strings.TrimRight(formatYear(fd.Year()), cutset)