Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 228 lines (184 sloc) 7.266 kb
5748b02 First public version in GITHUB repository
Sergio Costas authored
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3
4
5 # Copyright 2006-2009 (C) Raster Software Vigo (Sergio Costas)
6 # Copyright 2006-2009 (C) Peter Gill - win32 parts
7
8 # This file is part of DeVeDe
9 #
10 # DeVeDe is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # DeVeDe is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23
24 import signal
25 import os
26 import sys
27 import re
28 import shutil
29
30 import devede_executor
31 import devede_other
32
33 class subtitles_adder(devede_executor.executor):
34
35 def expand_xml(self,text):
36
37 text=text.replace('&','&amp;')
38 text=text.replace('<','&lt;')
39 text=text.replace('>','&gt;')
40 text=text.replace('"','&quot;')
41 text=text.replace("'",'&apos;')
42 return text
43
44
45 def __init__(self,videofile,filename,filefolder,progresbar,proglabel,disctype,title,chapter,stream):
46
47 """ This class adds the subtitles to an already converted file
48
49 VIDEOFILE contains the parameters to convert the video
50 FILENAME is the generic file name given by the user
51 FILEFOLDER is the path where all the temporary and finall files will be created
52 PROGRESBAR is the progress bar where the class will show the progress
53 PROGLABEL is the label where the class will show what is it doing
54 DISCTYPE can be dvd, vcd, svcd, cvd or divx
55 TITLE and CHAPTER are the numbers used to identify the TITLE and CHAPTER number for this file
56 STREAMS is the stream number (to allow to add several subtitles)
57 """
58
59 devede_executor.executor.__init__(self,filename,filefolder,progresbar)
60 progresbar.pulse()
61 proglabel.set_text(_("Adding subtitles to")+"\n"+videofile["filename"])
62 self.currentfile=self.create_filename(filefolder+filename,title,chapter,disctype=="divx")
63
64 subtitle_list=videofile["sub_list"][stream]
65
66 # generate the XML file
67
68 self.error=""
69
70 try:
71 print "Trying to create "+filefolder+filename+"_sub.xml"
72 fichero=open(filefolder+filename+"_sub.xml","w")
73 except IOError:
74 print "IOError en subtitulos"
75 self.print_error=_("Failed to write to the destination directory.\nCheck that you have privileges and free space there.")
76 self.initerror=True
77 return
78
10ad85e @rastersoft Añadida comprobacion de MP3LAME
authored
79 fichero.write('<subpictures format="')
80 if (videofile["fps"]==25) :
81 fichero.write('PAL')
82 else:
83 fichero.write('NTSC')
84 fichero.write('">\n\t<stream>')
5748b02 First public version in GITHUB repository
Sergio Costas authored
85 if (subtitle_list["sub_codepage"]!="UTF-8"):
86 final_type="UTF-8"
87 subfilename=os.path.join(filefolder,filename+"_sub_tmp.sub")
88 self.deletesub=subfilename
89 if 0!=devede_other.check_utf().convert_to_UTF8(subtitle_list["subtitles"],subfilename,subtitle_list["sub_codepage"]):
90 #except IOError:
91 print "IOError al convertir a UTF8"
92 self.print_error=_("Failed to write to the destination directory.\nCheck that you have privileges and free space there.")
93 self.initerror=True
94 return
95 else:
96 self.deletesub=""
97 final_type=subtitle_list["sub_codepage"]
98 subfilename=subtitle_list["subtitles"]
99 fichero.write('\n\t\t<textsub filename="'+self.expand_xml(subfilename)+'"')
100
101 if (sys.platform=="win32") or (sys.platform=="win64"):
102 if os.path.isfile(os.path.join(os.environ["WINDIR"],"Fonts","devedesans.ttf")):
103 fichero.write('\n\t\tfont="devedesans.ttf"')
104 else:
105 fichero.write('\n\t\tfont="ARIAL.ttf"')
106 else:
107 fichero.write('\n\t\tfont="devedesans.ttf"')
108 if ((subtitle_list["sub_codepage"]!="") and (subtitle_list["sub_codepage"]!="ASCII")):
109 fichero.write('\n\t\tcharacterset="'+final_type+'"')
110 fichero.write('\n\t\thorizontal-alignment="center"')
111
112 if (videofile["fps"]==25):
113 ancho=716
114 alto=572
115 tamanofont=videofile["subfont_size"]
116 else:
117 ancho=716
118 alto=476
119 tamanofont=videofile["subfont_size"]
120
121 margin_hor=int((58*ancho)/720)
122 margin_vert=int((28*alto)/576)
123 bottom_margin=margin_vert
124
125 fichero.write('\n\t\tmovie-width="'+str(ancho-4)+'"')
126 fichero.write('\n\t\tmovie-height="'+str(alto-4)+'"')
127 fichero.write('\n\t\tleft-margin="'+str(margin_hor)+'"')
128 fichero.write('\n\t\tright-margin="'+str(margin_hor)+'"')
129
130 if subtitle_list["subtitles_up"]:
131 tamanofont-=1
132 bottom_margin=4+(alto/8) # put it in the border of 16:9 aspect ratio
133
134 fichero.write('\n\t\tbottom-margin="'+str(bottom_margin)+'"')
135 fichero.write('\n\t\ttop-margin="'+str(margin_vert)+'"')
136
137 fichero.write('\n\t\tfontsize="'+str(tamanofont)+'.0"')
138
0d14d49 @nott Let set subtitles color
nott authored
139 fill_color = 'rgba(%.0f%%, %.0f%%, %.0f%%, %.0f%%)' % tuple(
140 float(item)/655.35 for item in videofile["sub_fill_color"]
141 )
142 fichero.write('\n\t\tfill-color="%s"' % fill_color)
143
144 outline_color = 'rgba(%.0f%%, %.0f%%, %.0f%%, %.0f%%)' % tuple(
145 float(item)/655.35 for item in videofile["sub_outline_color"]
146 )
147 fichero.write('\n\t\toutline-color="%s"' % outline_color)
148
5748b02 First public version in GITHUB repository
Sergio Costas authored
149 if (videofile["fps"]==30):
150 if (videofile["ofps"]==24) and ((disctype=="dvd") or (disctype=="divx")):
151 fps_out="24000/1001"
152 else:
153 fps_out="30000/1001"
154 else:
155 fps_out="25"
156
157 if videofile["ismpeg"]:
158 fps_out=videofile["ofps2"]
159 print "FPS sub 1 original"
160 else:
161 print "FPS sub 1 final"
162
163 fichero.write('\n\t\tmovie-fps="'+str(fps_out)+'"')
164 speed1,speed2=devede_other.get_speedup(videofile)
165 if speed1==speed2:
166 fps_out_subs=fps_out
167 print "FPS sub 2 final"
168 else:
169 if speed2==24:
170 fps_out_subs="24"
171 else:
172 fps_out_subs=videofile["ofps2"]
173 print "FPS sub 2 original"
174
175 fichero.write('\n\t\tsubtitle-fps="'+fps_out_subs+'"')
176 fichero.write('\n\t\tvertical-alignment="bottom" />')
177 fichero.write("\n\t</stream>\n</subpictures>")
178 fichero.close()
179
180 comando=""
181 if (sys.platform=="win32") or (sys.platform=="win64"):
182 comando=["spumux.exe"]
183 comando.append("-m")
184 if disctype=="vcd":
185 comando.append("svcd")
186 else:
187 comando.append(disctype)
188 comando.append("-s")
189 comando.append(str(stream))
190
191 comando.append(filefolder+filename+"_sub.xml")
192 comando.append("-i")
193 comando.append(self.currentfile)
194 comando.append("-o")
195 comando.append(self.currentfile+".sub")
196 self.print_error=_("Conversion failed.\nIt seems a bug of SPUMUX.")
197 self.launch_program(comando,output=True)
198 else:
199 comando="spumux -m "
200 if disctype=="vcd":
201 comando+="svcd"
202 else:
203 comando+=disctype
204
205 comando+=' -s '+str(stream)+' "'+filefolder+filename+'_sub.xml"'
206
207 self.print_error=_("Conversion failed.\nIt seems a bug of SPUMUX.")
208 self.launch_shell(comando,output=True,stdinout=[self.currentfile,self.currentfile+".sub"])
209
210
211 def end_process(self,eraser,erase_temporal_files):
212
213 shutil.move(self.currentfile+".sub", self.currentfile)
214 if erase_temporal_files:
215 eraser.delete_sub_xml(self.deletesub)
216
217
218
219 def set_progress_bar(self):
220
221 self.bar.pulse()
222 position=self.cadena.find("STAT: ")
223 if (position!=-1):
224 position2=self.cadena.find(".",position+6)
225 if position2!=-1:
226 self.bar.set_text(self.cadena[position:position2])
227 return True
Something went wrong with that request. Please try again.