-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdevede_other.py
executable file
·718 lines (564 loc) · 16.7 KB
/
devede_other.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# Copyright 2006-2009 (C) Raster Software Vigo (Sergio Costas)
# Copyright 2006-2009 (C) Peter Gill - win32 parts
# This file is part of DeVeDe
#
# DeVeDe is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DeVeDe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###########################################################################
# This block contains generic help functions that are used in the program #
###########################################################################
import os
import subprocess
import stat
import sys
import shutil
import cairo
import gtk
import struct
import devede_executor
class check_utf(devede_executor.executor):
""" This class allows to detect if a file is pure ASCII, UTF-8 or UTF-16, and allows
to convert from UTF-16 to UFT-8 """
def check_ascii(self,filename):
try:
file=open(filename,"rb")
except:
return False
while True:
element=file.read(1)
if len(element)==0:
return True
if ord(element)>127:
return False
def read_utf8_element(self,file,base_value,nb):
value=base_value
for counter in range(nb-1):
element=file.read(1)
if element=="":
return False
if (ord(element)>191) or (ord(element)<128):
return False
value*=64 # rotate value 6 bits to the left
value+=ord(element)-128
return value
def check_utf8(self,filename):
try:
file=open(filename,"rb")
except:
return False
while True:
el=file.read(1)
if el=="":
file.close()
return True
element=ord(el)
if element<128:
continue
if element<194: # it's a mid-sequence element or a 2-byte start sequence for an element smaller than 128
print "Elemento 1"
file.close()
return False # invalid element found
if element<224: # two-byte element
value=self.read_utf8_element(file,element-192, 2)
if value==False:
print "Elemento 2"
file.close()
return False # invalid sequence
if value<128:
print "Elemento 3"
file.close()
return False # invalid value for a 2-byte sequence
continue
if element<240: # three-byte element
value=self.read_utf8_element(file,element-224, 3)
if value==False:
print "Elemento 4"
file.close()
return False # invalid sequence
if value<2048:
print "Elemento 5"
file.close()
return False # invalid value for a 3-byte sequence
continue
if element<245: # four-byte element
value=self.read_utf8_element(file,element-240, 4)
if value==False:
print "Elemento 6"
file.close()
return False # invalid sequence
if value<65536:
print "Elemento 7"
file.close()
return False # invalid value for a 4-byte sequence
continue
file.close()
return False # invalid value
def read_16(self,file,format_b):
element=file.read(2)
if element=="":
return ""
if len(element)!=2:
return False
value=struct.unpack(format_b,element)[0]
return value
def read_full16(self,file,format_b):
value=self.read_16(file, format_b)
if value=="":
return True # end of file
if value==False:
return False
if (value>=56320) and (value<=57343): # invalid value
return False
if (value>55295) and (value<56320): # surrogate pair
value-=55296
value2=self.read_16(file, format_b)
if (value2==False) or (value2==""):
return False
if (value2<56320) or (value2>57343): # invalid surrogate pair
return False
value2-=56320
return ((value*1024+value2)+65536)
else:
return value
def write_utf8(self,file,value):
if value<128:
file.write(struct.pack("B",value))
elif value<2048:
v1=(value/64) & 31
v2= value & 63
file.write(struct.pack("BB",v1+192,v2+128))
elif value<65536:
v1=(value/4096) & 15
v2=(value/64) & 63
v3= value & 63
file.write(struct.pack("BBB",v1+224,v2+128,v3+128))
else:
v1=(value/262143) & 7
v2=(value/4096) & 63
v3=(value/64) & 63
v4= value & 63
file.write(struct.pack("BBBB",v1+240,v2+128,v3+128,v4+128))
def check_utf16BE(self,filename):
try:
file=open(filename,"rb")
except:
return False
value=self.read_16(file, ">H")
file.close()
if value==65279:
return True
return False
def check_utf16LE(self,filename):
try:
file=open(filename,"rb")
except:
return False
value=self.read_16(file, "<H")
file.close()
if value==65279:
return True
return False
def check_utf16(self,filename,big_endian):
try:
file=open(filename,"rb")
except:
return False
if big_endian:
format_b=">H"
else:
format_b="<H"
while True:
value=self.read_full16(file, format_b)
if value==True:
print "True"
return True
if value==False:
print "False"
return False
# value can be an integer too, that's why I put both IFs
def convert_to_UTF8(self,infile_n,outfile_n,origin_format):
command_line='iconv -f '+str(origin_format)+' -t UTF-8 "'+str(infile_n)+'" > "'+str(outfile_n)+'"'
return self.launch_shell(command_line).wait()
def convert_16_to_8(self,infile_n,outfile_n):
if self.check_utf16BE(infile_n):
format_b=">H"
else:
format_b="<H"
infile=open(infile_n,"rb")
outfile=open(outfile_n,"wb")
first=True
while True:
value=self.read_full16(infile, format_b)
if (value==65279) and first:
first=False
continue
first=False
if (value==True) or (value==False):
break
self.write_utf8(outfile, value)
infile.close()
outfile.close()
return value
def do_check(self,filename):
""" Checks if a file is pure ASCII, UTF-8, UTF-16 or Unknown """
self.type="Unknown"
if self.check_ascii(filename):
self.type="ascii"
return self.type
if self.check_utf8(filename):
self.type="utf8"
return self.type
if (self.check_utf16BE(filename)):
if (self.check_utf16(filename,True)):
self.type="utf16"
return self.type
if (self.check_utf16LE(filename)):
if (self.check_utf16(filename,False)):
self.type="utf16"
return self.type
return self.type
def get_speedup(videofile):
""" configure speedup values when the original file
is 23.976 or 24 fps and final file is 25 fps (PAL)
this allows to avoid jerky jumps during reproduction """
# First, check if the original is XX.9XX or XX.000 fps
rational_fps=False
pos=videofile["ofps2"].find(".")
if (pos!=-1) and (len(videofile["ofps2"])>(pos+1)):
if (videofile["ofps2"][pos+1]=="9"):
rational_fps=True
# if it's rational, we have to use a different pair of values to achieve good precission
if (videofile["ofps"]==24) and (videofile["fps"]==25) and (videofile["copy_audio"]==False) and (videofile["ismpeg"]==False):
if rational_fps:
return 25025,24000
else:
return 25,24
else:
return 1,1
def get_font_params(font_name):
font_elements=[]
font_temp=font_name
font_elements=font_name.split(" ")
if (len(font_elements))<2:
fontname="Sans"
fontstyle=cairo.FONT_WEIGHT_NORMAL
fontslant=cairo.FONT_SLANT_NORMAL
fontsize=12
else:
fontname=""
fontstyle=cairo.FONT_WEIGHT_NORMAL
fontslant=cairo.FONT_SLANT_NORMAL
for counter2 in range(len(font_elements)-1):
if font_elements[counter2]=="Bold":
fontstyle=cairo.FONT_WEIGHT_BOLD
elif font_elements[counter2]=="Italic":
fontslant=cairo.FONT_SLANT_ITALIC
else:
fontname+=" "+font_elements[counter2]
if fontname!="":
fontname=fontname[1:]
else:
fontname="Sans"
try:
fontsize=float(font_elements[-1])
except:
fontsize=12
return fontname,fontstyle,fontslant,fontsize
def calcula_tamano_parcial(vrate,arate,filesize,length,subs,ismpeg,isvob,cutting,speed1,speed2):
""" Calculates the estimated final size.
VRATE and ARATE is the bit rate for video and audio.
FILESIZE is the size of the original file.
LENGTH is the file length in seconds
SUBS is the number of subs
ISMPEG is true if the file is already an MPEG-compliant file
CUTTING is different than 0 if we are cutting the file in half.
"""
if (speed1!=speed2): # we are speeding up the film, so we must take it into account
length=int((float(length))*((float(speed2))/(float(speed1))))
if ismpeg or isvob:
l=filesize/1000
else:
l=float(((vrate+arate)*length)/8)
if cutting!=0:
l/=2
l+=float((8*subs*length)/8) # add the subtitles (assume 8kbit/sec for each one)
return l
def calcule_menu_size(structure,sound_duration):
# each menu needs 2692 kbits/sec * sound_duration / 8
return (337*sound_duration)*((len(structure)+9)/10)
def calcula_tamano_total(structure,sound_duration,disktype):
""" Calculates the total size of the DVD """
print "Calculating size for disk :"+str(disktype)
total=0.0
for element in structure:
if len(element)>1:
for film in element[1:]:
speed1,speed2=get_speedup(film)
total+=calcula_tamano_parcial(film["vrate"],film["arate"],film["filesize"],film["olength"],len(film["sub_list"]),film["ismpeg"],film["isvob"],film["cutting"],speed1,speed2)
if disktype=="dvd":
total+=calcule_menu_size(structure,sound_duration)
return total
def check_program(programa):
""" This function allows to check that a program is available in the system, just
by calling it without arguments and checking the error returned """
if (sys.platform=="win32") or (sys.platform=="win64"):
launcher=devede_executor.executor()
p=launcher.launch_program(programa,win32arg=False)
else:
p=subprocess.Popen(programa+" >/dev/null 2>/dev/null",shell=True)
p.wait()
return p.returncode
def to_bool(value):
""" Converts string to bool """
if str(value).lower() in ("true",1,"pal"): return True
if str(value).lower() in ("false",0,"ntsc"): return False
raise Exception('Invalid value for boolean conversion: ' + str(value))
def load_config(global_vars):
""" Load the configuration """
home=get_home_directory()
# TODO change to allow a windows temp directory
if (sys.platform=="win32") or (sys.platform=="win64"):
#global_vars["temp_folder"]=os.environ["TEMP"]
global_vars["temp_folder"]=os.path.join(home,"Local Settings", "Temp")
else:
global_vars["temp_folder"]="/var/tmp"
print "Temp Directory is: " , global_vars["temp_folder"]
if (sys.platform=="win32") or (sys.platform=="win64"):
home=os.path.join(home,"Application Data", "devede","devede.conf")
else:
home+=".devede"
print "home load: ", home
menuformat_found=False
try:
archivo=open(home,"r")
while True:
linea=archivo.readline()
print "linea: ", linea
if linea=="":
break
if linea[-1]=="\n":
linea=linea[:-1]
colon=linea.index(':') # find colon location
key=linea[:colon] # get key
value=linea[(colon+1):] # get value
#special cases
if key=="use_ffmpeg_menu":
if value=="1":
value="ffmpeg"
else:
value="mencoder"
if key=="use_ffmpeg":
if value=="1":
value="ffmpeg"
else:
value="mencoder"
if key=="multicore":
if not value=="1": value=global_vars["cores"]
if key=="AC3_fix":
value=to_bool(value)
if key=="AC3_fix_ffmpeg":
value=to_bool(value)
if key=="AC3_fix_avconv":
value=to_bool(value)
if key=="erase_tmp_files":
value=to_bool(value)
if key=="video_format":
value=to_bool(value)
if key=="hyperthreading":
value=to_bool(value)
if key=="with_menu":
value=to_bool(value)
if key=="shutdown_after_disc":
value=to_bool(value)
if key=="erase_files":
value=to_bool(value)
if key=="PAL":
value=to_bool(value)
if key=="menu_widescreen":
value=to_bool(value)
if key=="file_twopass":
value=to_bool(value)
if key=="file_turbo1stpass":
value=to_bool(value)
if key=="file_sound51":
value=to_bool(value)
if key=="file_volume":
value=float(value)
if key=="file_lchapters":
value=float(value)
global_vars[key]=value # save
archivo.close()
except IOError:
pass
def save_config(global_vars):
""" Stores the configuration """
home=get_home_directory()
if (sys.platform=="win32") or (sys.platform=="win64"):
home=os.path.join(home,"Application Data", "devede")
if not os.path.isdir(home):
os.mkdir(home)
home=os.path.join(home, "devede.conf")
else:
home+=".devede"
if global_vars["temp_folder"][-1]!=os.sep:
global_vars["temp_folder"]+=os.sep
try:
allowed=["PAL","erase_files","finalfolder","sub_codepage","sub_language","with_menu","AC3_fix","AC3_fix_ffmpeg","AC3_fix_avconv","encoder_video","encoder_menu","shutdown_after_disc","multicore","hyperthreading","menu_top_margin","menu_bottom_margin","menu_left_margin","menu_right_margin", "menu_widescreen","file_blackbars","file_lchapters","file_sound51","file_twopass","file_turbo1stpass","file_volume"]
archivo=open(home,"w")
for option, value in global_vars.items():
if option in allowed: archivo.write(option+":"+str(value)+"\n")
archivo.close()
except IOError:
print "Error when writting configuration"
pass
def get_new_param(parameters):
""" This function groups the parameters passed by the user into a list """
new_param=""
while(True):
if (parameters.find(" ")==0):
parameters=parameters[1:] # erase blank spaces at start
else:
break
if len(parameters)==0:
return "",""
p0=0
while True:
p1=parameters.find('\\',p0)
p2=parameters.find(' ',p0)
if p2==p1+1:
p0=p2+1
else:
if p2<0: # no next space, take all the string
retorno=""
doble=False
print parameters
for letra in parameters:
if (letra!='\\') or doble:
retorno+=letra
doble=False
else:
doble=True
return "",retorno
else:
retorno=""
doble=False
print parameters[:p2]
for letra in parameters[:p2]:
if (letra!='\\') or doble:
retorno+=letra
doble=False
else:
doble=True
return parameters[p2+1:],retorno
def get_home_directory():
if (sys.platform=="win32") or (sys.platform=="win64"):
home=os.environ["USERPROFILE"]
else:
home=os.environ.get("HOME")
if home[-1]!=os.sep:
home=home+os.sep
print home
return home
def return_time(seconds,empty):
""" cuts a time in seconds into seconds, minutes and hours """
seconds2=int(seconds)
hours=str(seconds2/3600)
if empty:
if len(hours)==1:
hours="0"+hours
else:
if hours=="0":
hours=""
if hours!="":
hours+=":"
minutes=str((seconds2/60)%60)
if empty or (hours!=""):
if len(minutes)==1:
minutes="0"+minutes
elif (minutes=="0") and (hours==""):
minutes=""
if minutes!="":
minutes+=":"
secs=str(seconds2%60)
if (len(secs)==1) and (minutes!=""):
secs="0"+secs
return hours+minutes+secs
def get_max_titles(disctocreate):
""" Returns the maximum number of titles/chapters for each type of disc """
if disctocreate=="dvd":
return 61
else:
return 99
def get_dvd_size(tree,disctocreate):
""" Returns the size for the currently selected disk type, and the minimum and maximum
videorate for the current video disk """
if tree!=None:
w=tree.get_object("dvdsize")
active=w.get_active()
# here we choose the size in Mbytes for the media
if 0==active:
tamano=170.0
elif 1==active:
tamano=700.0
elif 2==active:
tamano=750.0
elif 3==active:
tamano=1100.0
elif 4==active:
tamano=4200.0
else:
tamano=8000.0
else:
tamano=0
if disctocreate=="vcd":
minvrate=1152
maxvrate=1152
elif (disctocreate=="svcd") or (disctocreate=="cvd"):
minvrate=400
maxvrate=2300
elif (disctocreate=="dvd"):
minvrate=400
maxvrate=8500
elif (disctocreate=="divx"):
minvrate=300
maxvrate=6000
tamano*=0.92 # a safe margin of 8% to ensure that it never will be bigger
# (it's important to have in mind the space needed by disk structures like
# directories, file entries, and so on)
return tamano,minvrate,maxvrate
def get_picture_type(filename):
try:
f=open(filename,"r")
except:
return ""
ftype=""
line=f.read(4)
if (line[0]=="\211") and (line[1:]=="PNG"):
ftype="png"
line2=f.read(7)
if (line[0]=="\377") and (line[1]=="\330") and (line[2]=="\377") and (line[3]=="\340") and (line2[2:6]=="JFIF") and (line2[6]=="\000"):
ftype="jpeg"
f.close()
print "tipo: "+str(ftype)
return ftype
def create_tree(sobject,filename,builderpath="",autoconnect=True):
tree=gtk.Builder()
tree.set_translation_domain("devede")
filename=os.path.join(builderpath,filename+".ui")
print "Creating window "+filename
tree.add_from_file(filename)
if autoconnect:
tree.connect_signals(sobject)
return tree