Jump to content
Squid

Additional Scripts For User.Scripts Plugin

323 posts in this topic Last Reply

Recommended Posts

@IamSpartacus You can't really tell from outside the docker if an encoding process is currently running if the script kicks in. This above lines will shutdown the docker, no matter what it's doin inside. Maybe there is an schedule you can setup in the handbrake settings itself to prevent shuting down during a encoding process. Not sure. 

Share this post


Link to post

I would do a docker pause and a docker unpause instead in this circumstance

Share this post


Link to post

Hi, i updated my user script a little now with also moving my target files in a proper season 00 folder (2 digit instead 1 digit, default tvheadend behaviour)

 

may someone could take a look if i may overlooked something, so far its looking good and working here ...

 

what does it do, after i edited my .ts files and remuxed them with avisynth

 

check if the .mkv is there, run, in case the season is 1 digit (season 1 instead season 01) it creates the proper season dir, mv the .mkv to the proper season dir, update tvh recording path, remove the files and (if season dir was changed cause 2 digit) old dir.

 

if i have some errors or there are some things to make more simple, thanks ahead ;)

 

#!/bin/bash

startpath="/mnt/cache/Media/"    ### main place where to look for
searchpattern="*.ts"            ### what type of files to look for
matchpattern=".mkv"                ### compare if exist - work done ?
extradelpattern=".idx2"            ### extra delete type - sample, avidemux file
seasonnaming="Season "            ### Naming to check for season folders
mediafolder="/Media"            ### add to path beginning for TVHeadend update
waittime=30                        ### time to wait in case script runs while processing file

tvhuser="user"
tvhpass="pass"
tvhip="192.168.1.2"

cd $startpath

find . -mindepth 2 -iname $searchpattern|sed "s|^\./||"|while read fname; do
    echo "$fname"
    sourcefile=$(basename "$fname")
    filenameonly="${sourcefile%.*}"
    endfile="$filenameonly$matchpattern"
    extrafile="$sourcefile$extradelpattern"
    filepath=$(dirname "$fname")
    seasonpath=$(basename "$filepath")

    if [[ $seasonpath == "$seasonnaming"* ]]; then
        seasonpathnew=$(echo "$seasonpath" | sed -E 's/\<[0-9]\>/0&/')
        seriespath=$(dirname "$filepath")
        tvhfinalpath="$mediafolder/$seriespath/$seasonpathnew/$endfile"
    else
        tvhfinalpath="$mediafolder/$filepath/$endfile"
    fi

    echo "output path is: $tvhfinalpath"

    cd "$startpath/$filepath"

    if [ -f "$endfile" ]; then
        echo "$endfile found, processing ..."
        sleep $waittime
        if [[ $seasonpath == "$seasonnaming"* ]]; then
            cd ..
            mkdir "$seasonpathnew"
            mv "$seasonpath/$endfile" "$seasonpathnew/"
        fi

        curl -s "http://"$tvhuser":"$tvhpass"@"$tvhip":9981/api/dvr/entry/filemoved" --data-urlencode "src=$mediafolder/$filepath/$sourcefile" --data-urlencode "dst=$tvhfinalpath"
###        curl -s "http://"$tvhuser":"$tvhpass"@"$tvhip":9981/api/dvr/entry/filemoved" --data-urlencode "src=tvhfinalpath" --data-urlencode "dst=$mediafolder/$filepath/$sourcefile" ### uncomment to reverse

        cd "$startpath/$filepath"
        rm "$sourcefile"
        rm "$extrafile"
        if [[ ! $seasonpath == "$seasonpathnew" ]]; then
            cd ..
            rmdir "$seasonpath"
        fi
    else
        echo "$endfile not found."
    fi
done
 

Share this post


Link to post
Posted (edited)
11 hours ago, Squid said:

I would do a docker pause and a docker unpause instead in this circumstance

And i would just use -c=512

 

With -c= you can specify how much CPU power a docker uses at max, when the power is needed somewhere else. So you could set handbrake to -c=128 and it can run all day, if your server has extra power avaible, it uses it, if not, then its very slow. No need to start or stop, it just plain works.

 

100% is 1024 (which is standard btw) 50% would be 512... you get the idea.

 

I had same problem and that was the easiest and cleanest solution.

Edited by nuhll

Share this post


Link to post
14 hours ago, Squid said:

I would do a docker pause and a docker unpause instead in this circumstance

 

Why is that?

Share this post


Link to post
2 minutes ago, IamSpartacus said:

 

Why is that?

So if Handbrake is in the middle of doing something at the end of the session, it will resume where it left off when starting back up again rather than being blitzed (I assume)

Share this post


Link to post
3 minutes ago, Cessquill said:

So if Handbrake is in the middle of doing something at the end of the session, it will resume where it left off when starting back up again rather than being blitzed (I assume)

 

But would I want a file that was paused during the encode and then resumed?  That wouldn't cause issues with the file?

Share this post


Link to post
3 minutes ago, IamSpartacus said:

 

But would I want a file that was paused during the encode and then resumed?  That wouldn't cause issues with the file?

I'm way out of my depth, but I'd guess that Handbrake would be blissfully unaware that it had stopped and restarted and the output would be fine.  Just like pausing the copy/move of a file/folder in windows and resuming.  Hopefully one of the tech lifeguards will chime in and throw me a life ring. :)

Share this post


Link to post
3 hours ago, Cessquill said:

I'm way out of my depth, but I'd guess that Handbrake would be blissfully unaware that it had stopped and restarted and the output would be fine.  Just like pausing the copy/move of a file/folder in windows and resuming.  Hopefully one of the tech lifeguards will chime in and throw me a life ring. :)

 

I guess that make sense.  I can test that.

 

For now though, my roadblock is the handbrake /watch and /output folder being the same.  They can't be the same.  So I need a PostProcessing directory and a script that will watch that post processing directory and when it a sees a file in there, copy it back to the original destination under /mnt/cache/Media/.  Not sure how to accomplish this.

Share this post


Link to post
Posted (edited)
2 hours ago, IamSpartacus said:

 

I guess that make sense.  I can test that.

 

For now though, my roadblock is the handbrake /watch and /output folder being the same.  They can't be the same.  So I need a PostProcessing directory and a script that will watch that post processing directory and when it a sees a file in there, copy it back to the original destination under /mnt/cache/Media/.  Not sure how to accomplish this.

How is your media stored in /mnt/cache/Media/ ? is it all just thrown in the folder because if so, that will be an easy task to accomplish. All you'd need to do is change the line that says "output=/mnt/cache/output/" to wherever Handbrake outputs it's files to. This will then move any files from that directory to your media directory.

#!/bin/bash

#Set working directories
output=/mnt/cache/output/
media=/mnt/cache/Media/
cd $output

#Search the output directory for new media
for file in $output*
do
#Move any new files from the output directory back to the storage directory
	mv $file $media
done

EDIT: I just thought of another, albeit slight issue. If Handbrake puts the file there while it's still in progress, it'll try and move incomplete files; so to get around this, I'd suggest setting this script to run on either a custom cron job or daily. This would mean a smaller chance of catching half-complete files. 

 

Edited by zacharyd3
Added more info

Share this post


Link to post
7 minutes ago, zacharyd3 said:

How is your media stored in /mnt/cache/Media/ ? is it all just thrown in the folder because if so, that will be an easy task to accomplish. All you'd need to do is change the line that says "output=/mnt/cache/output/" to wherever Handbrake outputs it's files to. This will then move any files from that directory to your media directory.


#!/bin/bash

#Set working directories
output=/mnt/cache/output/
media=/mnt/cache/Media/
cd $output

#Search the output directory for new media
for file in $output*
do
#Move any new files from the output directory back to the storage directory
	mv $file $media
done

 

 

 

For the purposes of this process I have a TV and Movie directory under media.  So new media shows up in /mnt/cache/Media/TV and /mnt/cache/Media/Movies.  So i need the converted files to go back into those spots for them to be correct for Plex.

Share this post


Link to post
9 minutes ago, IamSpartacus said:

 

For the purposes of this process I have a TV and Movie directory under media.  So new media shows up in /mnt/cache/Media/TV and /mnt/cache/Media/Movies.  So i need the converted files to go back into those spots for them to be correct for Plex.

Ok, so I'm assuming your directories are set up something like 
/mnt/cache/Media/TV/Shameless/Season 1/episode goes here.mkv

 

You would then need to parse the filename so try and figure out where it should be stored, otherwise, if you had radarr and sonarr setup then you can always just copy the files back to the download directory of either of those programs and let it import them again. If you could post your naming scheme I can see if there is any way I can think of to try to parse the file and move it to the proper folder automatically. 

 

I'm also not familiar with how Handbrake outputs the file, does it output it back into a folder like it was originally (ie. /mnt/cache/HandbrakeOutput/Shameless/Season 1/episode.mkv), or does it just spit out the file (ie. /mnt/cache/HanebrakeOutput/episode.mkv)? Obviously enough, if it spits out the whole folder it would make things exponentially easier, but if not, I'm sure we can find a way to get it done.

 

Also, just know I'm no expert, I'm all self taught when it comes to bash and scripting but I can usually make stuff work. That being said, I am also at work so I may not reply asap but I'd be willing to help where I can 😄
 

Share this post


Link to post
5 hours ago, zacharyd3 said:

Ok, so I'm assuming your directories are set up something like 
/mnt/cache/Media/TV/Shameless/Season 1/episode goes here.mkv

 

You would then need to parse the filename so try and figure out where it should be stored, otherwise, if you had radarr and sonarr setup then you can always just copy the files back to the download directory of either of those programs and let it import them again. If you could post your naming scheme I can see if there is any way I can think of to try to parse the file and move it to the proper folder automatically. 

 

I'm also not familiar with how Handbrake outputs the file, does it output it back into a folder like it was originally (ie. /mnt/cache/HandbrakeOutput/Shameless/Season 1/episode.mkv), or does it just spit out the file (ie. /mnt/cache/HanebrakeOutput/episode.mkv)? Obviously enough, if it spits out the whole folder it would make things exponentially easier, but if not, I'm sure we can find a way to get it done.

 

Also, just know I'm no expert, I'm all self taught when it comes to bash and scripting but I can usually make stuff work. That being said, I am also at work so I may not reply asap but I'd be willing to help where I can 😄
 

 

First off I appreciate your assistance with working this out with me, so thank you 👍.

 

Yes my tv directory is setup like you have it and for movies it's /mnt/cache/Media/Movies/The Dark Knight/The Dark Knight.mkv.  I can output the files back to the Sonarr and Radarr directories but how would I signal to either of them to import the files since they've already done so?

Share this post


Link to post
10 hours ago, IamSpartacus said:

 

First off I appreciate your assistance with working this out with me, so thank you 👍.

 

Yes my tv directory is setup like you have it and for movies it's /mnt/cache/Media/Movies/The Dark Knight/The Dark Knight.mkv.  I can output the files back to the Sonarr and Radarr directories but how would I signal to either of them to import the files since they've already done so?

See, for movies it would be easy because you can just parse the filename and move it to the matching folder, but TV makes it a bit more difficult because the episodes more than likely wont have the show name in the title. 

 

As for how to get Radarr or Sonarr to search for them, I'd look at the api and see if there is a way to make it work. I can write something up for movies to get that moved to the proper folder, but TV might take a sec. Granted, there might be someone else on here with more knowledge than I that would be able to get it done easily, haha.

Share this post


Link to post
11 hours ago, IamSpartacus said:

 

First off I appreciate your assistance with working this out with me, so thank you 👍.

 

Yes my tv directory is setup like you have it and for movies it's /mnt/cache/Media/Movies/The Dark Knight/The Dark Knight.mkv.  I can output the files back to the Sonarr and Radarr directories but how would I signal to either of them to import the files since they've already done so?

Ok, so this should work specifically for movies, TV will still need some work. The only issue I see coming up is if there is a TV show episode that has a title that matches a movie name, however if your shows are stored like "/Shameless/Season 1/S01E01 - show title.mkv" then you shouldn't have to worry because the season and episode in the title will never match a movie.

 

You'll also want to go through the code below and edit some values to match your system. I left notes on the right side as to what to change, but basically, make sure the variables match your system and then run the script and it should spit out matching files. As long as it matches and everything looks right, then remove the # on the 4th last line and it will actually move stuff.

#!/bin/bash

#Start with a clear screen
clear

#Set working directories
output=/mnt/cache/Media/Output/										#Change this to your handbrake output directory
media=/mnt/cache/Media/			 									#This should be your media directory
movies=/mnt/cache/Media/Movies/										#This should be your movies directory

#Search the output directory for new media
for file in $output*
do
	#Parse the movie name from the filename
	parsedName0=$(echo "$file" | cut -d'.' -f1)
	parsedName1=$(echo "$parsedName0" | cut -d'/' -f7) 				#Lower or raise the number in f7 until "File parsed to:" shows just the movie name.
	echo "File parsed to: "$parsedName1

	#Check if the file is a movie
	movieFolder=$movies$parsedName1
	if [ -d "${movieFolder}" ]
	then
		echo "Movie Folder: "$movieFolder
		#Move any found files
		#mv "${file}" "${movieFolder}"; echo "Files Moved"			#Once all the outputs look correct, remove the "#"
	fi
	echo ""
done

 

Share this post


Link to post

may just a sidenote, ever thought about filebot ?

 

drop it there, it ll rename and move to the proper folder ...

Share this post


Link to post
7 minutes ago, zacharyd3 said:

Ok, so this should work specifically for movies, TV will still need some work. The only issue I see coming up is if there is a TV show episode that has a title that matches a movie name, however if your shows are stored like "/Shameless/Season 1/S01E01 - show title.mkv" then you shouldn't have to worry because the season and episode in the title will never match a movie.

 

You'll also want to go through the code below and edit some values to match your system. I left notes on the right side as to what to change, but basically, make sure the variables match your system and then run the script and it should spit out matching files. As long as it matches and everything looks right, then remove the # on the 4th last line and it will actually move stuff.


#!/bin/bash

#Start with a clear screen
clear

#Set working directories
output=/mnt/cache/Media/Output/										#Change this to your handbrake output directory
media=/mnt/cache/Media/			 									#This should be your media directory
movies=/mnt/cache/Media/Movies/										#This should be your movies directory

#Search the output directory for new media
for file in $output*
do
	#Parse the movie name from the filename
	parsedName0=$(echo "$file" | cut -d'.' -f1)
	parsedName1=$(echo "$parsedName0" | cut -d'/' -f7) 				#Lower or raise the number in f7 until "File parsed to:" shows just the movie name.
	echo "File parsed to: "$parsedName1

	#Check if the file is a movie
	movieFolder=$movies$parsedName1
	if [ -d "${movieFolder}" ]
	then
		echo "Movie Folder: "$movieFolder
		#Move any found files
		#mv "${file}" "${movieFolder}"; echo "Files Moved"			#Once all the outputs look correct, remove the "#"
	fi
	echo ""
done

 

 

 

Wow thanks so much for this @zacharyd3.  Really appreciate the work.

 

5 minutes ago, alturismo said:

may just a sidenote, ever thought about filebot ?

 

drop it there, it ll rename and move to the proper folder ...

 

Is there a container for this?  I don't actually need the naming piece as that's already done by Sonarr/Radarr.  I just need to be able to place both TV shows and Movies into a folder and then have that folder automatically move the my Media share.  Handbrake can output to the same folder the input was from.  My handbrake watch folder has a series folder and a movie folder.  Once I set the output folder and use the SAME_AS_SRC option, it will put the encoded file in the output folder under the same directory.

 

So if the watch folder is /downloads (which has series and movies under it) and the output folder is /downloadsPP, handbrake will output a file found in /downloads/series into /downloadsPP/series and a file found in /downlods/movies to /downloadspp/movies.

 

Maybe this would solve the issue @zacharyd3 with regard to parsing movies and tv shows?

Share this post


Link to post

exactly, filebot recognizes movies and series and sort them into the proper folders ...

 

and yes, there are dockers available, free ones (frozen older version) or new one (paid version of filebot).

Share this post


Link to post
27 minutes ago, alturismo said:

exactly, filebot recognizes movies and series and sort them into the proper folders ...

 

and yes, there are dockers available, free ones (frozen older version) or new one (paid version of filebot).

 

Thanks.

 

I'm thinking all I really need is a simple script now that watches two folders.  Something like:

 

If file exists in /downloadsPP/movies/Movie_Title, move to /mnt/cache/media/movies/Movie_Title/

if file exists in /downloadsPP/series, moves to /mnt/cache/media/TV/Series/

Share this post


Link to post
1 hour ago, IamSpartacus said:

 

Thanks.

 

I'm thinking all I really need is a simple script now that watches two folders.  Something like:

 

If file exists in /downloadsPP/movies/Movie_Title, move to /mnt/cache/media/movies/Movie_Title/

if file exists in /downloadsPP/series, moves to /mnt/cache/media/TV/Series/

Yea if you can get it to include the folders then it should be really easy to setup. If you can get handbrake to output the files into the original folder structure like you're saying you can easily get it to copy them back by just changing the "/downloads/" to "mnt/cache/media/" It's a tad busy at work rn but if I don't have time to set it up, I'll get a rough draft setup tonight and send it your way.

Share this post


Link to post
Posted (edited)
1 hour ago, zacharyd3 said:

Yea if you can get it to include the folders then it should be really easy to setup. If you can get handbrake to output the files into the original folder structure like you're saying you can easily get it to copy them back by just changing the "/downloads/" to "mnt/cache/media/" It's a tad busy at work rn but if I don't have time to set it up, I'll get a rough draft setup tonight and send it your way.

 

I've tested this with handbrake.  Say the following file/folder exists:

 

/mnt/cache/Media/TV/The Daily Show/Season 24/The Daily Show - S24E86 - Bernie Sanders.mkv

 

Then handbrake will output the converted file to:

 

/mnt/user/DownloadsPP/TV/The Daily Show/Season 24/The Daily Show - S24E86 - Bernie Sanders.mkv

 

And the same with movies, it will match the exact folder structure.  So all I need is a script that will watch the /mnt/user/DownloadsPP/TV and /mnt/user/DownloadsPP/Movies directories and move the entire folder structure underneath into /mnt/cache/Media/ or /mnt/use/Media/

Edited by IamSpartacus

Share this post


Link to post
23 minutes ago, IamSpartacus said:

 

I've tested this with handbrake.  Say the following file/folder exists:

 


/mnt/cache/Media/TV/The Daily Show/Season 24/The Daily Show - S24E86 - Bernie Sanders.mkv

 

Then handbrake will output the converted file to:

 


/mnt/user/DownloadsPP/TV/The Daily Show/Season 24/The Daily Show - S24E86 - Bernie Sanders.mkv

 

And the same with movies, it will match the exact folder structure.  So all I need is a script that will watch the /mnt/user/DownloadsPP/TV and /mnt/user/DownloadsPP/Movies directories and move the entire folder structure underneath into /mnt/cache/Media/ or /mnt/use/Media/

Actually, Disregard the code I just sent, you can actually take out one line for the media variable, as I never ended up needing it, haha.

 

This should work just fine:

 

#!/bin/bash
debug=1																		#Change this to 1 for more logging, 0 for less

#Start with a clear screen
clear

#Set working directories
output=/mnt/user/DownloadsPP/											#Change this to your handbrake output directory
movies=/mnt/cache/Media/Movies/											#This should be your movies directory
series=/mnt/cache/Media/TV/												#This should be your series directory

#Search the output directory for new media
find $output -type f | while read file
do
	#Parse the movie name from the filename
	movieParsed0=$(echo "$file" | cut -d'.' -f1)
	movieParsed1=$(echo "$movieParsed0" | cut -d'/' -f7) 						#Lower or raise the number in f9 until "Movie parsed to:" shows just the movie name.
	
	if [ $debug -eq "1" ]
	then
		echo "Full filename: "$file	
	fi
	
	#Check if the file is a movie
	movieFolder=$movies$movieParsed1
		
	if [ -d "${movieFolder}" ]
	then	
		if [ $debug -eq "1" ]
		then
			echo "Movie parsed to: "$movieParsed1
			echo "Movie Folder: "$movieFolder
		fi
		#Move any found files
		#mv "${file}" "${movieFolder}"; echo $movieParsed1" Moved"				#Once all the outputs look correct, remove the "#"
		
	else
		seriesParsed0=$(echo "$file" | cut -d'.' -f1)
		seriesParsed1=$(echo "$seriesParsed0" | cut -d'/' -f6)					#Lower or raise the number in f8 until "Series parsed to:" shows just the movie name.
		seasonParsed0=$(echo "$seriesParsed0" | cut -d'/' -f7)					#Change f9 to one higher than  the previous line
		episodeParsed0=$(echo "$seriesParsed0" | cut -d'/' -f8)				#Change f10 to one higher than  the previous line
		seriesFull=$series$seriesParsed1/$seasonParsed0
		
		if [ -d "${seriesFull}" ]
		then	
			if [ $debug -eq "1" ]
			then
				echo "Series parsed to: "$seriesParsed1
				echo "Season parsed to: "$seasonParsed0
				echo "Series Folder: "$seriesFull
			fi
			#mv "${file}" "${seriesFull}"; echo $seriesParsed1" Moved"			#Once all the outputs look correct, remove the "#"
		fi		
	fi
	echo ""
done

 

Share this post


Link to post
2 minutes ago, zacharyd3 said:

Actually, Disregard the code I just sent, you can actually take out one line for the media variable, as I never ended up needing it, haha.

 

This should work just fine:

 


#!/bin/bash
debug=1																		#Change this to 1 for more logging, 0 for less

#Start with a clear screen
clear

#Set working directories
output=/mnt/user/DownloadsPP/											#Change this to your handbrake output directory
movies=/mnt/cache/Media/Movies/											#This should be your movies directory
series=/mnt/cache/Media/TV/												#This should be your series directory

#Search the output directory for new media
find $output -type f | while read file
do
	#Parse the movie name from the filename
	movieParsed0=$(echo "$file" | cut -d'.' -f1)
	movieParsed1=$(echo "$movieParsed0" | cut -d'/' -f7) 						#Lower or raise the number in f9 until "Movie parsed to:" shows just the movie name.
	
	if [ $debug -eq "1" ]
	then
		echo "Full filename: "$file	
	fi
	
	#Check if the file is a movie
	movieFolder=$movies$movieParsed1
		
	if [ -d "${movieFolder}" ]
	then	
		if [ $debug -eq "1" ]
		then
			echo "Movie parsed to: "$movieParsed1
			echo "Movie Folder: "$movieFolder
		fi
		#Move any found files
		#mv "${file}" "${movieFolder}"; echo $movieParsed1" Moved"				#Once all the outputs look correct, remove the "#"
		
	else
		seriesParsed0=$(echo "$file" | cut -d'.' -f1)
		seriesParsed1=$(echo "$seriesParsed0" | cut -d'/' -f6)					#Lower or raise the number in f8 until "Series parsed to:" shows just the movie name.
		seasonParsed0=$(echo "$seriesParsed0" | cut -d'/' -f7)					#Change f9 to one higher than  the previous line
		episodeParsed0=$(echo "$seriesParsed0" | cut -d'/' -f8)				#Change f10 to one higher than  the previous line
		seriesFull=$series$seriesParsed1/$seasonParsed0
		
		if [ -d "${seriesFull}" ]
		then	
			if [ $debug -eq "1" ]
			then
				echo "Series parsed to: "$seriesParsed1
				echo "Season parsed to: "$seasonParsed0
				echo "Series Folder: "$seriesFull
			fi
			#mv "${file}" "${seriesFull}"; echo $seriesParsed1" Moved"			#Once all the outputs look correct, remove the "#"
		fi		
	fi
	echo ""
done

 

 

Dude you are awesome.  This works perfectly.  Now I guess I just need to schedule a cron job to make this run constantly?

 

And the last piece would be a cleanup of the folder it's moving from.  Otherwise I'm going to wind up with a ton of empty folders in /mnt/DownloadsPP/TV and /mnt/Downloads/Movies.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.