英文:
How to remove binary content from the strings?
问题
我正在尝试使用golang从mp3文件中读取ID3标签。我得到了结果。但是根据我的数据库查看器显示,它们还包含一些二进制内容。
有没有办法在golang中去除这些内容?
英文:
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.
Any way to remove that content in golang?
答案1
得分: 0
这是id3-go中的一个当前问题:PR 8修复了id3v1的问题。
但对于id3v2来说,这还不够,如此提交所示,它确实需要修剪这些空字符。
请看cutset := string(rune(0))
的用法,以及TrimRight(s string, cutset string)
的用法,例如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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论