是否可以使用 R 读取音乐文件元数据?

发布于 2024-09-12 04:03:32 字数 184 浏览 5 评论 0原文

我有一堆带有元数据的音频文件(比如 ogg 或 mp3)。

我希望将它们的元数据读入 R 中,以便创建一个 data.frame,其中包含:

  • 文件名
  • 文件位置
  • 文件艺术家
  • 文件专辑

你知道这样做的方法吗?

I've got a bunch of audio files (let's say ogg or mp3), with metadata.

I wish to read their metadata into R so to create a data.frame with:

  • file name
  • file location
  • file artist
  • file album
  • etc

Any way you know of for doing that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

∞琼窗梦回ˉ 2024-09-19 04:03:32

您采用现有的 mp3 或 ogg 客户端,查看它使用的库,然后为所述库编写到 R 的绑定,使用现有客户端作为该侧的指南 - 以及类似 Rcpp 的东西作为另一侧的指南来显示如何将 C/C++ 库连接到 R。

没有灵丹妙药。

一种更便宜且不太可靠的方法是使用 cmdline 工具来执行您想要的操作,并编写一些小帮助函数,使用 system() 在文件上运行该工具,重新读取 R 中的输出。不漂亮,不可靠,但可能不那么具有挑战性。

You take an existing mp3 or ogg client, look at what library it uses and then write a binding for said library to R, using the existing client as guide for that side -- and something like Rcpp as a guide on the other side to show you how to connect C/C++ libraries to R.

No magic bullet.

A cheaper and less reliable way is to use a cmdline tool that does what you want and write little helper functions that use system() to run that tool over the file, re-reading the output in R. Not pretty, not reliable, but possibly less challenging.

似最初 2024-09-19 04:03:32

正如在此答案中所建议的,您可以使用 exiftool。

要在 R 中使用它,您可以使用 exifrexiftoolr 也不错):

# download a public domain mp3 file from The Internet Archive
download.file("https://archive.org/download/Jazz_Sampler-9619/Kevin_MacLeod_-_AcidJazz.mp3", "jazz.mp3", mode = "wb")

install.packages("exifr") # if necessary

exifr::read_exif("jazz.mp3") |> 
  mutate(location = ls()) |> # this is assuming that the file is in the working directory. If not, then you want to replace ls() with "Directory"
  select(name = SourceFile,
         location,
         artist = Artist,
         album = Album)
# Output:
# A tibble: 1 × 4
  name     location artist        album       
  <chr>    <chr>    <chr>         <chr>       
1 jazz.mp3 ~        Kevin MacLeod Jazz Sampler

As suggested in this answer, you can use exiftool.

To use it within R, you can use exifr (exiftoolr is also good):

# download a public domain mp3 file from The Internet Archive
download.file("https://archive.org/download/Jazz_Sampler-9619/Kevin_MacLeod_-_AcidJazz.mp3", "jazz.mp3", mode = "wb")

install.packages("exifr") # if necessary

exifr::read_exif("jazz.mp3") |> 
  mutate(location = ls()) |> # this is assuming that the file is in the working directory. If not, then you want to replace ls() with "Directory"
  select(name = SourceFile,
         location,
         artist = Artist,
         album = Album)
# Output:
# A tibble: 1 × 4
  name     location artist        album       
  <chr>    <chr>    <chr>         <chr>       
1 jazz.mp3 ~        Kevin MacLeod Jazz Sampler
流心雨 2024-09-19 04:03:32

可能,是,容易,否。

您“可以”在文件上使用 readChar 和/或 readBin 的组合并解析出内容。不过,这将高度依赖于从原始字节解析 帧标签ID3v2 标签(请注意,如果它是 版本 1 标签< /a>)。实施直接的 R 解决方案肯定需要大量工作。以这个 Python 代码 为例,它是非常干净的直接 python 代码,但有很多分支和解析。

Possible, yes, easy, no.

You "could" use a combination of readChar and/or readBin on the file and parse out the contents. This would be highly dependent, though, on parsing the frame tags from the raw bytes of the ID3v2 tag (and mind you it would change if it was a version 1 tag). If would certainly be a lot of work to implement a straight R solution. Take this Python code for example, it's very clean straight python code but a lot of branching and parsing.

汐鸠 2024-09-19 04:03:32

您可以将 exiftool 与 R 中可用的 system 命令结合使用。或者,您可以创建正则表达式来处理您需要的字段...如果我是您,我会坚持使用德克的建议(像往常一样)=)!

You can use exiftool with system command available in R. Optionally, you can create regexp to handle the fields you need... If I were you, I'd stick with Dirk's advice (as usual) =)!

昔日梦未散 2024-09-19 04:03:32

在 2021 年,我想做到这一点,所以我做了以下操作...

  1. 在“歌曲”视图中创建一个新的播放列表。
  2. 选择所有歌曲并拖动到新的播放列表。突出显示该播放列表
  3. 文件>库>导出播放列表。我的默认文件是另存为.txt,如果没有,请指定。
  4. 打开Excel保存为csv或read.delim()在r中作为txt文件以制表符分隔
  5. 导入到R

Out here in 2021, I wanted to do this so I did the following...

  1. Create a new playlist while in 'songs' view.
  2. Select all songs and drag to the new playlist. Highlight that playlist
  3. File> Library>Export Playlist. My default file was to save as .txt, if not, designate.
  4. Open Excel to save as csv or read.delim() in r as the txt file is tab-separated
  5. import to R
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文