Page 1 of 1

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!

Sonarr - Script for...
Clear all

[Solved] Sonarr - Script for transcoding after download if new episode(s) (Linux/macOS)

1 Posts
1 Users
0 Reactions
Famed Member Admin
Joined: 11 years ago
Posts: 2717
Topic starter  

I had a user ask me to help him write a script which would make symbolic links to newly downloaded files.
These symbolic links would then be used by Handbrake on his Apple Silicon Mac (wicked fast in transcoding!), to transcode these new downloads.

Additionally the original (downloaded) filename should stay the same so importing a transcode file would be easy to manually import (maintaining quality, release group, etc).

Initially this seemed a challenge until I found out that Sonarr has the option to run a custom scrip after importing new episodes.

In Sonarr, go to "Settings" -> "Connect" -> "+" -> "Custom Script"

Enter a "Name" so you know what it's for. 

Select on what events your custom scripts needs to run. 
In our example: on Import (downloaded or manually imported) and on Upgrade.

In the "Path" box enter the location and filename of the script you'd like to run.
Note that the script needs to run on the same machine (in this case: the NAS) where Sonarr is running.

You can make this file just and empty file initially, and enter the file path and click "Test" to make sure the script can be found etc.

For example:

/share/Downloads/Sonarr Scripts/

The cool part is that Sonarr passes on a few variables that we need, like filename etc.
Two things we need to keep in mind:

- I wasn't able to find the original filename with proper file extension

So I had to reconstruct the original filename like so:


Where ${sonarr_episodefile_scenename} is the original filename, but unfortunately without file extension.
Next, ${sonarr_episodefile_sourcepath} holds the new filename with extension.
To grab only the extension we use ${sonarr_episodefile_sourcepath##*.}.


- We only want downloads (new and upgrades). Manual imports should be skipped!

This means that the source path should match the downloads folder or part of that name, so in our example the name should include "nzbget" in the original file path. If is doesn't include "nzbget" then we can skip it as it's most likely a manual import.


All combined; this is what I came up with (suitable for Linux or macOS environment - may work different under Windows):


# Skip all files that do NOT come from NZBGet
if ! [[ "${sonarr_episodefile_sourcepath}" =~ ${nzbget_path_portion} ]]; then

#original downloaded name
ln -s "${sonarr_episodefile_path}" "/share/Downloads/Media Files/Sonarr - New arrivals/${sonarr_episodefile_scenename}.${sonarr_episodefile_sourcepath##*.}"


All files in  "/share/Downloads/Media Files/Sonarr - New arrivals/" will now be symbolic links to newly downloaded or upgraded episodes (skipping manual imports), which can be transcoded with Handbrake or other tools.
The symbolic links can be deleted or moved without affecting the original files.
Of course opening one of these, editing, say in a video editor, and saving it again WILL change the original files.


More info can be found at the Wiki Servarr documentation.

If you'd like to make a dump of all known variables provided by Sonarr (and the Linux environment), add something like this to the beginning of your script (before the "exit")

# All environment vars:
env > "/share/Downloads/Media Files/envdump.txt"


Or if you want to test/add a single variable, for example:

# Single environment var:
echo "$sonarr_episodefile_scenename" >> "/share/Downloads/Media Files/envdump.txt"


Hope this is useful for anyone.