Skip to content

Instantly share code, notes, and snippets.

@manniru
Forked from christopherlovell/m3u-2-JSON.R
Created August 7, 2024 23:34
Show Gist options
  • Save manniru/a7e0444756a57bf7b409211cbde2875f to your computer and use it in GitHub Desktop.
Save manniru/a7e0444756a57bf7b409211cbde2875f to your computer and use it in GitHub Desktop.
m3u to JSON converter
library(rjson)
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
data_dir = 'Documents/playlists/'
play_list = list()
for(i in list.files(data_dir,"^.*\\.(m3u)$")){
year = substr(i,1,4) # record file specific information, in this case year from playlist group
d = data.frame()
for(j in readLines(paste0(data_dir,i),encoding = 'latin1')){
if(substr(j,1,8) == "#EXTINF:"){ # track line
extract = trim(strsplit(x = substr(j,9,nchar(j)), split = c(" - |,"))[[1]][-1])
d <- rbind(d,data.frame(extract[1],extract[2]))
}
}
colnames(d) <- c("artist", "song")
play_list[[i]] = list(name=i,year=year,tracks=d)
}
write(toJSON(play_list),paste0(data_dir,"playlists.json"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment