Due to rising costs for powerful servers that can serve a decent streaming service, I’m now planning to run my Jellyfin server (an open-source Plex alternative) on my Raspberry Pi 4B homelab server.
A Raspberry Pi board is a low-power board and can’t actually do transcoding, so we have to rely on direct play. A direct play stream is technically just a player streaming a file over a web server with no transcoding needed. Normally, this is to the MP4 format.
Sonarr and transcoding
Sonarr is an automated media manager. Basically, it keeps track of the shows and anime you watch using TheTVDB’s APIs to tag episode titles, seasons and summaries among others.
Once it detects a new episode, it scours the sources (in my case, it’s torrents – yeah, sue me) and downloads it, then automatically renames it so that it can be read by Plex or Jellyfin.
For instance, it turns
Family Guy/Season 19/S19E01 - Stewie's First Word.mp4 after your torrent manager downloads it.
The problem is that most releases are Matroska-based (MKV), and can’t be directly played by most players, so a stream transcode is necessary.
As you would expect, servers that can transcode can get expensive, and I used to run an Intel Xeon E3-1275v5 for it and while it works great, it’s expensive.
The solution? Transcode it before saving so no one has to transcode it again.
There’s the MP4 Automator tool on GitHub but it’s got some problems for my setup:
- My seedbox (a shared server that’s built for downloading torrents) has a problem running it due to Python on the server throwing up for some reason
- I can’t debug it because it’s in Python
- Doesn’t work well on
…so I decided to write my own.
I wrote the script in my favorite language, and I included it in my scripts repository.
I’ll probably make a dedicated repository for it sometime soon, I just needed something quick and dirty to get things started. It’s written in the dirtiest language known to mankind: PHP.
Because I’m just comfortable with PHP. Deal with it.
What does it do?
It’s supposed to be a Sonarr post-processing script. Basically, whenever Sonarr processes a file (during the import process), a notification is sent to the script and is captured for processing.
Ensure that PHP, FFMPEG and FFPROBE are running on the server, then set it up on Sonarr:
Path setting to wherever the PHP script is saved.
Needless to say, edit the file to specify your file locations and the like. It’s commented out on the first portion of the code. A config file would probably come soon when I get the time to clean things up.
Now, when Sonarr performs an import, it captures the operation and does the following logic:
- It checks the show type. If it’s a normal TV show, proceed to Step 3.
- If it’s an Anime, it goes through
ffprobeto determine the
Japaneseaudio and the
Englishsubtitles. If you want English dubs, change it yourself. Japanese audio + English subs are superior, but you’re free to be wrong.
- Once the
ffmpegoptions are determined, it proceeds with the transcoding process. An output file is created in the working directory ending with
rcloneis called to move the transcoded file to the remote.
This way works for remote storages, but has a few issues:
- Sonarr must treat deleted files as unmonitored, because the script will clean things up and since you’re not using the original file it’ll be deleted. And no, your torrents are untouched.
- If you don’t need to
rclonethe output file, you have to modify the script yourself. When I get things cleaned up I’ll make this configurable when I get the time.
Other than that, I don’t want to overengineer something that I need for my personal use. If you have a specific use-case, feel free to fork the code and make your own customizations.
The code is licensed under the MIT Open Source License.
Use this at your own risk, this script has been made to suit my personal setup.