Linux/Mac script for transcoding and adding to Plex

Agreed!

I have both installed on a multi-user system I manage.

Homebrew has a stronger community for sure. Homebrew does everything it can to act harmoniously with the software already present on Darwin. Iā€™ve also generally more familiar for those with Linux experience, and it certainly has more recent versions of software available. However, for the system I manage Iā€™ve found homebrew-installed software scattered all over, which is a mess. From an administratorā€™s perspective, I prefer something a little cleaner. This might not be an issue for single-user systems.

For those from a BSD background MacPorts will be more familiar. It does everything it can to ensure you are running a pure version of the ported software independent of what may be already installed by Apple (or anyone else), and by default sets everything up in /opt/local, which means itā€™s extremely easy to wipe it clean and start again. There are advantages and disadvantages to this approach. But for sure the availability of up-to-date packages is worse.

In both cases theyā€™ll get the job done, so generally itā€™s best to use whatever youā€™re comfortable with.

1 Like

ā€œthe priority has been to produce something that works and has all the functionality I want, rather than something very user-friendly.ā€

I completely understand.

Thanks so much for doing this! If you present a pocket down the road, Iā€™d love to present some coin.

Update posted, mostly internal changes and a couple of minor bug-fixes. The main changes:

  1. There is a new version of the prefs file, but the old one should keep working for you. The script will now, by default, wait until Channels is no longer recording or comskipping before it does the actual transcoding. This should prevent transcoding before comskip results are in, and times out after 4 hours.

  2. More variables are discovered via the Channels API, which means that for most users only Plex-specific options need to be set. In fact, if you run it locally then youā€™ll probably only need to set DEST_DIR in the prefs file.

1 Like

Major update posted.

Minor changes:

  1. The main script has been renamed to channels-transcoder.sh.
  2. Preferences folder, launch agent, etc. have been renamed to match.
  3. Shows can be explicitly selected on the command line by ID or (partial) name. No VAR=parameter format required.
  4. More robust error handling.
  5. Improved handling of GNU parallel execution.

Major change:

I have added an installation script, as requested. This should make life much simpler for new users and old users alike. Installation can be performed by running:

curl -O https://raw.githubusercontent.com/karllmitchell/Channels-DVR-to-Plex/master/install.sh
bash install.sh

But note that this is not suited to those that run as root; You should install as a regular user with superuser access only. If you have to run as root, Iā€™ll assume that you know what youā€™re doing, but in any case do not use the script. You may also need to move your old .transcode-plex folder, depending on where you set it up.

Importantly, some dependencies (curl, jq, and AtomicParsley) are not installed by the script, so install these using e.g. one of these:

sudo apt-get install curl jq ffmpeg AtomicParsley
sudo port install curl jq ffmpeg AtomicParsley
sudo brew install curl jq ffmpeg AtomicParsley

The install script will install the main script to /usr/local/bin, add a preferences folder to a default destination (~/.channels-transcoder/ or ~/Library/Application Support/channels-transcoder), and initialise the database. It will ask you for some questions if you have no existing prefs file, so make sure youā€™ve thought through: (i) where you want the destination directory to be (youā€™ll have to point Plex at it later), (ii) whether you want to transcode a backlog of shows (if so, how many days worth, and bear in mind that this may take a LONG time), and (iii) what the location of the Channels DVR interface is (usually http://localhost:8089). The latter two have okay defaults, but the first requires some consideration.

For those of you that have used previous (pre install script) versions, it SHOULD import your old preferences file, transcode database, etc., as long as they were in ~/.transcode-plex/ or ~/Library/Application Support/transcode-plex/. However, immediately after installation I recommend that you manually delete/check any old LaunchAgents or cronjobs you set up, as the script canā€™t handle switching these. Also, please double-check your preferences file if itā€™s fairly old.

Feel free to ping me with questions and bug reports if it doesnā€™t work for you.

Minor update of install.sh just uploaded. Improves handling of crontab for previous installs. It now imports a single previous entry, or reports if there are multiple entries and overwrites them.

@uspino: You requested continuous monitoring. Although not quite what you asked for, the script will now check to see if Channels DVR is busy before starting transcoding, waiting up to a user-defined amount of time before running (default = 4 hours). In theory the script plays nicely with additional instances, in so far as it will skip transcoding if either (a) the file already exists in Plex, or (b) an existing job on that ID is already running. In short, if you run it on a 4-hour-or-less cycle from a cronjob, it will almost do what you asked for.

What is does not yet do is immediately start transcoding as soon as individual instances of comskip are done. It waits for ALL Channels DVR activity to end. I personally this approach for lower powered systems, as those transcoding jobs can quickly overwhelm the system.

Iā€™ll give some thought to an better solution now that most of my higher priority features are implemented.

Here is a one-liner botch for Linux users who would prefer to run as soon as comskipping is complete:

inotifywait -e CLOSE -r -m /mnt/dvr/channels/Logs/comskip | awk -F/ ā€˜{system("/usr/local/bin/channels-transcoder.sh DAYS=0 "$(NF-1))}ā€™

Replace the file path with wherever your comskip logs are kept. Run this once at startup, rather than using the cron method (remove it from your crontab), and it should automatically transcode everything immediately. Just make sure you donā€™t accidentally run more than one instance of it!

I have paused all new feature development work on the main script for the time being, and am focusing only on bug fixes for a while. This is largely due to a reduction in my free time. That, and the script is running exactly as I want right now.

I am interested in hearing from anyone that has it working, and whether they are having any difficulties. Feel free to contact me in PM.

Working fine here. Is there a way to use Channels comskip info to cut out commercials or at least include chapter markers for the commercial breaks on the transcoded file?

Yes. COMTRIM=1 should work for cutting out the commercials, but bear in mind that the commercial marking isnā€™t foolproof, and so you could potentially lose some show this way. More commonly, however, it mostly mismarks commercials as show, not the other way around.

Note that CHAPTERS=1 should actually be turned on by default in the prefs file, marking the commercial start and finish as chapters. However, Plex at least has very limited support for this within their interfaces, so you might not be seeing those chapters. I also see that some of my recent conversions do not have any in, so you might have found a bug. Iā€™ll look into it over the next couple of days.

One more suggestion: can the script find out if the system is capable of hardware transcoding, and use software transcoding only as a fallback?

I discovered a problem with MP4Box no longer marking chapter files. Iā€™ve no idea why it stopped working, but one of the devs here pointed me in the right direction for doing it with ffmpeg, so now Iā€™ve dropped MP4Box completely. In brief, Iā€™ve uploaded a new version of the script that should hopefully work now (with CHAPTERS=1 or COMTRIM=1 enabled). Re-running the installation script, or simply replacing the main channels-transcoder.sh script, should fix it. Please report back if you have any problems.

Optional hardware transcoding is a long-term goal. Iā€™m somewhat reluctant in the short-run, because (a) I have never used it before and currently lack any systems with a decent hardware encoder, and (b) even the most recent Intel/NVidia systems produce worse quality results for the same file size, and so the gain over simply archiving the MPG file and doing live hardware transcoding is less. That being said, it is also a computational time/energy versus disk space trade, and I get that there is some use for it, so Iā€™ll eventually look into it. This might be something that is easier to do when I switch over to just using ffmpeg for transcoding, which is also a long term goal.

I should add that Plex support for chapters remains inconsistent and crude. It requires a down-swipe on the apple TV and then navigating with the remote. On the web interface, the keyboard shortcuts are the square brackets, [ & ].

I had a request for a web interface for the configuration file for my scripts. Itā€™s a good idea, but I donā€™t think I have the time for that, as Iā€™d have something of a learning curve. If anyone would like to try it out, please feel free. At the very least, I could give some ideas, and donā€™t mind assisting and tweaking how things work on the script/configuration end. The github repository is open to contributions, although I would prefer any solutions to support both MacOS and Linux.

Of course, thereā€™s always a chance that this will become redundant as features get added to Channels, but I suspect that, at the very least, the remote operation and parallelization aspects will remain somewhat unique, which should make the script useful for those of us that are running a very low powered server.

Running this on a Mac and thereā€™s no prompt for input, it just runs through everything and tells me thereā€™s a lot of errors:

The local destination directory is for producing Plex-like file structures. Enter the desired destination directory, followed by [ENTER]: sed: 1: "/DEST_DIR*/c\DEST_DIR=" ...": extra characters after \ at the end of c command If you're running this script on a machine other than the one running Channels DVR, you should specify the host here. Enter the hostname and port number (leave blank for default "localhost:8089"), followed by [ENTER]: sed: 1: "/HOST*/c\HOST="sudo mv ...": extra characters after \ at the end of c command Channels transcoder to be initiated. You may choose whether to transcode old recordings or ignore them. If you have a lot of backlog, this will run slowly in the background. Do not delete the Channels-DVR-to-Plex-master folder in the meantime. It will clean itself up. Enter the desired number of days of Channels DVR recordings backlog to transcode (default=0): bash: line 90: timeout: command not found Transcoding backlog is running in background. Assuming it completes, once done you should run the following command: sudo launchctl load "/Users/timstephens24/Library/LaunchAgents/com.getchannels.channels-transcoder.plist" You may follow progress by running: tail -f "/Users/timstephens24/.channels-transcoder/log" Done. [RiMac ~/.install] Cannot write to /Users/timstephens24/.channels-transcoder/transcode.db, using /Users/timstephens24/channels-transcoder/transcode.db instead /usr/local/bin/channels-transcoder.sh: line 219: /Users/timstephens24/channels-transcoder/transcode.db: No such file or directory Transcode database initialised at /Users/timstephens24/channels-transcoder/transcode.db Cannot write to /Users/timstephens24/channels-transcoder/transcode.db. I give up! Installation files removed

Sorry about that. Iā€™m on travels right now, but will look into it as soon as I get back.

It turned out to be an easy fix, caused by some stray text. Try again?

Still having the same issue.
The local destination directory is for producing Plex-like file structures. Enter the desired destination directory, followed by [ENTER]: sed: 1: "/DEST_DIR*/c\DEST_DIR=" ...": extra characters after \ at the end of c command If you're running this script on a machine other than the one running Channels DVR, you should specify the host here. Enter the hostname and port number (leave blank for default "localhost:8089"), followed by [ENTER]: sed: 1: "/HOST*/c\HOST="sudo mv ...": extra characters after \ at the end of c command Channels transcoder to be initiated. You may choose whether to transcode old recordings or ignore them. If you have a lot of backlog, this will run slowly in the background. Do not delete the Channels-DVR-to-Plex-master folder in the meantime. It will clean itself up. Enter the desired number of days of Channels DVR recordings backlog to transcode (default=0): bash: line 11: /usr/local/bin/channels-transcoder.sh: No such file or directory LaunchAgent has been installed. Depending on your backlog, this may take some time. You may follow progress by running: tail -f "/Users/timstephens24/Library/Application Support/channels-transcoder/log" You may close this window without harming any remaining processing. Done. [RiMac ~] If prompted, please enter your password now Installation files removed

I think I figured it out. One more try? Latest version uploaded.