Thursday, November 25, 2021

ffmpeg-convert.sh

i recently came across the need to quickly rip audio track from video files. my first thought was to use ffmpeg and wrap it in a script. sometimes i may have one or 20 MP4/MKV/webm files that need to be converted to an MP3. the files may be scattered across a few directories, but each serves its own purpose. thus, i wrote a script to automate a lot of repetitive work.

the script, called ffmpeg-convert.sh, makes a lot of assumptions and works fine as an underprivileged user. it expects the files to be actual video files and will fail gracefully if the file doesn't exist or ffmpeg unexpectedly exits. it will continue where it left off if you CTRL+C for some reason. it also has a neat spinner so that you don't have to wonder if it's working or not. some large files can take a while to process.

i endeavored to write as much of the functionality as possible in BASH. the sub-string search and replace is kind of disgusting, but it does a fairly decent job of sanitizing the filename.

the script works like this: when invoked, it scans the current working directory for files with "known" video file extensions. it prompts you with how many files were found and asks for permission to continue. it then processes each file via ffmpeg and bakes you a fresh MP3 file. it's pretty simple, and i might consider adding more functionality to the script. i've already received a couple of requests!

grab the script from here.