<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Sonarr - Script for transcoding after download if new episode(s) (Linux/macOS) - Home Theatre				            </title>
            <link>https://www.tweaking4all.com/forum/home-theatre/sonarr-script-for-transcoding-after-download-if-new-episodes-linux-macos/</link>
            <description>Tweaking4All.com Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Mon, 20 Apr 2026 12:55:01 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Sonarr - Script for transcoding after download if new episode(s) (Linux/macOS)</title>
                        <link>https://www.tweaking4all.com/forum/home-theatre/sonarr-script-for-transcoding-after-download-if-new-episodes-linux-macos/#post-4863</link>
                        <pubDate>Sun, 23 Apr 2023 13:58:07 +0000</pubDate>
                        <description><![CDATA[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...]]></description>
                        <content:encoded><![CDATA[<p>I had a user ask me to help him write a script which would make symbolic links to newly downloaded files.<br />These symbolic links would then be used by Handbrake on his Apple Silicon Mac (wicked fast in transcoding!), to transcode these new downloads.</p>
<p>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).</p>
<p>Initially this seemed a challenge until I found out that Sonarr has the option to run a custom scrip after importing new episodes.</p>
<p>In Sonarr, go to "<strong>Settings</strong>" -&gt; "<strong>Connect</strong>" -&gt; "<strong>+</strong>" -&gt; "<strong>Custom Script</strong>"</p>
<p>Enter a "<strong>Name</strong>" so you know what it's for. </p>
<p>Select on what events your custom scripts needs to run. <br />In our example: <strong>on Import</strong> (downloaded or manually imported) and <strong>on Upgrade</strong>.</p>
<p>In the "<strong>Path</strong>" box enter the location and filename of the script you'd like to run.<br />Note that the script needs to run on the same machine (in this case: the NAS) where Sonarr is running.</p>
<p>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.</p>
<p>For example:</p>
<pre contenteditable="false">/share/Downloads/Sonarr Scripts/make_link.sh</pre>
421
<p>The cool part is that Sonarr passes on a few variables that we need, like filename etc.<br />Two things we need to keep in mind:</p>
<p><em>- I wasn't able to find the original filename with proper file extension</em></p>
<p>So I had to reconstruct the original filename like so:</p>
<pre contenteditable="false">${sonarr_episodefile_scenename}.${sonarr_episodefile_sourcepath##*.}</pre>
<p>Where <strong>${sonarr_episodefile_scenename}</strong> is the original filename, but unfortunately without file extension.<br />Next, <strong>${sonarr_episodefile_sourcepath}</strong> holds the new filename with extension. <br />To grab only the extension we use <strong>${sonarr_episodefile_sourcepath<span style="color: #0000ff">##*.</span>}</strong>.</p>
<p> </p>
<p><em>- We only want downloads (new and upgrades). Manual imports should be skipped!</em></p>
<p>This means that the source path should match the downloads folder or part of that name, so in our example the name should include "<strong>nzbget</strong>" in the original file path. If is doesn't include "<strong>nzbget</strong>" then we can skip it as it's most likely a manual import.</p>
<p> </p>
<p>All combined; this is what I came up with (suitable for Linux or macOS environment - may work different under Windows):</p>
<pre contenteditable="false">#!/bin/bash
nzbget_path_portion="nzbget"

# Skip all files that do NOT come from NZBGet
if ! []; then
  exit
fi

#original downloaded name
ln -s "${sonarr_episodefile_path}" "/share/Downloads/Media Files/Sonarr - New arrivals/${sonarr_episodefile_scenename}.${sonarr_episodefile_sourcepath##*.}"</pre>
<p> </p>
<p>All files in  "<strong>/share/Downloads/Media Files/Sonarr - New arrivals/</strong>" will now be symbolic links to newly downloaded or upgraded episodes (skipping manual imports), which can be transcoded with Handbrake or other tools. <br />The symbolic links can be deleted or moved without affecting the original files. <br />Of course opening one of these, editing, say in a video editor, and saving it again WILL change the original files.</p>
<p> </p>
<p>More info can be found at the <a href="https://wiki.servarr.com/en/sonarr/custom-scripts" target="_blank" rel="noopener">Wiki Servarr documentation</a>.</p>
<p>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")</p>
<pre contenteditable="false"># All environment vars:
env &gt; "/share/Downloads/Media Files/envdump.txt"</pre>
<p> </p>
<p>Or if you want to test/add a single variable, for example:</p>
<pre contenteditable="false"># Single environment var:
echo "$sonarr_episodefile_scenename" &gt;&gt; "/share/Downloads/Media Files/envdump.txt"
</pre>
<p> </p>
<p>Hope this is useful for anyone.</p>]]></content:encoded>
						                            <category domain="https://www.tweaking4all.com/forum/home-theatre/">Home Theatre</category>                        <dc:creator>Hans</dc:creator>
                        <guid isPermaLink="true">https://www.tweaking4all.com/forum/home-theatre/sonarr-script-for-transcoding-after-download-if-new-episodes-linux-macos/#post-4863</guid>
                    </item>
							        </channel>
        </rss>
		