forked from mdhiggins/sickbeard_mp4_automator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadSettings.py
More file actions
284 lines (258 loc) · 12.3 KB
/
Copy pathreadSettings.py
File metadata and controls
284 lines (258 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import sys
import ConfigParser
from extensions import *
try:
from babelfish import Language
except:
print "Trying to install SetupTools"
setup.ez_setup.main()
try:
from babelfish import Language
except:
print "Please install SetupTools"
class ReadSettings:
def __init__(self, directory, filename):
# Default settings for SickBeard
sb_defaults = {'host': 'localhost',
'port': '8081',
'ssl': "False",
'api_key': '', }
# Default MP4 conversion settings
mp4_defaults = {'ffmpeg': 'ffmpeg.exe',
'ffprobe': 'ffprobe.exe',
'output_directory': '',
'copy_to': '',
'move_to': '',
'output_extension': 'mp4',
'output_format': 'mp4',
'delete_original': 'True',
'relocate_moov': 'True',
'ios-audio': 'True',
'audio-language': '',
'audio-codec': 'ac3',
'video-codec': 'h264, x264',
'subtitle-language': '',
'audio-default-language': '',
'subtitle-default-language': '',
'convert-mp4': 'False',
'fullpathguess': 'True',
'tagfile': 'True',
'download-artwork': 'True',
'download-subs': 'False',
'embed-subs': 'True',
'sub-providers': 'addic7ed, podnapisi, thesubdb, opensubtitles' }
# Default settings for CouchPotato
cp_defaults = {'host': 'localhost',
'port': '5050',
'username': '',
'password': '',
'apikey': '',
'delay': '65',
'method': 'renamer',
'delete_failed': 'False',
'ssl': 'False',
'web_root': ''}
defaults = {'SickBeard': sb_defaults, 'CouchPotato': cp_defaults, 'MP4': mp4_defaults}
write = False # Will be changed to true if a value is missing from the config file and needs to be written
config = ConfigParser.SafeConfigParser()
configFile = os.path.join(directory, filename)
if os.path.isfile(configFile):
fp = open(configFile, "rb")
config.readfp(fp)
fp.close()
else:
print "Error: Config file not found, creating"
#config.filename = filename
write = True
# Make sure all sections and all keys for each section are present
for s in defaults:
if not config.has_section(s):
config.add_section(s)
write = True
for k in defaults[s]:
if not config.has_option(s, k):
config.set(s, k, defaults[s][k])
write = True
# If any keys are missing from the config file, write them
if write:
self.writeConfig(config, configFile)
#Read relevant MP4 section information
section = "MP4"
self.ffmpeg = os.path.normpath(self.raw(config.get(section, "ffmpeg"))) # Location of FFMPEG.exe
self.ffprobe = os.path.normpath(self.raw(config.get(section, "ffprobe"))) # Location of FFPROBE.exe
self.output_dir = config.get(section, "output_directory")
if self.output_dir == '':
self.output_dir = None
else:
self.output_dir = os.path.normpath(self.raw(self.output_dir)) # Output directory
self.copyto = config.get(section, "copy_to") # Directories to make copies of the final product
if self.copyto == '':
self.copyto = None
else:
self.copyto = self.copyto.split('|')
for i in xrange(len(self.copyto)):
self.copyto[i] = os.path.normpath(self.copyto[i])
if not os.path.isdir(self.copyto[i]):
try:
os.makedirs(self.copyto[i])
except:
print "Error making directory %s" % (self.copyto[i])
self.moveto = config.get(section, "move_to") # Directory to move final product to
if self.moveto == '':
self.moveto = None
else:
self.moveto = os.path.normpath(self.moveto)
if not os.path.isdir(self.moveto):
try:
os.makedirs(self.moveto)
except:
print "Error making directory %s" % (self.moveto)
self.moveto = None
self.output_extension = config.get(section, "output_extension") # Output extension
self.output_format = config.get(section, "output_format") # Output format
if self.output_format not in valid_formats:
self.output_format = 'mov'
self.delete = config.getboolean(section, "delete_original") # Delete original file
self.relocate_moov = config.getboolean(section, "relocate_moov") # Relocate MOOV atom to start of file
self.acodec = config.get(section, "audio-codec").lower() # Gets the desired audio codec, if no valid codec selected, default to AC3
if self.acodec == '':
self.acodec == ['ac3']
else:
self.acodec = self.acodec.lower().replace(' ', '').split(',')
# !!! Leaving this disabled for now, users will be responsible for knowing whicn codecs do and don't work with mp4 files !!!
#if self.acodec not in valid_audio_codecs:
# self.acodec = 'aac'
# print "Audio codec not valid, defaulting to AAC"
self.iOS = config.get(section, "ios-audio") # Creates a second audio channel if the standard output methods are different from this for iOS compatability
if self.iOS == "" or self.iOS.lower() in ['false', 'no', 'f', '0']:
self.iOS = False
else:
if self.iOS.lower() in ['true', 'yes', 't', '1']:
self.iOS = 'aac'
self.downloadsubs = config.getboolean(section, "download-subs") # Enables downloading of subtitles from the internet sources using subliminal
if self.downloadsubs:
try:
import subliminal
except Exception as e:
print e
self.downloadsubs = False
print "Subliminal is not installed, automatically downloading of subs has been disabled"
self.subproviders = config.get(section, 'sub-providers').lower()
if self.subproviders == '':
self.downloadsubs = False
print "You must specifiy at least one subtitle provider to downlaod subs automatically"
else:
self.subproviders = self.subproviders.lower().replace(' ', '').split(',')
self.embedsubs = config.getboolean(section, 'embed-subs')
self.vcodec = config.get(section, "video-codec")
if self.vcodec == '':
self.vcodec == ['h264', 'x264']
else:
self.vcodec = self.vcodec.lower().replace(' ', '').split(',')
self.awl = config.get(section, 'audio-language') # List of acceptable languages for audio streams to be carried over from the original file, separated by a comma. Blank for all
if self.awl == '':
self.awl = None
else:
self.awl = self.awl.replace(' ', '').split(',')
self.swl = config.get(section, 'subtitle-language') # List of acceptable languages for subtitle streams to be carried over from the original file, separated by a comma. Blank for all
if self.swl == '':
self.swl = None
else:
self.swl = self.swl.replace(' ', '').split(',')
self.adl = config.get(section, 'audio-default-language').strip() # What language to default an undefinied audio language tag to. If blank, it will remain undefined. This is useful for single language releases which tend to leave things tagged as und
if self.adl == "" or len(self.adl) > 3:
self.adl = None
self.sdl = config.get(section, 'subtitle-default-language').strip() # What language to default an undefinied subtitle language tag to. If blank, it will remain undefined. This is useful for single language releases which tend to leave things tagged as und
if self.sdl == ""or len(self.sdl) > 3:
self.sdl = None
# Prevent incompatible combination of settings
if self.output_dir == "" and self.delete is False:
print "Error - you must specify an alternate output directory if you aren't going to delete the original file"
sys.exit()
# Create output directory if it does not exist
if self.output_dir is not None:
if not os.path.isdir(self.output_dir):
os.makedirs(self.output_dir)
self.processMP4 = config.getboolean(section, "convert-mp4") # Determine whether or not to reprocess mp4 files or just tag them
self.fullpathguess = config.getboolean(section, "fullpathguess") # Guess using the full path or not
self.tagfile = config.getboolean(section, "tagfile") # Tag files with metadata
self.artwork = config.getboolean(section, "download-artwork") # Download and embed artwork
#Read relevant CouchPotato section information
section = "CouchPotato"
self.CP = {}
self.CP['host'] = config.get(section, "host")
self.CP['port'] = config.get(section, "port")
self.CP['username'] = config.get(section, "username")
self.CP['password'] = config.get(section, "password")
self.CP['apikey'] = config.get(section, "apikey")
self.CP['delay'] = config.get(section, "delay")
self.CP['method'] = config.get(section, "method")
self.CP['web_root'] = config.get(section, "web_root")
try:
self.CP['delay'] = float(self.CP['delay'])
except ValueError:
self.CP['delay'] = 60
try:
self.CP['delete_failed'] = config.getboolean(section, "delete_failed")
except (ConfigParser.NoOptionError, ValueError):
self.CP['delete_failed'] = False
try:
if config.getboolean(section, 'ssl'):
self.CP['protocol'] = "https://"
else:
self.CP['protocol'] = "http://"
except (ConfigParser.NoOptionError, ValueError):
self.CP['protocol'] = "http://"
#Pass the values on
self.config = config
self.configFile = configFile
def getRefreshURL(self, tvdb_id):
config = self.config
section = "SickBeard"
protocol = "http://" # SSL
try:
if config.getboolean(section, "ssl"):
protocol = "https://"
except (ConfigParser.NoOptionError, ValueError):
pass
host = config.get(section, "host") # Server Address
port = config.get(section, "port") # Server Port
api_key = config.get(section, "api_key") # Sickbeard API key
sickbeard_url = protocol + host + ":" + port + "/api/" + api_key + "/?cmd=show.refresh&tvdbid=" + str(tvdb_id)
return sickbeard_url
def writeConfig(self, config, cfgfile):
fp = open(cfgfile, "wb")
try:
config.write(fp)
except IOError:
pass
fp.close()
def raw(self, text):
escape_dict = {'\a': r'\a',
'\b': r'\b',
'\c': r'\c',
'\f': r'\f',
'\n': r'\n',
'\r': r'\r',
'\t': r'\t',
'\v': r'\v',
'\'': r'\'',
'\"': r'\"',
'\0': r'\0',
'\1': r'\1',
'\2': r'\2',
'\3': r'\3',
'\4': r'\4',
'\5': r'\5',
'\6': r'\6',
'\7': r'\7',
'\8': r'\8',
'\9': r'\9'}
output = ''
for char in text:
try:
output += escape_dict[char]
except KeyError:
output += char
return output