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


How to improve your Google PageSpeed score with Webp images

If you're still serving images in long-time standard formats such as JPEG and PNG then a Google PageSpeed Insights analysis of your site will identify an opportunity to improve your page loading times by serving images in "next-gen" formats. There are several so-called "next-gen" image formats which offer superior compression to JPEG and PNG but in this post we will… Continue reading »

A puzzle with one piece missing

What are WebSockets?

What do WebSockets do? Hypertext Transfer Protocol (HTTP) is unidrectional, meaning requests can only be made by the client. This is a limitation which means that a certain class of web application cannot be built using HTTP alone.¬†WebSockets are a separate communication protocol, fully compatible with HTTP, which allow for bidirectional communication between a client and a server. A protocol… Continue reading »