Linux/Mac script for transcoding and adding to Plex

You removed the function at the beginning so it gets to line 34 and exits.

Also looking at the code, if youā€™re going to run something from ~/Library/LaunchAgents/.plist, it should be run as the user, not root. If you want it to run as root (which it seems like you want this run as), itā€™s better to place it in /Library/LaunchAgents.

And thanks for helping getting this setup properly!

Some more bug corrections in. You were right about running the Launch Agent as user; I had it as root previously, but there was no reason for that, and I forgot to update it.
-Karl

Looks like line 38 has a comment before the comment should be there? Thereā€™s no end quote, but it looks like the comment got placed in the wrong spot to me. I removed the ā€˜# Default=ā€œlocalhost:8089ā€. For running script remotely.ā€™ and tried running but still another errorā€¦

sed: 1: "/DEST_DIR*/c\DEST_DIR=" ...": extra characters after \ at the end of c command

So I added a line to just move the prefs file:
mv channels-transcoder.prefs "${prefsdir}/prefs"
Then changed the sed lines to inline:
sed -i ".bak" "s,DEST_DIR=\"/mnt/network/Plex\",DEST_DIR=\"${destination}\"," "${prefsdir}/prefs" sed -i ".bak" "s,HOST=\"\",HOST=\"${host_name}\"," "${prefsdir}/prefs"
From there I ran it and that whole section finally worked, so now I had ~/Library/Application Support/channels-transcoder with a correct prefs fileā€¦

Then it jumped down and moved channels-transcoder.sh to /usr/local/bin

And then it just failed:
./install.sh: line 101: syntax error: unexpected end of file

So I read farther and noticed there was no ;; in the ā€˜*)ā€™ caseā€¦ But I still canā€™t get it to work, so somethingā€™s wrong in the: ā€˜if [ ! -f ā€œ${prefsdir}/transcode.dbā€ ] ; thenā€™ section but I canā€™t see itā€¦

Found itā€¦ missing a ā€˜fiā€™!

But now more errors:

Please wait for the database to be initiated and backlog to be transcoded... Working directory: /tmp/.PQyjviI0 Channels DVR API Interface Found Cannot write to /Users/timstephens24/Library/Application Support/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! Installing automation script... If prompted, please enter your password now... /Users/timstephens24/Library/LaunchAgents/com.getchannels.channels-transcoder.plist: service already loaded Launch Agent installed Installation files removed Done.

Okay, back home now, so I can pay this some more attention. It might take me a while, but I intend to test it more thoroughly before the next update of the install script.

The error here:

Cannot write to /Users/timstephens24/Library/Application Support/channels-transcoder/transcode.db, using /Users/timstephens24/channels-transcoder/transcode.db instead

suggests that the permissions on transcode.db are wrong. Please check that and run:

sudo chown timstephens24 "/Users/timstephens24/Library/Application Support/channels-transcoder/transcode.db"

if they are.

If all else fails, then the main things are to get the main script to /usr/local/bin, the prefs file (just called prefs) into that directory above, and the LaunchAgent installed, before running it. Use the following (only for a Mac), making sure to use your preferred editor on line 6 to set DEST_DIR in the prefs file, and change the number of days backlog on line 8 to whatever you want:

wget https://github.com/karllmitchell/Channels-DVR-to-Plex/archive/master.zip
unzip master.zip
cd Channels-DVR-to-Plex-master
sudo cp channels-transcoder.sh /usr/local/bin/
mkdir -p "~/Library/Application Support/channels-transcoder/"
nedit channels-transcode.prefs #Use whichever editor to point DEST_DIR
mv channels-transcode.prefs "~/Library/Application Support/channels-transcoder/prefs"
/usr/local/bin/channels-transcoder.sh DAYS=0. # Set to whatever you prefer
cp com.getchannels.channels-transcoder.plist "~/Library/LaunchAgents/"
launchctl load "~/Library/LaunchAgents/com.getchannels.channels-transcoder.plist"

That should install everything you need.

I have uploaded an almost completely new installation script, as well as some minor corrections to the README file and main script. This has been tested on both a Mac and an ARM64 Ubuntu Linux system, but not a wide range of circumstances, so please test. It should be more reliable than the previous script.

New installation command is:

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

If you are running GNU parallel and only have the relevant code installed on your remote system(s), then please run:

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

Everything can now work in user space, although by default if you have sudo access it will install the main script in /usr/local/bin.

Thanks,

-Karl

Hi there.
Iā€™ve tried several times to install with the curl string you provided on my Mac mini running 10.11.4 and have run into errors about prerequisites. Iā€™ve checked using ā€˜whichā€™ and jq, ffmpeg, curl, and realpath are all on my system. Below is the line I copy/pasted into the term window with error message.

$ curl https://raw.githubusercontent.com/karllmitchell/Channels-DVR-to-Plex/master/install.sh > install.sh && bash install.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7153  100  7153    0     0  10722      0 --:--:-- --:--:-- --:--:-- 10724
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]: 
Checking for critical pre-requisite programs:
 jq curl ffmpeg realpath
Optional packages include:
 AtomicParsley >= 0.9.6 and parallel >= 20161222
If they do not exist on your system, please use whichever package manager
 works for your system to install them
On Ubuntu/Debian Linux:
 sudo apt-get install jq curl HandBrakeCLI ffmpeg realpath
On Mac, use homebrew, macports or fink similarly.
Some pre-requisites not installed.
Please try again or use "bash install.sh force" then edit your prefs file manually

I get a little further when using the ā€˜forceā€™ flag, but run into an error on line 124.

Installing automation script...
install.sh: line 124: /log: Permission denied

Any advice or pointers would be greatly appreciated.
-Andrew

@karlmitchell You might be interested in http://www.shellcheck.net

Personally I always write my shell scripts with ā€œset -eā€

@tmm1 I use shellcheck often. Itā€™s a great resource.
@amann180 Sorry about that. I added some corrections for Linux installations a while back, think I got them working, but perhaps broke something for the Mac. Iā€™ll try to fix it again, but in the meantime there are only three files you really need to set up, and itā€™s easy enough to do manually from the terminal, so try running the following, making sure to pause at and read the comments I inserted.

ā€“

wget https://github.com/karllmitchell/Channels-DVR-to-Plex/archive/master.zip
unzip master.zip
cd Channels-DVR-to-Plex-master/
mkdir ~/.channels-transcoder
cp channels-transcoder.prefs "${HOME}/.channels-transcoder/prefs"
sudo cp channels-transcoder.sh /usr/local/bin   # Enter your password here
cp com.getchannels.channels-transcoder.plist "${HOME}/Library/LaunchAgents/"

At this stage you could double-check that ~/.channels-transcoder/prefs looks okay to you. The first line, DEST_DIR, is the critical one. This should be where you want your Plex recordings to go. You will need to point Plex at the subfolders ā€œTV Showsā€ and ā€œMoviesā€ afterwards. Other than that, mostly it will operate fine with default settings, but there might be things you want to tweak, and familiarizing yourself with the options does no harm.

Once youā€™ve done that, the next command both tests that channels-transcoder.sh is working for you, and initiates the database. If you would like to transcode some past shows, I recommend changing DAYS to be a bigger number that encompasses the time range of shows you wish to transcode, e.g. DAYS=7 will transcode the last weekā€™s recordings for you. The subsequent command initializes nightly transcoding.

channels-transcoder.sh DAYS=0
launchctl load "${HOME}/Library/LaunchAgents/com.getchannels.channels-transcoder.plist

ā€“

And thatā€™s it. You should be done!

-Karl

I should add that I just re-ran the script on a Mac (current version of the OS) and had no issues. Very strange.

From the error, it looks like itā€™s trying to write to /log when it should be writing to ~/Library/Application\ Support/channels-transcoder/log. If you could check whether ~/Library/Application\ Support/channels-transcoder was created for you and whether you can write to it (cd ~/Library/Application\ Support/channels-transcoder; touch test), that might help me diagnose what is going wrong. Alternative, for some reason ${prefsdir} wasnā€™t set correctly by the script, but I canā€™t see why.

I did update the script a little, as per my subsequent post.

Updates:

HandBrakeCLI dependency removed in favor of ffmpeg.
ffmpeg version installed by channels-dvr can be used instead of installing a separate one.
All transcoding is now handled via ffmpeg
Installation script updated to improve Mac install and correct some bugs

  • ā€œset -eā€ added
  • realpath replacement bash alias now installed in ~/.profile if binary not available.
  • script requirements updated to reflect changes to HandBrakeCLI and ffmpeg requirements

If channels-transcoder is already working for you, there is no need to upgrade. It shouldnā€™t break your system if you do, though.

I moved the files into place manually, however when running ā€˜channels-transcoder.shā€™ it now complains that it canā€™t find the db and canā€™t write to any of the locations. Iā€™ve chowned the ~/Library/Application Support/channels-transcoder/ and can write to the location. I even ran chmod 777 just to make sure it could be written to.

$ channels-transcoder.sh DAYS=0
Working directory: /tmp/transcode.BtmS8v16
Channels DVR API Interface Found
Cannot write to /Users/amannone/Library/Application Support/channels-transcoder/transcode.db, using /Users/amannone/.channels-transcoder/transcode.db instead
/usr/local/bin/channels-transcoder.sh: line 205: /Users/amannone/.channels-transcoder/transcode.db: No such file or directory
Transcode database initialized at /Users/amannone/.channels-transcoder/transcode.db
Cannot write to /Users/amannone/.channels-transcoder/transcode.db.  I give up!

However, if I do ā€˜touch transcode.dbā€™ in the location, I get the following when running ā€˜channels-transcoder.shā€™:

$ channels-transcoder.sh DAYS=7
Working directory: /tmp/transcode.AtsnJKX6
Channels DVR API Interface Found
No new shows to transcode

No matter what value I put for Days, it always says ā€œNo new shows to transcodeā€.

In Prefs, for the DEST_DIR key, do I need to escape any spaces in the path? For example DEST_DIR="/Users/amannone/My\ Video\ Folder"? or is this fine DEST_DIR="/Users/amannone/My Video Folder"

Okay, something went wrong in your initial install, but it can all be fixed. Please simply delete ~/.channels-transcoder, e.g.

rm -rf ~/.channels-transcoder

Basically, if that folder still exists it will stop looking for the one under Application Support, and at some point it probably populated your transcode.db with all past episodes by running DAYS=0. The transcode.db is simply a list of recording numbers that have previously been transcoded, and that by default it ignores for future transcoding unless you override this behavior. To reset and repopulate your transcode.db, after deleting the .channels-transcoder directory, run e.g.:

channels-transcoder.sh DAYS=7 CLEAR_DB=1

No escape characters should be used in the prefs file, as long as you put them in quotes. If you do not include quotes, youā€™ll need them.

Iā€™m afraid Iā€™m still having issues.
I ran ā€œrm -rf ~/.channels-transcoderā€ as requested (even tho I didnā€™t see that folder in my user directory using ls -a). I also removed ā€œ~/Library/Application Support/channels-transcoderā€ and the channels-transcoder.sh from /usr/local/bin/
I recreated ~/Library/Application Support/channels-transcoder and copied a new prefs and log file there, put a fresh copy of channels-transcoder.sh in /usr/local/bin.

Running channels-transcoder.sh DAYS=7 CLEAR_DB=1, I get that ā€œCannot write to /Users/amannone/Library/Application Support/channels-transcoder/transcode.db,ā€ etc. error. It seems the script dosenā€™t like it if a transcode.db file is not in that folder. In each time Iā€™ve gone through the installation Iā€™ve had to create a transcode.db file using touch.

With the transcode.db in place, I get this:

$ channels-transcoder.sh DAYS=7 CLEAR_DB=1
Working directory: /tmp/transcode.WjnDSekZ
Channels DVR API Interface Found
Transcode database initialised at /Users/amannone/Library/Application Support/channels-transcoder/transcode.db
No new shows to transcode

Iā€™m starting to think thereā€™s a gremlin in my mac!!

That is very strange. It sounds like the script doesnā€™t have write permissions to ā€œ/Users/amannone/Library/Application Support/transcode.dbā€ but you do, which shouldnā€™t happen. Youā€™re definitely installing this all as user? The only time you should use ā€œsudoā€ is for moving the main script to /usr/local/bin/channels-transcoder.sh.

Iā€™ve tried duplicating everything you describe, and it works fine for me, so Iā€™m a little lost.

Could you run the following little diagnostic and report back on any output?

mkdir -p ~/Library/Application\ Support/channels-transcoder/test
touch ~/Library/Application\ Support/channels-transcoder/ test.db
ls -l ~/Library/Application\ Support/channels-transcoder/
ls -l ~/Library/Application\ Support/ | grep channels-transcoder
ls -l ~/Library/ | grep Application\ Support
ls -l /usr/local/bin/channels-transcoder.sh
rm -rf ~/Library/Application\ Support/channels-transcoder/test*

Bear in mind that you can also simply generate/edit your own transcode.db file if you like. It is one recording per line, and if you donā€™t know what show these recordings corresponds to, try changing to the DVR directory (from the Settings page of your Channels DVR web interface) and running this:

for i in ./Logs/comskip/* ; do printf "$(echo $i | rev | cut -d/ -f1 | rev): " ; readlink $i/video.mpg | rev | cut -d/ -f1 | rev; done

If you delete episodes manually, and then run channels-transcoder.sh with a large DAYS= value, and without setting CLEAR_DB=1, it should transcode them. Alternatively, simply run:

echo 0 >> ~/Library/Application\ Support/channels-transcoder/transcode.db

and it will initiate without blocking any episodes from being transcoded.

Thanks,

-Karl

Hi Karl,

Hereā€™s the output of the terminal commands you asked for. I donā€™t see anything that jumps out at me. Also, yes, I did everything as my user ā€” no sudo was used.

amannone@mann1mini:~$ mkdir -p ~/Library/Application\ Support/channels-    transcoder/test
amannone@mann1mini:~$ touch ~/Library/Application\ Support/channels-transcoder/test.db
amannone@mann1mini:~$ ls -l ~/Library/Application\ Support/channels-transcoder/
total 32
-rw-r--r--  1 amannone  staff     0B May  9 18:55 log
-rw-r--r--  1 amannone  staff   8.1K May  9 19:45 prefs
drwxr-xr-x  2 amannone  staff    68B May 10 01:41 test/
-rw-r--r--  1 amannone  staff     0B May 10 01:41 test.db
-rw-r--r--  1 amannone  wheel   105B May  9 22:27 transcode.db
amannone@mann1mini:~$ ls -l ~/Library/Application\ Support/ | grep channels-transcoder
drwxr-xr-x    7 amannone  staff   238B May 10 01:41 channels-transcoder/
amannone@mann1mini:~$ ls -l ~/Library/ | grep Application\ Support
drwx------+  132 amannone  staff   4.4K May  9 17:59 Application Support/
amannone@mann1mini:~$ ls -l /usr/local/bin/channels-transcoder.sh
-rwxr-xr-x  1 amannone  admin    29K May  9 18:09 /usr/local/bin/channels-transcoder.sh*
amannone@mann1mini:~$ rm -rf ~/Library/Application\ Support/channels-transcoder/test*

Appreciate the assistance!

I have started to use PLEX a lot more now. I have considered moving my Channels DVR Recordings there but I really like the double click to skip ā€œmarkedā€ commercials. I understand you can just have the commercials removed but I have seen too many times where the marked commercials are not perfect. Is there a way with the PLEX version to have ā€œmarkedā€ commercials like with Channels DVR / Apple TV play back? If so, might be nice if Channels DVR had a PLEX Option to support it natively.

Also, I use WatchAid as a Universal Watch List for Hulu, CBS, HBO, Showtime and many more and they are soon to add PLEX for those with TV Shows. So, this might be a good option.

@d21mike: Commercial boundaries are marked as Chapters by default using my scripts. On the web version of the client, you can go forward and back by using squade brackets: [ and ]. On the Apple TVā€™s Plex, you can downswipe on the Siri Remote and see these chapter markings, but unfortunately this is not as easy as just pressing a button to skip to the next chapter, and I tend to not use this interface as a result. The precise implementation of chapter change by client is out of my hands.