-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdisc_track.py
executable file
·406 lines (289 loc) · 13.1 KB
/
disc_track.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import json
import logging
import subprocess
import apppath
dirname = os.path.dirname(os.path.realpath( __file__ ))
sys.path.append( os.path.join(dirname,"subtitle") )
import caption
class VideoTrack(object):
def __init__(self):
self.bytes=0
self.megabytes=0.0
self.gigabytes=0.0
self.chapters=0.0
self.durationS=0.0
self.subtitles=[]
def numberOfSubtitles(self):
return len(self.subtitles)
def numberOfSubtitlesOfLanguage(self,lang):
count = 0
for i in range(0,self.numberOfSubtitles()):
if self.subtitleLanguageAtIndex(i) == lang:
count += 1
return count
def allSubtitleLanguages(self):
langs = []
for i in range(0,self.numberOfSubtitles()):
langs.append(self.subtitleLanguageAtIndex(i))
langs = list(set(langs))
langs.sort()
return langs
def subtitleOfLanguageAtSubindex(self,lang,index):
subLang = None
count = 0
for i in range(0,len(self.subtitles)):
langAtIndex = self.subtitleLanguageAtIndex(i)
if langAtIndex == lang:
if index == count:
subLang = self.subtitleAtIndex(i)
count += 1
return subLang
def subtitleLanguageAtIndex(self,index):
lang = None
try:
lang = self.subtitleAtIndex(index).language
except:
pass
return lang
def subtitleAtIndex(self,index):
subLang = None
if index < len(self.subtitles):
try:
subLang = self.subtitles[index]
except:
pass
return subLang
def serialize(self):
retDict = {}
retDict['discname'] = self.disc_name
retDict['bytes'] = self.bytes
retDict['chapters'] = self.chapters
retDict['duration'] = self.durationS
retDict['segmentsmap'] = self.segmentsMap
retDict['titleid'] = self.titleID
return json.dumps(retDict)
def __hash__(self):
return hash((self.bytes, self.durationS, str(self.subtitles)))
def __repr__(self):
retStr = '<track '
retStr += 'Megabytes: ' + str(self.megabytes) + ', '
retStr += 'DurationS: ' + str(self.durationS) + ', '
retStr += 'Subtitles: ' + str(self.subtitles) + '>'
return retStr
class LocalTrackMkv(VideoTrack):
def __init__(self,filepath):
if os.path.exists(filepath) == False:
raise Exception('Cannot create LocalTrackMkv object when file does not exist: ' + str(filepath))
super(LocalTrackMkv, self).__init__()
self.filepath=filepath
self.bytes=float(os.path.getsize(filepath))
self.megabytes=self.bytes/1024.0
self.gigabytes=self.bytes/1024.0/1024.0
self.chapters=0.0 #TODO
self.mkvInfo = self._loadMkvInfo()
if self.mkvInfo:
self.subtitles = []
for i in range(0,self.numberOfSubtitles()):
self.subtitles.append(None)
if self.mkvInfo:
self.durationS = int( re.findall(r'\+\ Duration\:\ (\d+)',self.mkvInfo)[0] )
logging.debug('LocalTrackMkv intialized for path ' + str(self.filepath))
def numberOfSubtitles(self):
numberOfSubtitles = len(re.findall(r'Track\ type\:\ subtitles',self.mkvInfo))
return numberOfSubtitles
def subtitleAtIndex(self,index):
sub = None
if index < len(self.subtitles):
sub = self.subtitles[index]
if sub == None:
self._loadSubtitleAtIndex(index)
sub = self.subtitles[index]
logging.debug('Loaded sub at index: ' + str(index) + ', ' + str(sub))
assert(sub)
return sub
def subtitleLanguageAtIndex(self,index):
subLang = None
if index < len(self.subtitles):
if self.subtitles[index]:
subLang = self.subtitles[index].language
else:
currentSubtitleIndex = 0
if self.mkvInfo:
segmentTracks = self.mkvInfo[self.mkvInfo.find('| + A track'):len(self.mkvInfo)]
for segment in segmentTracks.split('| + A track'):
if len(re.findall(r'Track\ type\:\ subtitles',segment)) > 0:
if index == currentSubtitleIndex:
try:
subLang = re.findall(r'Language\:\ ...?',segment)[0].replace('Language: ','').strip()
except:
#Language not set, assume english
subLang = 'eng'
break
currentSubtitleIndex += 1
return subLang
def _loadMkvInfo(self):
cmdargs = [apppath.mkvinfo(),self.filepath]
logging.debug('Running command ' + ' '.join(cmdargs))
cmd = subprocess.Popen(cmdargs,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd.wait()
response = cmd.communicate()
info = response[0].strip()
if '(MKVInfo) Error: Couldn\'t open input file' in info:
logging.error('Failed to load mkvInfo: ' + info)
info = None
return info
def vobsubDataForTrackNumber(self,trackNumber):
tmpDir = apppath.pathTemporary('disk_track')
vobsubFile = os.path.join(tmpDir,'tmpFile.sub')
if os.path.exists(vobsubFile):
os.remove(vobsubFile)
idxFile = os.path.join(tmpDir,'tmpFile.idx')
if os.path.exists(idxFile):
os.remove(idxFile)
cmdargs = [apppath.mkvextract(),'tracks',self.filepath,str(trackNumber)+':'+vobsubFile]
logging.debug('Running command ' + ' '.join(cmdargs))
cmd = subprocess.Popen(cmdargs,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# cmd.wait()
response = cmd.communicate()
vobsubdata = None
idxdata = None
if os.path.exists(vobsubFile) and os.path.exists(idxFile):
logging.debug('Found vob sub file ' + vobsubFile)
fSub = open(vobsubFile,'r')
vobsubdata = fSub.read()
fSub.close()
fIdx = open(idxFile,'r')
idxdata = fIdx.read()
fIdx.close()
else:
logging.error('Failed to find vobsub file ' + str(vobsubFile))
return [vobsubdata,idxdata]
def pgsDataForTrackNumber(self,trackNumber):
tmpDir = apppath.pathTemporary('disk_track')
pgsFile = os.path.join(tmpDir,'tmpFile.sup')
if os.path.exists(pgsFile):
os.remove(pgsFile)
cmdargs = [apppath.mkvextract(),'tracks',self.filepath,str(trackNumber)+':'+pgsFile]
logging.debug('Running command ' + ' '.join(cmdargs))
cmd = subprocess.Popen(cmdargs,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
response = cmd.communicate()
pgsdata = None
if os.path.exists(pgsFile):
logging.debug('Found pgs file ' + pgsFile)
fPgs = open(pgsFile,'r')
pgsdata = fPgs.read()
assert(len(pgsdata)>0)
fPgs.close()
else:
logging.error('Failed to find PGS file ' + str(pgsFile))
return pgsdata
def _loadSubtitleAtIndex(self,subIndex):
logging.debug('Loading subtitles for ' + str(self.filepath) + ', at index: ' + str(subIndex))
currentSubtitleIndex = 0
segmentTracks = self.mkvInfo[self.mkvInfo.find('| + A track'):len(self.mkvInfo)]
for segment in segmentTracks.split('| + A track'):
if '| + Track type: subtitles' in segment:
codec = re.findall(r'Codec ID\:\ .*\n',segment)[0].replace('Codec ID: ','').strip()
tracknum = int( re.findall(r'track\ ID\ for\ mkvmerge\ \&\ mkvextract\:\ \d+',segment)[0].replace('track ID for mkvmerge & mkvextract: ','').strip() )
if currentSubtitleIndex == subIndex:
lang = self.subtitleLanguageAtIndex(currentSubtitleIndex)
assert(lang)
if codec.lower() == 's_vobsub':
logging.debug('Loading vobsub data for tracknum ' + str(tracknum) + ' language ' + str(lang))
vobsubdata, idxdata = self.vobsubDataForTrackNumber(tracknum)
assert(vobsubdata)
assert(idxdata)
newSub = caption.VobSubCaption(vobsubdata,idxdata,lang)
newSub.data_source = 'makemkv'
assert(len(newSub.text)>0)
logging.debug('Loaded caption: ' + str(newSub) + 'for index ' + str(subIndex))
self.subtitles[subIndex] = newSub
assert(self.subtitles[subIndex])
elif codec.lower() == 's_hdmv/pgs':
logging.debug('Loading PGS data for tracknum ' + str(tracknum) + ' language ' + str(lang))
'''sub subtract 1 from tracknumber when called mkextract'''
pgsdata = self.pgsDataForTrackNumber(tracknum)
assert(len(pgsdata)>0)
newSub = caption.PGSCaption(pgsdata,lang)
newSub.data_source = 'makemkv'
assert(len(newSub.text)>0)
logging.debug('Loaded caption: ' + str(newSub) + 'for index ' + str(subIndex))
self.subtitles[subIndex] = newSub
assert(self.subtitles[subIndex])
elif codec.lower() == 's_text/utf8':
logging.debug('Loading srt data for tracknum ' + str(tracknum) + ' language ' + str(lang))
#TODO
else:
logging.warn('Unrecognised subtitles codec: ' + codec)
currentSubtitleIndex += 1
class DiscTrack(VideoTrack):
def __init__(self,dictLoad=None):
super(DiscTrack, self).__init__()
self.disc_name=''
self.segmentsMap=[]
self.titleID=0
self.trackNumber=0
self.outputFileName=''
if ( dictLoad is not None ) and ( type(dictLoad) == type({}) ):
self.disc_name = dictLoad['discname']
self.bytes = float(dictLoad['bytes'])
self.megabytes = self.bytes / 1024.0 / 1024.0
self.gigabytes = self.bytes / 1024.0 / 1024.0 / 1024.0
self.chapters = dictLoad['chapters']
self.durationS = dictLoad['duration']
self.segmentsMap = dictLoad['segmentsmap']
self.titleID = dictLoad['titleid']
self.trackNumber = dictLoad['tracknumber']
self.outputFileName = dictLoad['filename']
def __repr__(self):
retStr = '<track '
retStr += 'DiscName: ' + self.disc_name + ', '
retStr += 'DeviceName: ' + self.disc_device + ', '
retStr += 'Gigabytes: ' + str(self.gigabytes) + ', '
retStr += 'Chapters: ' + str(self.chapters) + ', '
retStr += 'Duration: ' + str(self.durationS) + ', '
retStr += 'SegmentsMap: ' + str(self.segmentsMap) + ', '
retStr += 'TitleID: ' + str(self.titleID) + ', '
retStr += 'TrackNumber: ' + str(self.trackNumber) + ', '
retStr += 'Filename: ' + self.outputFileName + '>'
return retStr
def tracksOverDuration(durationMin,discTracks):
tracksRet = []
for track in discTracks:
if track.durationS >= durationMin:
tracksRet.append(track)
return tracksRet
def tracksUnderDuration(durationMax,discTracks):
tracksRet = []
for track in discTracks:
if track.durationS <= durationMax:
tracksRet.append(track)
return tracksRet
def tracksBetweenDurationMinMax(tracks,durationMin,durationMax):
return tracksUnderDuration(durationMax, tracksOverDuration(durationMin,tracks))
def test():
dictLoad = {}
dictLoad['discname'] = 'disc'
dictLoad['bytes'] = 999
dictLoad['chapters'] = 10
dictLoad['duration'] = 300
dictLoad['segmentsmap'] = [[0,1],[2,3]]
dictLoad['titleid'] = 1
dictLoad['filename'] = 'file1'
dictLoad['tracknumber'] = 'file1'
track = DiscTrack(dictLoad)
assert track.disc_name == 'disc'
assert track.bytes == 999
assert track.chapters == 10
assert track.durationS == 300
assert track.segmentsMap == [[0,1],[2,3]]
assert track.titleID == 1
assert track.outputFileName == 'file1'
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test()