Converting a FLAC collection to Opus

If you are looking to transcode an entire FLAC collection to Opus to save space on your mobile device, here are a few one-off scripts to get you started. It makes use of opusenc and parallel, so make sure you have both of those to start off.

makeopus.sh - Where the encoding actually happens.

#!/bin/bash
##Set our command prefix here, use this to modify bitrate, etc.
OPUSPREFIX="opusenc --bitrate 128"

##Cleanup original jobs list
find -type f -name "jobs.txt" -delete

find -type f -name "*.flac" -print0 | while read -d $'\0' a; do
    #$(printf '%s' "$(printf '%s' "$OPUSPREFIX") $(printf '%q' "$a") $(printf '%q' "${a[@]/%flac/opus}")") >> jobs.txt
    ##Set our escaped source flac file
    SOURCEFILE="$(printf '%q' "$a")"
    ##Set our destination opus file, replacing the file extension
    DESTFILE="$(printf '%q' "${a[@]/%flac/opus}")"
    ##Output to the jobs list that will feed parallel below
    printf '%s\n' "$OPUSPREFIX $SOURCEFILE $DESTFILE" >> jobs.txt
done

##Comment out the line below if you would like to review the commands
##that will be executed from jobs.txt below.
parallel --bar --eta :::: jobs.txt

syncopus.sh - Used to pull only desired files from the transcoding process.

#!/bin/sh
rsync -av --exclude='*.flac' --exclude='*.FLAC' --exclude='*.zip' --exclude='*.sh' /home/user/Music/ /home/user/Opus/

deleteopus.sh - Be careful with this, I use this for cleanup on the Music directory on my setup and for this reason use absolute paths.

#!/bin/bash
find /home/user/Music -name "*.opus" -delete

There are many different ways to handle the above, and I'm sure much cleaner solutions. Here is what you will get with the above:

  • Opus files that will follow the exact directory structure as your FLAC collection.
  • The rsync will include external cover art and other miscellaneous files, while opusenc will carry over embedded cover art for the FLAC files in your collection that have them.
  • Usage of all your cpu cores for the transcoding process thanks to parallel.
  • A nice little progress bar so you can keep an eye on just how much is left to complete.

Example console output from makeopus.sh, displaying the progress bar and percentage completed.

If you have a laptop with poor cooling, take advantage of cpupower. In my case running even a single thread for a large collection eventually forced my current laptop to shutdown. With parallel I was able to achive an overall comparable transcoding speed while using the lowest clock setting using the commands below.

root@toastylaptop ~# cpupower frequency-set -u 1.20GHz
Setting cpu: 0
Setting cpu: 1
Setting cpu: 2
Setting cpu: 3
root@toastylaptop ~# cpupower frequency-info
analyzing CPU 0:
  driver: acpi-cpufreq
  CPUs which run at the same hardware frequency: 0
  CPUs which need to have their frequency coordinated by software: 0
  maximum transition latency: 10.0 us.
  hardware limits: 1.20 GHz - 2.40 GHz
  available frequency steps: 2.40 GHz, 2.40 GHz, 2.27 GHz, 2.13 GHz, 2.00 GHz, 1.87 GHz, 1.73 GHz, 1.60 GHz, 1.47 GHz, 1.33 GHz, 1.20 GHz
  available cpufreq governors: ondemand, performance
  current policy: frequency should be within 1.20 GHz and 1.20 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.20 GHz (asserted by call to hardware).
  boost state support:
    Supported: yes
    Active: yes
    2000 MHz max turbo 2 active cores
    2200 MHz max turbo 1 active cores

Waldo

A *nix enthusiast and accidental programmer interested in sharing whatever tidbits I learn, more or less for my own reference.

The Evergreen State