Compressing audio with FFmpeg and PowerShell

← Back
A laptop and notepad

I recently had a folder full of audio files which I wanted to compress and I found that using FFmpeg and PowerShell offered a solution, so I thought I’d share what I learnt. FFmpeg is a command line based program for processing of video and audio files. You can download FFmpeg here and if, like I was in this case, you’re using Windows, you can simply extract the folder once it’s downloaded and add the path to your Environment Variables. It’s possible to use FFmpeg without this step but only if you’re in the directory which has ffmpeg.exe. Adding the path means you can access the command from any location and will make life easier. You can do so by searching “Environment variables”, selecting “Edit the system environment variables”, clicking “Environment Variables”, clicking “Path”, “Edit” and creating a new variable for the location of the folder. Once you’ve done so, open PowerShell (or close and re-open it if it was already open) and type “ffmpeg” and you should see information about the version of you’ve downloaded.

Now that FFmpeg is up and running you can compress an audio file with the following command:

 

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 output.mp3

 

You can choose the -qscale:a number based on your target bitrate. An overview table can be found here. Excellent. But what if we have a large folder full of multiple files which we want to compress? It wouldn’t be practical to do this manually, and as developers we can do better. Windows PowerShell is a more powerful alternative to the Command Prompt which allows you to create variables and loops. You can create a variable of all of the files in a directory like so:

 

$files = Get-ChildItem "<directory path>"

 

You can then loop through each of the files, executing your ffmpeg compression command on each like so:

 

foreach ($file in $files) {
  $newName = $file.BaseName + "_compressed.mp3"
  ffmpeg -i $file.FullName -codec:a libmp3lame -qscale:a 2 $newName
}

 

The FFmpeg command will then be executed on each file sequentially and leave you with the output files which have “compressed” appended to the filename. An excellent way to compress files efficiently via the command line.

Recent Blog Posts

A laptop with PHP code on the screen

A look at some of PHP 7.4’s new features

PHP 7.4 was released on 28 November 2019 and with it comes a lot of new features. In this post we'll examine a few of the more interesting ones along with examples of how they can be used. PHP has long been the punchline in low-effort memes, and though some of the disdain for the language is justified, more recent… Continue reading »

Some text from a computer terminal

Scheduling database backups with cron jobs

We're going to create a bash script which runs mysqldump (a MySQL backup program) to generate a database backup with a filename containing the date. We're then going to set up a cron jobĀ (a utility for running scheduled tasks) to run the script at regular intervals. We'll do all of this securely so that other users on the server can't… Continue reading »