[Request] mkclean PP-script

Share your scripts or request scripts with specific features.
Forum rules
Please keep the forum clean - one topic per script. Questions not related to a specific script should be posted in Support forum.
Post Reply
rubylaser
Posts: 34
Joined: 06 Feb 2014, 14:47

[Request] mkclean PP-script

Post by rubylaser » 19 Nov 2015, 20:37

Hello, I wonder if someone would be so kind as to create a script that runs mkclean on finished mkv downloads? I found a thread on Sonarr's forums where someone explains the benefits of running mkclean on files and another member even created and linked to an NZBGet pp-script, but the links are all dead. Any help is greatly appreciated.

Here's the thread...
https://forums.sonarr.tv/t/optimizing-m ... ssing/4275

Here's more info about mkclean.
http://www.matroska.org/downloads/mkclean.html

rubylaser
Posts: 34
Joined: 06 Feb 2014, 14:47

Re: [Request] mkclean PP-script

Post by rubylaser » 19 Nov 2015, 21:38

I just PM'd the author of the script and he provided it to me. I'm sure this can be spruced up a bit if someone was willing.

Code: Select all

#!/bin/bash

# Clean up mkv files to make them more stream-able

##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Runs mkclean on mkv files to make them more stream-able


##############################################################################
#### OPTIONS                                                               ###


### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

#Clean the mkv (will make a clean.<filename>

echo "Running mkclean on mkv files..."
find "$NZBPP_DIRECTORY" -name '*.mkv' -execdir /usr/local/bin/mkclean --optimize --quiet {} \;

#Rename to remove 'clean.'

echo "Renaming cleaned files..."
find "$NZBPP_DIRECTORY" -name '*.mkv' -execdir rename -f 's#clean.##' {} \;

# Exit good no matter what
exit 93
Last edited by rubylaser on 20 Nov 2015, 00:11, edited 4 times in total.

hugbug
Developer & Admin
Posts: 7645
Joined: 09 Sep 2008, 11:58
Location: Germany

Re: [Request] mkclean PP-script

Post by hugbug » 19 Nov 2015, 22:19

Add at the end:

Code: Select all

exit 93
For more info see https://github.com/nzbget/nzbget/wiki/P ... exit-codes

rubylaser
Posts: 34
Joined: 06 Feb 2014, 14:47

Re: [Request] mkclean PP-script

Post by rubylaser » 19 Nov 2015, 23:24

Thanks! That did it :)

preacher65
Posts: 1
Joined: 14 Feb 2016, 08:56

Re: [Request] mkclean PP-script

Post by preacher65 » 14 Feb 2016, 09:10

Hi, I've been trying to implement this on a Windows install, using Python. I'm not very experienced at scripting, and have never used Python before, but I've looked at a number of other scripts to try and see how they work, so a lot of the code has been lifted from other scripts. I wasn't sure whether all of the checks were relevant to this script, but I've left them in.

I wondered if anyone was able to review this and point out any mistakes or inefficiencies:

Code: Select all

#!/usr/bin/env python
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Runs mkclean on mkv files to make them more stream-able
#
# NOTE: This script requires Python to be installed on your system.

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import os
import sys
import subprocess


# NZBGet Exit Codes
NZBGET_POSTPROCESS_PARCHECK = 92
NZBGET_POSTPROCESS_SUCCESS = 93
NZBGET_POSTPROCESS_ERROR = 94
NZBGET_POSTPROCESS_NONE = 95

if not os.environ.has_key('NZBOP_SCRIPTDIR'):
    print "This script can only be called from NZBGet (11.0 or later)."
    sys.exit(0)

if os.environ['NZBOP_VERSION'][0:5] < '11.0':
    print "NZBGet Version %s is not supported. Please update NZBGet." % (str(os.environ['NZBOP_VERSION']))
    sys.exit(0)

print "Script triggered from NZBGet Version %s." % (str(os.environ['NZBOP_VERSION']))
status = 0
if os.environ.has_key('NZBPP_TOTALSTATUS'):
    if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS':
        print "Download failed with status %s." % (os.environ['NZBPP_STATUS'])
        status = 1

else:
    # Check par status
    if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4':
        print "Par-repair failed, setting status \"failed\"."
        status = 1

    # Check unpack status
    if os.environ['NZBPP_UNPACKSTATUS'] == '1':
        print "Unpack failed, setting status \"failed\"."
        status = 1

    if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0':
        # Unpack was skipped due to nzb-file properties or due to errors during par-check

        if os.environ['NZBPP_HEALTH'] < 1000:
            print "Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \"failed\"."
            print "Please check your Par-check/repair settings for future downloads."
            status = 1

        else:
            print "Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful."
            print "Please check your Par-check/repair settings for future downloads."

# Check if destination directory exists (important for reprocessing of history items)
if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
    print "Nothing to post-process: destination directory", os.environ['NZBPP_DIRECTORY'], "doesn't exist. Setting status \"failed\"."
    status = 1

# All checks done, now launching the script.
if status == 1:
    sys.exit(NZBGET_POSTPROCESS_NONE)

for dirpath, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
    for file in filenames:
        if file.endswith('.mkv'):
			filepath = os.path.join(dirpath, file)
			print "cleaning file", filepath
			try:
				subprocess.call(["F:\\Downloads\\mkclean-win32.v0.8.7\\mkclean.exe", "--optimize", "--quiet", filepath])
			except:
				print "Error: unable to clean file", filepath
				sys.exit(NZBGET_POSTPROCESS_ERROR)
			print "renaming cleaned file", filepath
			try:
				newfile = "clean." + file
				newfilepath = os.path.join(dirpath, newfile)
				if os.path.exists(filepath):
					if os.path.exists(newfilepath):
						os.remove(filepath)
						os.rename(newfilepath, filepath)
					else:
						print "Error: file not present", newfilepath
						sys.exit(NZBGET_POSTPROCESS_ERROR)
				else:
					print "Error: file not present", filepath
					sys.exit(NZBGET_POSTPROCESS_ERROR)
			except:
				print "Error: unable to rename file", newfilepath
				sys.exit(NZBGET_POSTPROCESS_ERROR)
			
sys.exit(NZBGET_POSTPROCESS_SUCCESS)
Update: Well it seems to work, though I'm sure it could be greatly improved. I'm also trying to have the path to the mkclean.exe executable set as a script variable through the NZBGet gui, but can't figure out how to use that variable in the subprocess.call.

Many thanks for any help.

Madic
Posts: 1
Joined: 14 Apr 2016, 18:48

Re: [Request] mkclean PP-script

Post by Madic » 14 Apr 2016, 18:53

preacher65 wrote:Update: Well it seems to work, though I'm sure it could be greatly improved. I'm also trying to have the path to the mkclean.exe executable set as a script variable through the NZBGet gui, but can't figure out how to use that variable in the subprocess.call.

Many thanks for any help.
Just registered for this because I like the idea of the script and it seems my attempt in a config page works:

Code: Select all

#!/usr/bin/env python
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Runs mkclean on mkv files to make them more stream-able
#
# NOTE: This script requires Python to be installed on your system.

##############################################################################
### OPTIONS                                                                ###

# mkclean Path
#
# Path to the mkclean executable
#mkcleanpath=/usr/local/bin/mkclean

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import os
import sys
import subprocess


# NZBGet Exit Codes
NZBGET_POSTPROCESS_PARCHECK = 92
NZBGET_POSTPROCESS_SUCCESS = 93
NZBGET_POSTPROCESS_ERROR = 94
NZBGET_POSTPROCESS_NONE = 95

if not os.environ.has_key('NZBOP_SCRIPTDIR'):
    print "This script can only be called from NZBGet (11.0 or later)."
    sys.exit(0)

if os.environ['NZBOP_VERSION'][0:5] < '11.0':
    print "NZBGet Version %s is not supported. Please update NZBGet." % (str(os.environ['NZBOP_VERSION']))
    sys.exit(0)

print "Script triggered from NZBGet Version %s." % (str(os.environ['NZBOP_VERSION']))
status = 0
if os.environ.has_key('NZBPP_TOTALSTATUS'):
    if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS':
        print "Download failed with status %s." % (os.environ['NZBPP_STATUS'])
        status = 1

else:
    # Check par status
    if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4':
        print "Par-repair failed, setting status \"failed\"."
        status = 1

    # Check unpack status
    if os.environ['NZBPP_UNPACKSTATUS'] == '1':
        print "Unpack failed, setting status \"failed\"."
        status = 1

    if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0':
        # Unpack was skipped due to nzb-file properties or due to errors during par-check

        if os.environ['NZBPP_HEALTH'] < 1000:
            print "Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \"failed\"."
            print "Please check your Par-check/repair settings for future downloads."
            status = 1

        else:
            print "Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful."
            print "Please check your Par-check/repair settings for future downloads."

# Check if destination directory exists (important for reprocessing of history items)
if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
    print "Nothing to post-process: destination directory", os.environ['NZBPP_DIRECTORY'], "doesn't exist. Setting status \"failed\"."
    status = 1

# All checks done, now launching the script.
if status == 1:
    sys.exit(NZBGET_POSTPROCESS_NONE)

for dirpath, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
    for file in filenames:
        if file.endswith('.mkv'):
         filepath = os.path.join(dirpath, file)
         print "cleaning file", filepath
         try:
             subprocess.call([os.environ.get('NZBPO_MKCLEANPATH'), "--optimize", "--quiet", filepath])
         except:
            print "Error: unable to clean file", filepath
            sys.exit(NZBGET_POSTPROCESS_ERROR)
         print "renaming cleaned file", filepath
         try:
            newfile = "clean." + file
            newfilepath = os.path.join(dirpath, newfile)
            if os.path.exists(filepath):
               if os.path.exists(newfilepath):
                  os.remove(filepath)
                  os.rename(newfilepath, filepath)
               else:
                  print "Error: file not present", newfilepath
                  sys.exit(NZBGET_POSTPROCESS_ERROR)
            else:
               print "Error: file not present", filepath
               sys.exit(NZBGET_POSTPROCESS_ERROR)
         except:
            print "Error: unable to rename file", newfilepath
            sys.exit(NZBGET_POSTPROCESS_ERROR)

sys.exit(NZBGET_POSTPROCESS_SUCCESS)
Please test it in your environment.

rubylaser
Posts: 34
Joined: 06 Feb 2014, 14:47

Re: [Request] mkclean PP-script

Post by rubylaser » 04 Jul 2022, 04:39

Sorry to resurrect a really old thread...I used to have this working in the past, but it doesn't seem to work anymore. Does anyone have this still working in newer versions NZBget? I would still like to be able to optimize my mkv files, but it's not working. I've compiled mkclean and have it working within my Docker container. I've configured the path to mkclean, but post processing fails.

Code: Select all

[INFO] Executing post-process-script mkclean.py for Filename.1080p.BluRay.x264
[INFO] mkclean: Script triggered from NZBGet Version 21.1.
[INFO] mkclean: cleaning file /downloads/completed/Movies/Filename.1080p.BluRay.x264/wxf0.mkv
[INFO] mkclean: Error: unable to clean file /downloads/completed/Movies/Filename.1080p.BluRay.x264/wxf0.mkv

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests