2

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 ?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Tal Galili
  • 24,605
  • 44
  • 129
  • 187

5 Answers5

3

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
1

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.

Mark
  • 106,305
  • 20
  • 172
  • 230
1

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) =)!

aL3xa
  • 35,415
  • 18
  • 79
  • 112
1

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
Mark
  • 7,785
  • 2
  • 14
  • 34
0

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