7d46005828d8b3b48cc18a3a6cc22f038817a993
[openblackhole/openblackhole-enigma2.git] / lib / python / Plugins / Extensions / DVDBurn / DVDTitle.py
1 from Components.config import config, ConfigSubsection, ConfigSubList, ConfigInteger, ConfigText, ConfigSelection, getConfigListEntry, ConfigSequence, ConfigYesNo
2 import TitleCutter
3
4 class ConfigFixedText(ConfigText):
5         def __init__(self, text, visible_width=60):
6                 ConfigText.__init__(self, default = text, fixed_size = True, visible_width = visible_width)
7         def handleKey(self, key):
8                 pass
9
10 class DVDTitle:
11         def __init__(self, project):
12                 self.properties = ConfigSubsection()
13                 self.properties.menutitle = ConfigText(fixed_size = False, visible_width = 80)
14                 self.properties.menusubtitle = ConfigText(fixed_size = False, visible_width = 80)
15                 self.properties.aspect = ConfigSelection(choices = [("4:3", _("4:3")), ("16:9", _("16:9"))])
16                 self.properties.widescreen = ConfigSelection(choices = [("nopanscan", "nopanscan"), ("noletterbox", "noletterbox")])
17                 self.properties.autochapter = ConfigInteger(default = 0, limits = (0, 60))
18                 self.properties.audiotracks = ConfigSubList()
19                 self.DVBname = _("Title")
20                 self.DVBdescr = _("Description")
21                 self.DVBchannel = _("Channel")
22                 self.cuesheet = [ ]
23                 self.source = None
24                 self.filesize = 0
25                 self.estimatedDiskspace = 0
26                 self.inputfile = ""
27                 self.cutlist = [ ]
28                 self.chaptermarks = [ ]
29                 self.timeCreate = None
30                 self.VideoType = -1
31                 self.project = project
32                 self.length = 0
33
34         def addService(self, service):
35                 from os import path
36                 from enigma import eServiceCenter, iServiceInformation
37                 from ServiceReference import ServiceReference
38                 from time import localtime, time
39                 self.source = service
40                 serviceHandler = eServiceCenter.getInstance()
41                 info = serviceHandler.info(service)
42                 sDescr = info and info.getInfoString(service, iServiceInformation.sDescription) or ""
43                 self.DVBdescr = sDescr
44                 sTimeCreate = info.getInfo(service, iServiceInformation.sTimeCreate)
45                 if sTimeCreate > 1:
46                         self.timeCreate = localtime(sTimeCreate)
47                 serviceref = ServiceReference(info.getInfoString(service, iServiceInformation.sServiceref))
48                 name = info and info.getName(service) or "Title" + sDescr
49                 self.DVBname = name
50                 self.DVBchannel = serviceref.getServiceName()
51                 self.inputfile = service.getPath()
52                 self.filesize = path.getsize(self.inputfile)
53                 self.estimatedDiskspace = self.filesize
54                 self.length = info.getLength(service)
55
56         def addFile(self, filename):
57                 from enigma import eServiceReference
58                 ref = eServiceReference(1, 0, filename)
59                 self.addService(ref)
60                 self.project.session.openWithCallback(self.titleEditDone, TitleCutter.CutlistReader, self)
61
62         def titleEditDone(self, cutlist):
63                 self.initDVDmenuText(len(self.project.titles))
64                 self.cuesheet = cutlist
65                 self.produceFinalCuesheet()
66
67         def initDVDmenuText(self, track):
68                 s = self.project.menutemplate.settings
69                 self.properties.menutitle.setValue(self.formatDVDmenuText(s.titleformat.getValue(), track))
70                 self.properties.menusubtitle.setValue(self.formatDVDmenuText(s.subtitleformat.getValue(), track))
71
72         def formatDVDmenuText(self, template, track):
73                 template = template.replace("$i", str(track))
74                 template = template.replace("$t", self.DVBname)
75                 template = template.replace("$d", self.DVBdescr)
76                 template = template.replace("$c", str(len(self.chaptermarks)+1))
77                 template = template.replace("$f", self.inputfile)
78                 template = template.replace("$C", self.DVBchannel)
79
80                 #if template.find("$A") >= 0:
81                 from TitleProperties import languageChoices
82                 audiolist = [ ]
83                 for audiotrack in self.properties.audiotracks:
84                         active = audiotrack.active.getValue()
85                         if active:
86                                 trackstring = audiotrack.format.getValue()
87                                 language = audiotrack.language.getValue()
88                                 if languageChoices.langdict.has_key(language):
89                                         trackstring += ' (' + languageChoices.langdict[language] + ')'
90                                 audiolist.append(trackstring)
91                 audiostring = ', '.join(audiolist)
92                 template = template.replace("$A", audiostring)
93
94                 if template.find("$l") >= 0:
95                         l = self.length
96                         lengthstring = "%d:%02d:%02d" % (l/3600, l%3600/60, l%60)
97                         template = template.replace("$l", lengthstring)
98                 if self.timeCreate:
99                         template = template.replace("$Y", str(self.timeCreate[0]))
100                         template = template.replace("$M", str(self.timeCreate[1]))
101                         template = template.replace("$D", str(self.timeCreate[2]))
102                         timestring = "%d:%02d" % (self.timeCreate[3], self.timeCreate[4])
103                         template = template.replace("$T", timestring)
104                 else:
105                         template = template.replace("$Y", "").replace("$M", "").replace("$D", "").replace("$T", "")
106                 return template
107
108         def produceFinalCuesheet(self):
109                 CUT_TYPE_IN = 0
110                 CUT_TYPE_OUT = 1
111                 CUT_TYPE_MARK = 2
112                 CUT_TYPE_LAST = 3
113
114                 accumulated_in = 0
115                 accumulated_at = 0
116                 last_in = 0
117
118                 self.cutlist = [ ]
119                 self.chaptermarks = [ ]
120
121                 # our demuxer expects *strictly* IN,OUT lists.
122                 currently_in = not any(type == CUT_TYPE_IN for pts, type in self.cuesheet)
123                 if currently_in:
124                         self.cutlist.append(0) # emulate "in" at first
125
126                 for (pts, type) in self.cuesheet:
127                         #print "pts=", pts, "type=", type, "accumulated_in=", accumulated_in, "accumulated_at=", accumulated_at, "last_in=", last_in
128                         if type == CUT_TYPE_IN and not currently_in:
129                                 self.cutlist.append(pts)
130                                 last_in = pts
131                                 currently_in = True
132
133                         if type == CUT_TYPE_OUT and currently_in:
134                                 self.cutlist.append(pts)
135
136                                 # accumulate the segment
137                                 accumulated_in += pts - last_in
138                                 accumulated_at = pts
139                                 currently_in = False
140
141                         if type == CUT_TYPE_MARK and currently_in:
142                                 # relocate chaptermark against "in" time. This is not 100% accurate,
143                                 # as the in/out points are not.
144                                 reloc_pts = pts - last_in + accumulated_in
145                                 self.chaptermarks.append(reloc_pts)
146
147                 if len(self.cutlist) > 1:
148                         part = accumulated_in / (self.length*90000.0)
149                         usedsize = int ( part * self.filesize )
150                         self.estimatedDiskspace = usedsize
151                         self.length = accumulated_in / 90000
152
153         def getChapterMarks(self, template="$h:$m:$s.$t"):
154                 timestamps = [ ]
155                 chapters = [ ]
156                 minutes = self.properties.autochapter.getValue()
157                 if len(self.chaptermarks) < 1 and minutes > 0:
158                         chapterpts = 0
159                         while chapterpts < (self.length-60*minutes)*90000:
160                                 chapterpts += 90000 * 60 * minutes
161                                 chapters.append(chapterpts)
162                 else:
163                         chapters = self.chaptermarks
164                 for p in chapters:
165                         timestring = template.replace("$h", str(p / (90000 * 3600)))
166                         timestring = timestring.replace("$m", ("%02d" % (p % (90000 * 3600) / (90000 * 60))))
167                         timestring = timestring.replace("$s", ("%02d" % (p % (90000 * 60) / 90000)))
168                         timestring = timestring.replace("$t", ("%03d" % ((p % 90000) / 90)))
169                         timestamps.append(timestring)
170                 return timestamps