Debian Codegraph @

src/devedeng/dvdauthor_converter.py

raw
1 #!/usr/bin/env python3
2
3 # Copyright 2014 (C) Raster Software Vigo (Sergio Costas)
4 #
5 # This file is part of DeVeDe-NG
6 #
7 # DeVeDe-NG is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 3
9 # as published by the Free Software Foundation.
10 #
11 # DeVeDe-NG is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>
18
19 import os
20 import devedeng.configuration_data
21 import devedeng.executor
22
23
24 class dvdauthor_converter(devedeng.executor.executor):
25
26 def __init__(self):
27
28 devedeng.executor.executor.__init__(self)
29 self.config = devedeng.configuration_data.configuration.get_config()
30
31 def create_dvd_project(self, path, name, file_movies, menu_entries, start_with_menu, play_all_opt, menuWide):
32
33 movie_path = os.path.join(path, "dvd_tree")
34 try:
35 os.makedirs(movie_path)
36 except:
37 pass
38
39 xml_file = self.create_dvdauthor_xml(path, file_movies, menu_entries, start_with_menu, play_all_opt, menuWide)
40
41 self.command_var = []
42 self.command_var.append("dvdauthor")
43 self.command_var.append("-o")
44 self.command_var.append(movie_path)
45 self.command_var.append("-x")
46 self.command_var.append(xml_file)
47 self.use_pulse_mode = True
48 self.text = _("Creating DVD structure")
49
50 def process_stdout(self, data):
51 return
52
53 def process_stderr(self, data):
54 if (data is not None) and (data[0] != ""):
55 self.progress_bar[1].set_text(data[0])
56 return
57
58 def create_dvdauthor_xml(self, movie_folder, file_movies_in, menu_entries, start_with_menu, play_all_opt, menuWide):
59
60 file_movies = []
61 for element in file_movies_in:
62 if element.element_type != "file_movie":
63 continue
64 file_movies.append(element)
65
66 xmlpath = os.path.join(movie_folder, "xml_data")
67 xml_file_path = os.path.join(xmlpath, "dvdauthor.xml")
68 datapath = os.path.join(movie_folder, "dvd_tree")
69 try:
70 os.makedirs(xmlpath)
71 except:
72 pass
73
74 if (len(file_movies) == 1) and (menu_entries is None):
75 onlyone = True
76 else:
77 onlyone = False
78
79 if (menu_entries is None):
80 elements_per_menu = 1000
81 else:
82 elements_per_menu = len(menu_entries[0]["chapters"])
83
84 xml_file = open(xml_file_path, "w")
85 xml_file.write('<dvdauthor dest="' + datapath + '">\n')
86
87 if onlyone:
88 xml_file.write('\t<vmgm>\n')
89 xml_file.write("\t\t<menus>\n")
90
91 xml_file.write('\t\t\t<video format="')
92 if self.config.PAL:
93 xml_file.write("pal")
94 else:
95 xml_file.write("ntsc")
96 if menuWide:
97 xml_file.write('" aspect="16:9"> ')
98 else:
99 xml_file.write('" aspect="4:3"> ')
100 xml_file.write('</video>\n')
101 xml_file.write("\t\t</menus>\n")
102 xml_file.write('\t</vmgm>\n')
103 else:
104 xml_file.write('\t<vmgm>\n')
105
106 # MENU
107
108 # in the FPC we do a jump to the first menu in the first titleset if we wanted MENU
109 # or we jump to the second titleset if we didn't want MENU at
110 # startup
111
112 xml_file.write('\t\t<fpc>\n')
113 xml_file.write('\t\t\tg0=100;\n')
114 xml_file.write('\t\t\tg1=') # goto variable
115 if (menu_entries is not None) and (start_with_menu):
116 xml_file.write('0;\n') # show menu
117 else:
118 xml_file.write('100;\n') # auto play
119 xml_file.write('\t\t\tg2=1024;\n') # highlight?
120 xml_file.write('\t\t\tg3=') # play all variable
121 if play_all_opt and (menu_entries is not None) and (start_with_menu):
122 xml_file.write('1;\n') # auto play all
123 else:
124 xml_file.write('0;\n') # do not play all
125 xml_file.write('\t\t\tjump menu 1;\n')
126 xml_file.write('\t\t</fpc>\n')
127
128 # in the VMGM menu we create a code to jump to the title specified in G0
129 # but if the title is 100, we jump to the menus. There we show the menu number
130 # contained in G1
131
132 xml_file.write("\t\t<menus>\n")
133
134 xml_file.write('\t\t\t<video format="')
135 if self.config.PAL:
136 xml_file.write("pal")
137 else:
138 xml_file.write("ntsc")
139 if menuWide:
140 xml_file.write('" aspect="16:9"> ')
141 else:
142 xml_file.write('" aspect="4:3"> ')
143 xml_file.write('</video>\n')
144
145 xml_file.write('\t\t\t<pgc>\n')
146 xml_file.write('\t\t\t\t<pre>\n')
147
148 counter = 1
149 for element in file_movies:
150 xml_file.write('\t\t\t\t\tif (g0 eq ' + str(counter) + ') {\n')
151 xml_file.write('\t\t\t\t\t\tjump titleset ' +
152 str(1 + counter) + ' menu;\n')
153 xml_file.write('\t\t\t\t\t}\n')
154 counter += 1
155 xml_file.write('\t\t\t\t\tif (g0 eq 100) {\n')
156 xml_file.write('\t\t\t\t\t\tg2=1024;\n')
157 xml_file.write('\t\t\t\t\t\tjump titleset 1 menu;\n')
158 xml_file.write('\t\t\t\t\t}\n')
159 xml_file.write('\t\t\t\t</pre>\n')
160 # fake video (one black picture with one second of sound) to ensure
161 # 100% compatibility
162 xml_file.write('\t\t\t\t<vob file="')
163
164 if menuWide:
165 if self.config.PAL:
166 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_pal_wide.mpg"))))
167 else:
168 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_ntsc_wide.mpg"))))
169 else:
170 if self.config.PAL:
171 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_pal.mpg"))))
172 else:
173 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_ntsc.mpg"))))
174
175 xml_file.write('"></vob>\n')
176 xml_file.write('\t\t\t</pgc>\n')
177 xml_file.write('\t\t</menus>\n')
178 xml_file.write("\t</vmgm>\n")
179
180 xml_file.write("\n")
181
182 # the first titleset contains all the menus. G1 allows us to jump
183 # to the desired menu
184
185 xml_file.write('\t<titleset>\n')
186 xml_file.write('\t\t<menus>\n')
187 xml_file.write('\t\t\t<video format="')
188 if self.config.PAL:
189 xml_file.write("pal")
190 else:
191 xml_file.write("ntsc")
192 if menuWide:
193 xml_file.write('" aspect="16:9"> ')
194 else:
195 xml_file.write('" aspect="4:3"> ')
196 xml_file.write('</video>\n')
197
198 first_entry = True
199 menu_number = 0
200
201 counter = 1
202 title_list = []
203 for element in file_movies:
204 if element.show_in_menu:
205 title_list.append(counter)
206 counter += 1
207
208 if (menu_entries is not None):
209 nmenues = len(menu_entries)
210 title_counter = 0 # A counter for all titles
211 for menu_page in menu_entries:
212 button_counter = 0 # A counter for all buttons on the actual page
213 xml_file.write('\t\t\t<pgc>\n')
214 xml_file.write('\t\t\t\t<pre>\n')
215 # first we recover the currently selected button
216 xml_file.write('\t\t\t\t\ts8=g2;\n')
217 xml_file.write('\t\t\t\t\tg3=0;\n') # turnoff play all
218
219 if first_entry: # here we add some code to jump to each menu
220 for menu2 in range(nmenues - 1):
221 xml_file.write(
222 '\t\t\t\t\tif (g1 eq ' + str(menu2 + 1) + ') {\n')
223 xml_file.write(
224 '\t\t\t\t\t\tjump menu ' + str(menu2 + 2) + ';\n')
225 xml_file.write('\t\t\t\t\t}\n')
226
227 # this code is to fix a bug in some players
228 xml_file.write('\t\t\t\t\tif (g1 eq 100) {\n')
229 # menu '+str(self.nmenues+1)+';\n')
230 xml_file.write('\t\t\t\t\t\tjump title 1;\n')
231 xml_file.write('\t\t\t\t\t}\n')
232 first_entry = False
233
234 xml_file.write('\t\t\t\t</pre>\n')
235 xml_file.write('\t\t\t\t<vob file="')
236
237 xml_file.write(self.expand_xml(menu_page["filename"]))
238 xml_file.write('"></vob>\n')
239
240 for nbutton in menu_page["chapters"]:
241 #xml_file.write('\t\t\t\t<button name="'+nbutton+'"> g0='+str(title_list[button_counter])+'; jump vmgm menu; </button>\n')
242 xml_file.write(
243 '\t\t\t\t<button name="' + nbutton + '">\n')
244 # If PlayAll option is set, the first button on each page
245 # will be the PlayAll button
246 if play_all_opt and button_counter == 0:
247 xml_file.write('\t\t\t\t\tg3=1;\n')
248 xml_file.write('\t\t\t\t\tg0=1;\n')
249 xml_file.write('\t\t\t\t\tjump vmgm menu;\n')
250 else:
251 xml_file.write('\t\t\t\t\tg0={:};\n'.format(str(title_list[title_counter])))
252 xml_file.write('\t\t\t\t\tjump vmgm menu;\n')
253 title_counter += 1
254 button_counter += 1
255 xml_file.write('\t\t\t\t</button>\n')
256
257 if (menu_page["left"] is not None):
258 xml_file.write('\t\t\t\t<button name="' + menu_page["left"] + '"> g1=')
259 xml_file.write(str(menu_number - 1))
260 xml_file.write('; g2=1024; jump menu ')
261 xml_file.write(str(menu_number))
262 xml_file.write('; </button>\n')
263
264 if (menu_page["right"] is not None):
265 xml_file.write('\t\t\t\t<button name="' +
266 menu_page["right"] + '"> g1=')
267 xml_file.write(str(menu_number + 1))
268 xml_file.write('; g2=1024; jump menu ')
269 xml_file.write(str(menu_number + 2))
270 xml_file.write('; </button>\n')
271
272 xml_file.write('\t\t\t\t<post>\n')
273 xml_file.write('\t\t\t\t\tg2=s8;\n')
274 xml_file.write('\t\t\t\t\tg1=' + str(menu_number) + ';\n')
275 xml_file.write('\t\t\t\t\tjump menu ' +
276 str(menu_number + 1) + ';\n')
277 xml_file.write('\t\t\t\t</post>\n')
278 xml_file.write('\t\t\t</pgc>\n')
279 menu_number += 1
280
281 xml_file.write('\t\t</menus>\n')
282 else:
283 xml_file.write('\t\t\t<pgc>\n')
284 xml_file.write('\t\t\t\t<pre>\n')
285 # first we recover the currently selected button
286 xml_file.write('\t\t\t\t\ts8=g2;\n')
287
288 # this code is to fix a bug in some players
289 xml_file.write('\t\t\t\t\tif (g1 eq 100) {\n')
290 # menu '+str(self.nmenues+1)+';\n')
291 xml_file.write('\t\t\t\t\t\tjump title 1;\n')
292 xml_file.write('\t\t\t\t\t}\n')
293
294 xml_file.write('\t\t\t\t</pre>\n')
295 xml_file.write('\t\t\t\t<vob file="')
296 if menuWide:
297 if self.config.PAL:
298 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_pal_wide.mpg"))))
299 else:
300 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_ntsc_wide.mpg"))))
301 else:
302 if self.config.PAL:
303 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_pal.mpg"))))
304 else:
305 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_ntsc.mpg"))))
306 xml_file.write('"></vob>\n')
307
308 xml_file.write('\t\t\t\t<post>\n')
309 xml_file.write('\t\t\t\t\tg2=s8;\n')
310 xml_file.write('\t\t\t\t\tg1=0;\n')
311 xml_file.write('\t\t\t\t\tjump menu 1;\n')
312 xml_file.write('\t\t\t\t</post>\n')
313 xml_file.write('\t\t\t</pgc>\n')
314
315 xml_file.write('\t\t</menus>\n')
316
317 xml_file.write('\t\t<titles>\n')
318 xml_file.write('\t\t\t<video format="')
319 if self.config.PAL:
320 xml_file.write("pal")
321 else:
322 xml_file.write("ntsc")
323 if menuWide:
324 xml_file.write('" aspect="16:9"> ')
325 else:
326 xml_file.write('" aspect="4:3"> ')
327 xml_file.write('</video>\n')
328 xml_file.write('\t\t\t<pgc>\n')
329 xml_file.write('\t\t\t\t<vob file="')
330 if menuWide:
331 if self.config.PAL:
332 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_pal_wide.mpg"))))
333 else:
334 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_ntsc_wide.mpg"))))
335 else:
336 if self.config.PAL:
337 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_pal.mpg"))))
338 else:
339 xml_file.write(self.expand_xml(str(os.path.join(self.config.other_path, "base_ntsc.mpg"))))
340 xml_file.write('"></vob>\n')
341 xml_file.write('\t\t\t\t<post>\n')
342 xml_file.write('\t\t\t\t\tg0=1;\n')
343 xml_file.write('\t\t\t\t\tg1=0;\n')
344 xml_file.write('\t\t\t\t\tg2=1024;\n')
345 xml_file.write('\t\t\t\t\tcall vmgm menu entry title;\n')
346 xml_file.write('\t\t\t\t</post>\n')
347 xml_file.write('\t\t\t</pgc>\n')
348 xml_file.write('\t\t</titles>\n')
349 xml_file.write("\t</titleset>\n")
350
351 xml_file.write("\n")
352
353 # Now, create the titleset for each video
354
355 total_t = len(file_movies)
356 titleset = 1
357 titles = 0
358 counter = 0
359 for element in file_movies:
360 files = 0
361 action = element.actions
362
363 xml_file.write("\n")
364
365 if element.is_mpeg_ps:
366
367 # if it's already an MPEG-2 compliant file, we use the original
368 # values
369 if element.original_fps == 25:
370 pal_ntsc = "pal"
371 ispal = True
372 else:
373 pal_ntsc = "ntsc"
374 ispal = False
375
376 if element.original_aspect_ratio > 1.6:
377 faspect = '16:9'
378 fwide = True
379 else:
380 faspect = '4:3'
381 fwide = False
382
383 else:
384 # but if we are converting it, we use the desired values
385 if element.format_pal:
386 pal_ntsc = "pal"
387 ispal = True
388 else:
389 pal_ntsc = "ntsc"
390 ispal = False
391
392 if element.aspect_ratio_final > 1.6:
393 faspect = '16:9'
394 fwide = True
395 else:
396 faspect = '4:3'
397 fwide = False
398
399 xml_file.write("\t<titleset>\n")
400
401 if not onlyone:
402 xml_file.write("\t\t<menus>\n")
403 xml_file.write('\t\t\t<video format="' +
404 pal_ntsc + '" aspect="' + faspect + '"')
405 if fwide:
406 xml_file.write(' widescreen="nopanscan"')
407 xml_file.write('> </video>\n')
408
409 xml_file.write("\t\t\t<pgc>\n")
410 xml_file.write("\t\t\t\t<pre>\n")
411 xml_file.write('\t\t\t\t\tif (g0 eq 100) {\n')
412 xml_file.write('\t\t\t\t\t\tjump vmgm menu entry title;\n')
413 xml_file.write('\t\t\t\t\t}\n')
414 xml_file.write('\t\t\t\t\tg0=100;\n')
415 xml_file.write('\t\t\t\t\tg1=' +
416 str(int(titles / elements_per_menu)) + ';\n')
417 xml_file.write('\t\t\t\t\tjump title 1;\n')
418 xml_file.write('\t\t\t\t</pre>\n')
419 # fake video to ensure compatibility
420 xml_file.write('\t\t\t\t<vob file="')
421 if ispal:
422 xml_file.write(self.expand_xml(
423 str(os.path.join(self.config.other_path, "base_pal"))))
424 else:
425 xml_file.write(self.expand_xml(
426 str(os.path.join(self.config.other_path, "base_ntsc"))))
427 if fwide:
428 xml_file.write("_wide")
429 xml_file.write('.mpg"></vob>\n')
430 xml_file.write("\t\t\t</pgc>\n")
431 xml_file.write("\t\t</menus>\n")
432
433 xml_file.write("\t\t<titles>\n")
434 xml_file.write('\t\t\t<video format="' +
435 pal_ntsc + '" aspect="' + faspect + '"')
436 if fwide:
437 xml_file.write(' widescreen="nopanscan"')
438 xml_file.write('> </video>\n')
439
440 # subtitles part
441 # for element3 in element2["sub_list"]:
442 # xml_file.write('\t\t\t<subpicture lang="'+self.expand_xml(str(element3["sub_language"][:2].lower()))+'" />\n')
443 xml_file.write('\t\t\t<pgc>\n')
444
445 # if (element2["force_subs"]) and (len(element2["sub_list"])!=0):
446 # xml_file.write('\t\t\t\t<pre>\n')
447 # xml_file.write('\t\t\t\t\tsubtitle=64;\n')
448 # xml_file.write('\t\t\t\t</pre>\n')
449
450 xml_file.write('\t\t\t\t<vob file="' +
451 self.expand_xml(element.converted_filename) + '" ')
452 xml_file.write('chapters="0')
453 if (element.original_length > 5):
454 if (element.divide_in_chapters): # add chapters
455 manual_chapter_list = []
456 if (element.chapter_list_entry):
457 manual_chapter_list = element.chapter_list_entry.split(
458 ",")
459 toadd = element.chapter_size
460 seconds = toadd * 60
461 while seconds < (element.original_length - 4):
462 # Check manual_chapter_list for manual entries before
463 # set next chapter
464 for idx, m_chapter in enumerate(manual_chapter_list):
465 m_chapter = m_chapter.split(":")
466 # Change from mm:ss to seconds
467 m_chapter = int(
468 m_chapter[0]) * 60 + int(m_chapter[1])
469 if (m_chapter < seconds):
470 seconds = m_chapter
471 del manual_chapter_list[idx]
472 thetime = self.return_time(seconds, False)
473 xml_file.write("," + thetime)
474 seconds += (toadd * 60)
475 xml_file.write(
476 ',' + self.return_time((element.original_length - 2), False))
477 xml_file.write('" />\n')
478
479 if not onlyone:
480 xml_file.write('\t\t\t\t<post>\n')
481 files += 1
482 xml_file.write('\t\t\t\t\tg1=' +
483 str(int(titles / elements_per_menu)) + ';\n')
484
485 # play all
486 xml_file.write('\t\t\t\t\tif (g3 eq 1) {\n') # if play all:
487 if titles == total_t - 1: # return to menu if last title
488 xml_file.write('\t\t\t\t\t\tg0=100;\n')
489 else: # play next title
490 xml_file.write('\t\t\t\t\t\tg0=' + str(titles + 2) + ';\n')
491 xml_file.write('\t\t\t\t\t} else {\n')
492
493 # end of play opt
494 xml_file.write('\t\t\t\t\t\tg0=')
495 if action == "action_stop":
496 xml_file.write('100')
497 elif action == "action_play_previous":
498 if titles == 0:
499 prev_t = total_t - 1
500 else:
501 prev_t = titles - 1
502 xml_file.write(str(prev_t + 1))
503 elif action == "action_play_again":
504 xml_file.write(str(titles + 1))
505 elif action == "action_play_next":
506 if titles == total_t - 1:
507 next_t = 0
508 else:
509 next_t = titles + 1
510 xml_file.write(str(next_t + 1))
511 elif action == "action_play_last":
512 xml_file.write(str(total_t))
513 else:
514 xml_file.write('1') # first
515
516 xml_file.write(';\n')
517 xml_file.write('\t\t\t\t\t}\n')
518
519 # preform action
520 xml_file.write('\t\t\t\t\tcall vmgm menu entry title;\n')
521 xml_file.write('\t\t\t\t</post>\n')
522 xml_file.write("\t\t\t</pgc>\n")
523 xml_file.write("\t\t</titles>\n")
524 xml_file.write("\t</titleset>\n")
525 counter += 1
526 titles += 1
527
528 xml_file.write("</dvdauthor>")
529
530 xml_file.close()
531
532 return xml_file_path
533
534 def return_time(self, seconds, empty):
535 """ cuts a time in seconds into seconds, minutes and hours """
536
537 seconds2 = int(seconds)
538
539 hours = str(int(seconds2 / 3600))
540 if empty:
541 if len(hours) == 1:
542 hours = "0" + hours
543 else:
544 if hours == "0":
545 hours = ""
546 if hours != "":
547 hours += ":"
548
549 minutes = str(int((seconds2 / 60) % 60))
550 if empty or (hours != ""):
551 if len(minutes) == 1:
552 minutes = "0" + minutes
553 elif (minutes == "0") and (hours == ""):
554 minutes = ""
555 if minutes != "":
556 minutes += ":"
557
558 secs = str(int(seconds2 % 60))
559 if (len(secs) == 1) and (minutes != ""):
560 secs = "0" + secs
561
562 return hours + minutes + secs