Skip to content

Instantly share code, notes, and snippets.

@ckarrie
Last active April 5, 2016 04:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckarrie/1efbb8aaa8b8038b28f3 to your computer and use it in GitHub Desktop.
Save ckarrie/1efbb8aaa8b8038b28f3 to your computer and use it in GitHub Desktop.
TVHeadend Python PostProcessing script
"""
Author: ckarrie <ckarrie@gmail.com>
Python PostProcessing script that deletes all duplicate recordings according to their
description and filesize. The script keeps the recording with the largest filesize.
Usage:
1) put this python file somewhere (i.e. /media/daten/tvheadend/pp.py)
2) Add this line to TVHeadend > Configuration > Recording > [your recording settings] > Post-processor command
replace <path/to/this/file/pp.py> according to Point 1)
/usr/bin/python2.7 /media/daten/tvheadend/pp.py "%f" "%d"
3) Record something with this recording settings
"""
import sys, os
import shutil
import re
import syslog
_p = "[TVH PP pp.py] "
#TVH_HOST = "127.0.0.1"
#TVH_PORT = 9981
args = sys.argv
current_recording_file = args[1]
current_description_file = current_recording_file + '.txt'
current_description = args[2]
descriptionfile = open(current_recording_file + ".txt", "w")
descriptionfile.write(current_description)
descriptionfile.close()
dvr_folder, dvr_filename = os.path.split(current_recording_file)
same_description = {}
print("Scanning DVR folder", dvr_folder)
syslog.syslog(_p + 'Start processing in %s' % dvr_folder)
for root, dirs, files in os.walk(dvr_folder):
syslog.syslog(_p + 'Walking through %s' % root)
for name in files:
full_filename = os.path.join(root, name)
if name.endswith('.mkv'):
syslog.syslog(_p + 'Checking %s' % full_filename)
full_txt_filename = full_filename + '.txt'
if os.path.exists(full_txt_filename):
description_f = open(full_txt_filename, 'r')
description = description_f.read()
description_f.close()
description_shortened = description[:100]
if description_shortened not in same_description.keys():
same_description[description_shortened] = [full_filename]
else:
same_description[description_shortened].append(full_filename)
else:
print("Missing description file %s, ignoring..." % full_txt_filename)
for descr, files in same_description.items():
print(" >%s< has %d file(s)" %(descr[:50], len(files)))
largest_size = 0
largest_size_filename = ''
for f in files:
size = os.path.getsize(f)
if size > largest_size:
largest_size = size
largest_size_filename = f
if largest_size and largest_size_filename and (len(files) > 1):
for fname in files:
if fname != largest_size_filename:
print(" - Removing", fname)
os.remove(fname)
os.remove(fname + '.txt')
syslog.syslog(_p + "All done")
@ckarrie
Copy link
Author

ckarrie commented Jun 2, 2014

TODO: delete those database entries if file is missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment