golang实现MP3音频文件ID3标签解码与编码插件库id3v2的使用
Golang实现MP3音频文件ID3标签解码与编码插件库id3v2的使用
简介
id3v2是一个用纯Go实现的ID3 v2.3和v2.4标签处理库,可以用来读取和写入MP3文件的元数据。
安装
要安装id3v2库,运行以下命令:
go get -u github.com/bogem/id3v2/v2
使用示例
基本用法
package main
import (
"fmt"
"log"
"github.com/bogem/id3v2/v2"
)
func main() {
// 打开MP3文件并解析ID3标签
tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true})
if err != nil {
log.Fatal("打开MP3文件时出错: ", err)
}
defer tag.Close()
// 读取标签信息
fmt.Println(tag.Artist())
fmt.Println(tag.Title())
// 设置标签信息
tag.SetArtist("Aphex Twin")
tag.SetTitle("Xtal")
// 添加评论帧
comment := id3v2.CommentFrame{
Encoding: id3v2.EncodingUTF8, // 使用UTF-8编码
Language: "eng", // 语言代码
Description: "My opinion", // 描述
Text: "I like this song!", // 评论内容
}
tag.AddCommentFrame(comment)
// 将标签保存到文件
if err = tag.Save(); err != nil {
log.Fatal("保存标签时出错: ", err)
}
}
读取多个帧
// 获取所有图片帧
pictures := tag.GetFrames(tag.CommonID("Attached picture"))
for _, f := range pictures {
pic, ok := f.(id3v2.PictureFrame)
if !ok {
log.Fatal("无法断言为图片帧")
}
// 处理图片帧
fmt.Println(pic.Description)
}
编码设置
id3v2支持不同的文本编码方式:
// 使用UTF-16编码的评论帧示例
comment := id3v2.CommentFrame{
Encoding: id3v2.EncodingUTF16, // 使用UTF-16编码
Language: "ger", // 德语
Description: "Tier", // 描述
Text: "Der Löwe", // 文本内容
}
tag.AddCommentFrame(comment)
Text
字段将自动使用带BOM的UTF-16BE编码并写入文件。对于v2.4版本,默认使用UTF-8编码;对于v2.3版本,默认使用ISO-8859-1编码。
注意事项
- 使用完毕后记得调用
tag.Close()
关闭文件 - 修改标签后需要调用
tag.Save()
保存更改 - 对于不同的ID3版本,默认编码可能不同
更多关于golang实现MP3音频文件ID3标签解码与编码插件库id3v2的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang实现MP3音频文件ID3标签解码与编码插件库id3v2的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
使用id3v2库处理MP3音频文件的ID3标签
简介
id3v2是一个Go语言实现的ID3标签处理库,可以读取和写入MP3文件的ID3v2标签信息。ID3标签是MP3文件中存储元数据(如歌曲名、艺术家、专辑等)的标准格式。
安装
go get github.com/bogem/id3v2
基本使用
打开MP3文件并读取标签
package main
import (
"fmt"
"github.com/bogem/id3v2"
"log"
)
func main() {
// 打开MP3文件
tag, err := id3v2.Open("example.mp3", id3v2.Options{Parse: true})
if err != nil {
log.Fatal("打开文件错误:", err)
}
defer tag.Close()
// 读取基本标签信息
fmt.Println("标题:", tag.Title())
fmt.Println("艺术家:", tag.Artist())
fmt.Println("专辑:", tag.Album())
fmt.Println("年份:", tag.Year())
fmt.Println("流派:", tag.Genre())
fmt.Println("音轨号:", tag.GetTextFrame(tag.CommonID("Track number/Position in set")).Text)
// 读取自定义帧
if comment := tag.GetLastFrame("COMM").(id3v2.CommentFrame); comment != nil {
fmt.Printf("评论: %s (语言: %s, 描述: %s)\n",
comment.Text, comment.Language, comment.Description)
}
// 读取所有帧
for _, frame := range tag.AllFrames() {
fmt.Printf("帧ID: %s, 内容: %v\n", frame.Key(), frame.Value())
}
}
写入ID3标签
package main
import (
"github.com/bogem/id3v2"
"log"
)
func writeTags() {
// 打开文件准备写入
tag, err := id3v2.Open("output.mp3", id3v2.Options{Parse: true})
if err != nil {
log.Fatal("打开文件错误:", err)
}
defer tag.Close()
// 设置基本标签
tag.SetTitle("示例歌曲")
tag.SetArtist("示例艺术家")
tag.SetAlbum("示例专辑")
tag.SetYear("2023")
tag.SetGenre("示例流派")
// 添加自定义帧
tag.AddFrame(tag.CommonID("Track number/Position in set"),
id3v2.TextFrame{Text: "1/10"})
// 添加评论帧
comment := id3v2.CommentFrame{
Language: "eng",
Description: "我的评论",
Text: "这是一首很好的歌曲",
}
tag.AddFrame("COMM", comment)
// 保存更改
if err := tag.Save(); err != nil {
log.Fatal("保存错误:", err)
}
}
高级功能
处理图片帧
func handlePictures() {
tag, err := id3v2.Open("with_picture.mp3", id3v2.Options{Parse: true})
if err != nil {
log.Fatal(err)
}
defer tag.Close()
// 获取所有图片帧
pictures := tag.GetFrames(tag.CommonID("Attached picture"))
for _, f := range pictures {
pic, ok := f.(id3v2.PictureFrame)
if !ok {
continue
}
fmt.Printf("图片类型: %d, 描述: %s, MIME类型: %s, 大小: %d bytes\n",
pic.PictureType, pic.Description, pic.MimeType, len(pic.Picture))
// 可以将图片数据保存到文件
// ioutil.WriteFile("cover.jpg", pic.Picture, 0644)
}
// 添加新图片
newPic := id3v2.PictureFrame{
Encoding: id3v2.EncodingUTF8,
MimeType: "image/jpeg",
PictureType: id3v2.PTFrontCover,
Description: "封面",
Picture: readFile("cover.jpg"),
}
tag.AddFrame("APIC", newPic)
tag.Save()
}
处理不同编码
func handleEncodings() {
tag, err := id3v2.Open("encoded.mp3", id3v2.Options{Parse: true})
if err != nil {
log.Fatal(err)
}
defer tag.Close()
// 默认情况下库会自动处理编码,但也可以手动指定
frame := tag.GetTextFrame("TIT2")
if frame != nil {
fmt.Println("标题(原始编码):", frame.Text)
fmt.Println("标题(UTF-8):", frame.Unicode())
}
// 以指定编码写入文本
customFrame := id3v2.TextFrame{
Encoding: id3v2.EncodingISO, // 使用ISO-8859-1编码
Text: "ISO编码文本",
}
tag.AddFrame("TXXX", customFrame)
tag.Save()
}
注意事项
- 修改文件后必须调用
Save()
方法才能保存更改 - 对于大文件,打开时可以使用
id3v2.Options{Parse: false}
延迟解析 - ID3v2有多个版本(2.2, 2.3, 2.4),库会自动处理兼容性问题
- 写入时默认使用ID3v2.4版本,可以通过
tag.SetVersion()
更改
错误处理
tag, err := id3v2.Open("nonexistent.mp3", id3v2.Options{Parse: true})
if err != nil {
if err == id3v2.ErrNoTag {
fmt.Println("文件没有ID3标签")
} else {
log.Fatal(err)
}
}
id3v2库提供了全面的ID3标签处理功能,适合需要读取或编辑MP3元数据的Go应用程序。