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