is reading .wma sound files in R possible?

178 Views Asked by At

Is the there a way to read .WMA sound files in R or the copyright restrictions do not allow this?

The final aim is to convert it to another format (MP3/WAV)

1

There are 1 best solutions below

0
Artem On

In one or another way R audio packages use ffmpeg converter.

Please see the following options:

  • After its download you can use R to convert WMA to MP3 file format directly by system function call;
  • or you can use ffmpeg wrapper package for simple audio conversions. However it is Linux-oriented it could be easily transformed into Windows compatible one.

Please see the code below for Option 2:

# install.packages("devtools")
# library(devtools)
# install_github("pmur002/ffmpeg")

library(ffmpeg)

# set path to your ffmpeg.exe file
ffmpeg_path <- "C:\\<Path to ffmpeg>\\ffmpeg-20180906-70a7087-win64-static\\bin\\ffmpeg.exe"

ffmpeg_win <- function (inputs, outputs, filters = NULL, overwrite = FALSE, 
                        wait = TRUE, echo = FALSE) {
  if (!is.null(filters)) {
    stop("Filters are currently unsupported")
  }
  if (inherits(inputs, "FFmpeg_input")) {
    inputs <- list(inputs)
  }
  if (inherits(outputs, "FFmpeg_output")) {
    outputs <- list(outputs)
  }
  options <- ""
  if (overwrite) {
    options <- paste0(options, "-y ")
  }
  cmd <- paste(ffmpeg_path, options, do.call(paste, inputs), do.call(paste, 
                                                                     outputs))
  system(cmd, wait = wait)
  if (echo) {
    cat(cmd, "\n")
  }
}

# just copy to your working directory required file, here is for example "mellow.wma"
ffmpeg_win(fileInput("mellow.wma"), fileOutput("mellow.mp3"), echo = TRUE)