recordingprocessing/TVEncoder.py

141 lines
4.1 KiB
Python
Raw Normal View History

2013-07-05 14:15:03 +10:00
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 5 14:14:22 2013
@author: shanef
"""
2013-07-05 15:35:12 +10:00
import sys
import getopt
2013-07-08 17:21:42 +10:00
from libfilemanager import FileManager
2013-07-06 20:18:52 +10:00
from libsettings import Settings
2013-07-11 00:30:00 +10:00
import libhandbrake
2013-07-09 12:14:02 +10:00
from libtvdatasource import TVData
2013-07-10 23:13:44 +10:00
from collections import namedtuple
2013-07-13 11:55:36 +10:00
from termcolor import colored
2013-07-05 21:27:53 +10:00
2013-07-05 22:40:17 +10:00
2013-07-10 23:13:44 +10:00
def showhelp():
"""
Prints the command lines switches that are valid for the program.
"""
2013-07-10 15:45:42 +10:00
print 'TVEncoder.py -p -n <number of files to prepare for processing> ' \
'- prepare n recordings'
print 'TVEncoder.py -p -l -n <number of files to process> - lists the ' \
'files that will be processed without actually encoding them'
2013-07-05 21:27:53 +10:00
print 'TVEncoder.py -e - encode the files that have been processed'
print 'TVEncoder.py -e -l - list the files that would be encoded'
2013-07-10 15:45:42 +10:00
def print_shows(shows):
2013-07-10 23:13:44 +10:00
"""
Prints he details of the shows.
2013-07-10 23:13:44 +10:00
"""
okshows = []
noepisodes = []
existingfiles = []
2013-07-05 15:35:12 +10:00
for show in shows:
showstr = str(show)
2013-07-13 16:07:45 +10:00
errors = show.checkproblems()
if not errors:
okshows.append(showstr)
elif "NO_EPISODE" in errors:
noepisodes.append(showstr)
elif "FILE_EXISTS" in errors:
existingfiles.append(showstr)
for show in okshows:
print show
2013-07-10 15:45:42 +10:00
if noepisodes:
print colored("\nDetails of the episode could not be determined for "
"the following shows:", 'red')
for show in noepisodes:
print colored(show, 'red')
2013-07-13 16:07:45 +10:00
if existingfiles:
print colored("\nThe following shows have a pre-existing "
"output file:", 'red')
for show in existingfiles:
print colored(show, 'red')
2013-07-06 20:18:52 +10:00
2013-07-15 08:31:45 +10:00
2013-07-10 23:13:44 +10:00
def processarguments(options):
"""
Determine the actions required from the input flags
"""
inputoptions = namedtuple("inputoptions",
"numfiles doencode readonly dolist")
2013-07-13 21:06:47 +10:00
inputoptions.readonly = False
2013-07-10 23:13:44 +10:00
for opt, arg in options:
2013-07-10 15:45:42 +10:00
if opt == '-h':
2013-07-10 23:13:44 +10:00
showhelp()
2013-07-10 15:45:42 +10:00
sys.exit()
elif opt == "-p":
2013-07-10 23:13:44 +10:00
inputoptions.doencode = False
2013-07-10 15:45:42 +10:00
elif opt == "-e":
2013-07-10 23:13:44 +10:00
inputoptions.doencode = True
2013-07-10 15:45:42 +10:00
elif opt == "-n":
2013-07-10 23:13:44 +10:00
inputoptions.numfiles = arg
2013-07-10 15:45:42 +10:00
elif opt == "-l":
2013-07-10 23:13:44 +10:00
inputoptions.readonly = True
return inputoptions
def main(argv):
"""
The main program for TVEncoder.
"""
try:
opts, args = getopt.getopt(argv, "hlpen:")
except getopt.GetoptError:
showhelp()
sys.exit(2)
inputoptions = processarguments(opts)
2013-07-10 15:45:42 +10:00
2013-07-08 17:21:42 +10:00
settings = Settings("settings.cfg")
2013-07-10 23:13:44 +10:00
filemanager = FileManager(settings)
2013-07-05 15:35:12 +10:00
2013-07-13 21:06:47 +10:00
if inputoptions.readonly:
2013-07-10 23:13:44 +10:00
if inputoptions.doencode:
2013-07-05 21:27:53 +10:00
#Generate the list of files that would be encoded
2013-07-10 23:13:44 +10:00
showdata = filemanager.getencodingfiles(inputoptions.readonly)
print_shows(showdata)
2013-07-05 21:27:53 +10:00
else:
# Generate the list of files to process
2013-07-10 23:13:44 +10:00
shows = filemanager.getfilestoprepare(inputoptions.numfiles)
2013-07-06 21:20:21 +10:00
print "num results: {0}".format(len(shows))
print_shows(shows)
2013-07-05 21:27:53 +10:00
else:
2013-07-10 23:13:44 +10:00
if inputoptions.doencode:
2013-07-05 21:27:53 +10:00
#Encode the files and move them to their final destination
2013-07-10 23:13:44 +10:00
showdata = filemanager.getencodingfiles(inputoptions.readonly)
2013-07-10 15:45:42 +10:00
2013-07-10 23:13:44 +10:00
for show in showdata:
2013-07-13 21:09:14 +10:00
if filemanager.checkfileexists(show.outputfile):
2013-07-10 15:45:42 +10:00
print "File {0} already exists. Cannot process." \
2013-07-13 21:09:14 +10:00
.format(show.outputfile)
2013-07-05 21:27:53 +10:00
else:
2013-07-11 00:30:00 +10:00
result = libhandbrake.encode(settings.handbrakecommand(),
2013-07-13 21:09:14 +10:00
show.inputfile,
show.outputfile)
2013-07-10 23:13:44 +10:00
# TODO do something with the result
filemanager.performpostencodefileoperations(
2013-07-13 21:09:14 +10:00
show.inputfile, show.outputfile)
2013-07-05 21:27:53 +10:00
else:
2013-07-10 23:13:44 +10:00
# Process files for encoding
shows = filemanager.getfilestoprepare(inputoptions.numfiles)
tvdata = TVData(settings)
tvdata.prepareepisodes(shows)
2013-07-05 15:35:12 +10:00
2013-07-10 15:45:42 +10:00
2013-07-05 15:35:12 +10:00
if __name__ == "__main__":
main(sys.argv[1:])