Transcoding FLAC to Opus in bulk

Opus is a neat lossy audio codec that is superior to MP3 in every single way. If you tend to explore music of various genres a lot, legally borrowing artists' discographies in lossless format that come to grow in size, you will inevitably run out of free space at some point. If you also happen to be a poorcuck who's unable to afford a hard disk drive of any decent capacity — the issue has become especially tangible with the advent of a disastrous Chia cryptocurrency, — you may come to realisation that converting all of your lossless files to lossy could be a good idea.

First of all, let's assume that you have a directory structure that looks like this:

music/
├── electronic/
│   └── electro-industrial/
│       └── Gridlock/
│           └── albums/
│               └── 2001 - Trace [vinyl]/
│                   ├── 01 - Bu'yaam.flac
│                   ├── 02 - Voiceless.flac
│                   ├── 03 - F².flac
│                   ├── 04 - Estrella.flac
│                   ├── 05 - F.flac
│                   ├── 06 - UH.56.flac
│                   ├── 07 - UH4.17 (rebashed by Somatic Responses).flac
│                   └── 08 - Kcab.flac
└── experimental/
    └── power electronics/
        ├── Ramleh/
        │   └── albums/
        │       └── 1987 - Hole In The Heart/
        │           ├── 01 - Spear Flowers.flac
        │           ├── 02 - Hole In The Heart.flac
        │           ├── 03 - Product Of Fear.flac
        │           └── 04 - Grazing On Fear2.flac
        └── Sutcliffe Jügend/
            └── albums/
                └── 1998 - When Pornography Is No Longer Enough/
                    └── < ... >

As shown in the code block above, music contains multiple directories and subdirectories within itself, so in order to transcode all of your lossless files to opus while retaining directory structure, you may cd into your top-level directory — in my case it's music — and simply run the following command:

flac2opus

The output will be located inside a directory named opus.

the code

The flac2opus utility is a simple shell script written in POSIX sh, meaning that it's going to run on any operating system, be it GNU/Linux, *BSD or MacOS, which is technically BSD under the hood. The only dependency you'll need is the opus encoding utility, opusenc, which is available from the opus-tools package.

Firstly, download the file. If you're on *BSD (including MacOS):

$ ftp -Vo flac2opus 'https://notabug.org/kv/dotfiles/raw/master/bin/flac2opus'

If you're on GNU/Linux:

$ curl -sO 'https://notabug.org/kv/dotfiles/raw/master/bin/flac2opus'

Make the file executable:

$ chmod 744 flac2opus

As always, move the script into your $PATH if you want to invoke it from anywhere without providing full or relative path to the file.

You may edit the program and change the value of the bit variable to whatever you want it to be; the official Xiph.Org wiki states that Opus at 128 Kb/s is transparent, however, the gist of lossy coding formats is to achieve the strongest compression without drastic deformation regarding the audio quality, which essentially means that you're encouraged to continuously lower the bit rate of files and keep checking whether the output sounds like utter garbage or has negligible, minor artefacts.