Raspberry Pi NAS and FFmpeg

I use a Pi4 4 GB model as my NAS. With it’s Gigabit LAN and USB 3.0 for my old portable HDDs, it makes a good installation for a casual NAS.

I noticed somedays ago that running PiHole and Samba wasn’t pushing Pi hard enough, so I could run some stuff as an offload on it. And then stumbled on this.

Remember all those old AVI files? Annoying to play on many default video players, hard to cast sometimes? Or sometimes an annoying audio codec in mkv (like DTS) which is unsupported and hence you get no audio? What if we schedule these encoding jobs on the Pi?

To my surprise, it works quite reliably and surprisingly well. First install ffmpeg on your Pi:

sudo apt update
sudo apt-get install -y ffmpeg

After that, we need a wrapper over ffmpeg to show us a nice progress bar. I chose ffmpeg-bar. To install that, first we need NodeJS and then install the npm package. I have Ubuntu on my Pi, and hence following is the correct code. For other OS images, you can get it from here.

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt-get install -y nodejs

Now let’s add the following to our aliases. I choose ~/.profile.

alias batch-mp4-convert-dir-avi='for i in *.avi; do ffmpeg-bar -i "$i" "${i%.*}.mp4"; done'

alias batch-mp4-convert-dir-mkv='for i in *.mkv; do ffmpeg-bar -i "$i" -c:v copy -c:a aac "${i%.*}.mp4"; done'source ~/.profile

The above aliases are:

  1. batch-mp4-convert-dir-avi: Allows us to convert the AVI to MP4 using the FFmpeg bar, doing the full re-encode with the optimal parameters ffmpeg guesses.
  2. batch-mp4-convert-dir-mkv: Allows us to convert the MKV to MP4, but reusing the video bitstream as is (notice -c:v copy) but converts audio to aac. There is a more optimal version to handle the no audio stream case here, but I personally want any error to be thrown back to me.

Our final output, looks something like this:

Now I can use Picture in Picture on my iPad flawlessly.