登录
首页 >  Golang >  Go问答

使用golang的id3-go库来提取和输出mp3文件的Popimeter数据

来源:stackoverflow

时间:2024-03-01 14:09:26 371浏览 收藏

今天golang学习网给大家带来了《使用golang的id3-go库来提取和输出mp3文件的Popimeter数据》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我想使用 id3-go 从文件中读取流行度计框架。

这是使用诱变检查打印时框架的样子:

$ mutagen-inspect samples/with_popm.mp3 | grep popm
[email protected]=0 255/255

我想从文件中读取值(255/255)。由于我找不到任何文档,我的天真的做法是:

popframe := mp3file.frame("popm")
log.println(popframe.string())

但是当我运行这个(在带有和不带有流行度标签的文件上)时,我遇到了分段错误:

$ ./id3-go-popm-example samples/with_popm.mp3 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x4ac302]

goroutine 1 [running]:
main.main()
    /home/ifischer/src/rivamp/rivamp-dist/id3-go-popm-example/main.go:21 +0xd2
$ ./id3-go-popm-example samples/without_popm.mp3 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x4ac302]

goroutine 1 [running]:
main.main()
    /home/ifischer/src/rivamp/rivamp-dist/id3-go-popm-example/main.go:21 +0xd2

我在这里设置了一个包含两个示例文件的示例存储库(一个带有流行度计框架,一个没有流行度计框架):https://github.com/ifischer/id3-go-popm-example

如何使用 id3-go 库获取人气计值 (255/255)?


解决方案


您遇到分段错误,因为 mp3file.frame("popm") 实际上返回 nil 值。我已将其范围缩小到 id3-go 中的 v23frametypemap ,它缺少 trdc 框架。我真的不熟悉 id3 的内部结构,但也许您需要检查 trdc 是否真的在 v2.3 中,如果是,请在 id3-go 中的 v23frametypemap 中添加缺少的 id。

由于学习 id3 太麻烦,所以我设法使用另一个库 https://github.com/dhowden/tag 获取 popm 标签

f, _ := os.open("with_popm.mp3")
m, _ := tag.readfrom(f)
fmt.printf("%s\n", m.raw()["popm"])

输出:

[email protected]

编辑:我把 tdrc 误认为是 trdc,我的错误。无论如何,您需要执行以下操作才能使用 id3-go 获取 popm 字段

github.com/mikkyang/id3-go/v2/idv23.go 中的 v23frametypemap 中添加以下内容

v23frametypemap = map[string]frametype{
    //...
    "tdrc": frametype{id: "tdrc", description: "recording date", constructor: parsetextframe},
}

然后您可以轻松获取 popm 字段,例如

package main

import (
    "fmt"
    id3 "github.com/mikkyang/id3-go"
)

func main() {

    f, _ := id3.open("with_popm.mp3")
    p := f.frame("popm")
    fmt.printf("%s\n", p.bytes())

}

编辑 2:我添加了一种方法,以便您可以解析 popm 字段。然后,您可以从 popularimeter.rating 访问评级 255/255:

import (
    "bytes"
    "encoding/binary"
    "errors"
    "fmt"

    id3 "github.com/mikkyang/id3-go"
)

func main() {

    f, _ := id3.Open("with_popm.mp3")
    popm := f.Frame("POPM")
    popularimeter, _ := FromData(popm.Bytes())
    fmt.Println(popularimeter)
}

type Rating uint8

func (r Rating) String() string {
    if r == 0 {
        return "unknown"
    }
    return fmt.Sprintf("%d/255", r)
}

type Popularimeter struct {
    Email   string
    Rating  Rating
    Counter uint32
}

func FromData(data []byte) (*Popularimeter, error) {

    tokens := bytes.SplitN(data, []byte{0x0}, 2)
    // Pupolarimeter: , null, 1byte rating, 4bytes counter
    if len(tokens) != 2 || len(tokens[1]) != 5 {
        return nil, errors.New("invalid Popularimeter")
    }

    return &Popularimeter{
        Email:   string(tokens[0]),
        Rating:  Rating(tokens[1][0]),
        Counter: binary.BigEndian.Uint32(tokens[1][1:]),
    }, nil
}

终于介绍完啦!小伙伴们,这篇关于《使用golang的id3-go库来提取和输出mp3文件的Popimeter数据》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>