Automating Video to mp3 Conversion

Computing: «I love computers because they automate the tedious stuff and give us more time for important things like market manipulation.»
Hutama, We are All Destined to Prosper in «Civilization: Beyond Earth»

Preferring mp3s over newer formats, I used to use an audio-converter app with batch processing. Unfortunately, it no longer runs on modern Macs. I tried other ones and I would have even paid for one, if they did not use subscription models. Why in the world would anyone rent software?

Luckily, ffmpeg can convert lots of media formats and stackoverflow had the necessary sample code ( https://stackoverflow.com/questions/3255674/convert-audio-files-to-mp3-using-ffmpeg ).

Using R (I am just familiar with it) it is possible to automate the conversion. Note that there is no warranty, and that Mac Terminal (which the system command accesses) cannot handle some characters. You have to mask blank spaces with \, (that’s what the str_replace_all(inputFiles[[i]], ” “, “\\\\ “) command is for). Also brackets can pose problems. Take care that the path to the folder with the input and output files is similarly sane.

makemp3 <- function() {
    library("beepr")
    inputFiles <- list.files("PATH/makemp3input")
    pathName <- "PATHMAKED"
    if(length(inputFiles) > 0) {
        for(i in 1:length(inputFiles)) {
            convertFile <- str_replace_all(inputFiles[[i]], " ", "\\\\ ")
            target <- str_replace(convertFile, file_ext(convertFile), "mp3")
            targetFileCheck <- paste0("PATH/makemp3output/", str_replace(inputFiles[[i]], file_ext(inputFiles[[i]]), "mp3"))
            if(!file.exists(targetFileCheck)) {
                commandText <- paste0("ffmpeg -i ", pathName, "makemp3input/", convertFile, " -vn -q:a 2 ", pathName, "makemp3output/", target)
                system(commandText)
            } else { warning(paste0("File already exists: ", targetFileCheck)) }
        }
        
        # commandText <- paste0("ffmpeg -i ", pathName, "makemp3input/", convertFile, " -vn -ar 44100 -ac 2 -b:a 192k ", pathName, "makemp3output/", target)
        
        beep(4)
        print("Work Complete.")
    } else { warning("No files found.") }

}

The result is a script that quickly converts the files in the input directory to mp3 (I think).