Skip to content

Commit 1cb8421

Browse files
committed
expose exceptions
1 parent a0e0e4f commit 1cb8421

File tree

2 files changed

+91
-85
lines changed

2 files changed

+91
-85
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
SimpleSubtitleEditor
2-
====================
1+
# SimpleSubtitleEditor
32

4-
SimpleSubtitleEditor for Blender
3+
An Blender Addon for editing subtitles.
54

6-
Usage:
5+
## Features
6+
- Adding/deleting lines of subtitle.
7+
- Adjusting each lines with markers in the time line.
8+
- Importing/exporting: SRT(SubRip) SUB(SubViewer).
79

10+
##Usage
11+
12+
When activated, your should find an UI in Video Sequencer Editor > Properties > SimpleSubtitleEditor.
13+
14+
### From Scratch
15+
Just press the 'new' button and there will be two markers added, marking the start and end frame of that subtitle line.
16+
17+
NOTE: Don't delete and insert markers manually. Use the buttons instead. There are background property storing info and manually adding or deleting markers will break its consistency.
18+
19+
### Import
20+
Fill in the first line editor with the file path, then click the tick aside.
21+
22+
Then markers' position can be tweaked. Subtitles context can be changed or deleted.
23+
24+
Only file of format SRT or SUB is supported.
25+
26+
### Export
27+
Fill in the last line editor with the file path, then click the tick aside.
28+
29+
Only file of format SRT or SUB is supported.
30+
31+
## Known Problems
32+
- Stores only timecode info and titling info, no styling info or metadata.
33+
- Only appending and deleting is supported (So that is why it's called "simple", for few people would use inserting).
34+
- Encoding is based on your system's locale.
35+
36+
## Future Plan
37+
Interface refraction? The current interface is slow for a large file.

SimpleSubtitleEditor.py

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
bl_info = {
2222
"name": "Simple Subtitle Editor",
2323
"author": "James Ruan",
24-
"version": (0, 2, 1),
25-
"blender": (2, 72, 0),
24+
"version": (0, 2, 2),
25+
"blender": (2, 79, 0),
2626
"api": 40779,
2727
"location": "VSE > Properties > Simple Subtitle Editor",
2828
"description": "Simple subtitle editor",
2929
"warning": "Format guess is based on filename extension.",
3030
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Sequencer/SimpleSubtitleEditor",
31-
"tracker_url": "https://developer.blender.org/T28810",
31+
"tracker_url": "https://github.com/jamesruan/SimpleSubtitleEditor/issues",
3232
"category": "Sequencer"}
3333

3434
import bpy
@@ -68,16 +68,15 @@ def frameno2timecode(frameno, fps, format="srt"):
6868

6969

7070
class MarkerExportOperator(bpy.types.Operator):
71+
"""Export markers to file"""
7172
bl_idname = "marker.sse_export"
7273
bl_label = "Export markers to file"
7374

7475
def execute(self, context):
7576
scene = context.scene
7677
m = []
77-
scene.sse_msg = ""
7878
if not scene.sse_outfile:
79-
scene.sse_msg = "You must select or name a file to write to."
80-
return
79+
raise Exception("You must select or name a file to write to.")
8180
ext = scene.sse_outfile.rsplit('.')[-1]
8281
if ext == "srt" or ext == "SRT":
8382
format = "srt"
@@ -86,35 +85,29 @@ def execute(self, context):
8685
else:
8786
scene.sse_outfile += ".srt"
8887
format = "srt"
89-
scene.sse_msg = "Output to file with format: %s"%(format)
9088

9189
if format == "srt":
92-
try:
93-
f = open(scene.sse_outfile, mode='wt')
94-
fps = scene.render.fps
95-
for n in scene.sse_sublist:
96-
s = scene.timeline_markers["S%03d"%(n.index)].frame
97-
e = scene.timeline_markers["E%03d"%(n.index)].frame
98-
s = frameno2timecode(s, fps, "srt")
99-
e = frameno2timecode(e, fps, "srt")
100-
l = "%d\n%s --> %s\n%s\n"%(n.index, s, e, n.text.replace('\\n','\n'))
101-
m.append(l)
102-
f.writelines("\n".join(m))
103-
except IOError:
104-
return {'FINISHED'}
105-
90+
f = open(scene.sse_outfile, mode='wt')
91+
fps = scene.render.fps
92+
for n in scene.sse_sublist:
93+
s = scene.timeline_markers["S%03d"%(n.index)].frame
94+
e = scene.timeline_markers["E%03d"%(n.index)].frame
95+
s = frameno2timecode(s, fps, "srt")
96+
e = frameno2timecode(e, fps, "srt")
97+
l = "%d\n%s --> %s\n%s\n"%(n.index, s, e, n.text.replace('\\n','\n'))
98+
m.append(l)
99+
f.writelines("\n".join(m))
106100
elif format == "sub":
107-
try:
108-
f = open(scene.sse_outfile, mode='wt')
109-
fps = scene.render.fps
110-
for n in scene.sse_sublist:
111-
s = scene.timeline_markers["S%03d"%(n.index)].frame
112-
e = scene.timeline_markers["E%03d"%(n.index)].frame
113-
s = frameno2timecode(s, fps, "sub")
114-
e = frameno2timecode(e, fps, "sub")
115-
l = "%s,%s\n%s\n"%(s, e, n.text.replace('\\n','[br]'))
116-
m.append(l)
117-
header="""[INFORMATION]
101+
f = open(scene.sse_outfile, mode='wt')
102+
fps = scene.render.fps
103+
for n in scene.sse_sublist:
104+
s = scene.timeline_markers["S%03d"%(n.index)].frame
105+
e = scene.timeline_markers["E%03d"%(n.index)].frame
106+
s = frameno2timecode(s, fps, "sub")
107+
e = frameno2timecode(e, fps, "sub")
108+
l = "%s,%s\n%s\n"%(s, e, n.text.replace('\\n','[br]'))
109+
m.append(l)
110+
header="""[INFORMATION]
118111
[TITLE]
119112
[AUTHOR]
120113
[SOURCE]
@@ -124,10 +117,8 @@ def execute(self, context):
124117
[END INFORMATION]
125118
[SUBTITLE]
126119
"""
127-
f.write(header)
128-
f.writelines("\n".join(m))
129-
except IOError:
130-
return {'FINISHED'}
120+
f.write(header)
121+
f.writelines("\n".join(m))
131122
# print(m)
132123
return {'FINISHED'}
133124

@@ -137,7 +128,6 @@ class MarkerImportOperator(bpy.types.Operator):
137128

138129
def execute(self,context):
139130
scene = context.scene
140-
scene.sse_msg = ""
141131
for i in range(0, len(scene.sse_sublist)):
142132
scene.sse_sublist.remove(0)
143133

@@ -146,55 +136,47 @@ def execute(self,context):
146136
m = []
147137

148138
if not scene.sse_infile:
149-
scene.sse_msg = "You must select a file to open."
139+
raise Exception("You must select a file to open.")
150140
return
151141
ext = scene.sse_infile.rsplit('.')[-1]
152142
if ext == "srt" or ext == "SRT":
153143
format = "srt"
154144
elif ext == "sub" or ext =="SUB":
155145
format = "sub"
156146
else:
157-
scene.sse_msg = "Can not open file of format: %s"%(ext)
147+
raise Exception("Can not open file of format: %s"%(ext))
158148
return
159149

160150
if format == "srt":
161-
try:
162-
f = open(scene.sse_infile)
163-
all = "".join(f.readlines()).replace('\n\n',"\n#SEP#").split('#SEP#')
164-
all = [x.strip('\n').splitlines() for x in all]
165-
all = [x for x in all if x != []]
166-
for i in all:
167-
n = {}
168-
n['i'] = int(i[0])
169-
t = i[1].split('-->')
170-
n['s'] = t[0].strip()
171-
n['e'] = t[1].strip()
172-
n['t'] = '\\n'.join(i[2:])
173-
m.append(n)
174-
f.close()
175-
except IOError:
176-
print('IOError')
177-
return {'FINISHED'}
151+
f = open(scene.sse_infile)
152+
all = "".join(f.readlines()).replace('\n\n',"\n#SEP#").split('#SEP#')
153+
all = [x.strip('\n').splitlines() for x in all]
154+
all = [x for x in all if x != []]
155+
for i in all:
156+
n = {}
157+
n['i'] = int(i[0])
158+
t = i[1].split('-->')
159+
n['s'] = t[0].strip()
160+
n['e'] = t[1].strip()
161+
n['t'] = '\\n'.join(i[2:])
162+
m.append(n)
163+
f.close()
178164
elif format == "sub":
179-
try:
180-
f = open(scene.sse_infile)
181-
#skip all INFORMATION
182-
all = "".join(f.readlines()).rsplit('[SUBTITLE]\n')[-1].replace('\n\n',"\n#SEP#").split('#SEP#')
183-
all = [x.strip('\n').splitlines() for x in all]
184-
all = [x for x in all if x != []]
185-
print(all)
186-
for k in range(1, len(all)+1):
187-
n = {}
188-
n['i'] = k
189-
t = all[k-1][0].split(',')
190-
n['s'] = t[0].strip()
191-
n['e'] = t[1].strip()
192-
n['t'] = '[br]'.join(all[k-1][1:])
193-
m.append(n)
194-
f.close()
195-
except IOError:
196-
print('IOError')
197-
return {'FINISHED'}
165+
f = open(scene.sse_infile)
166+
#skip all INFORMATION
167+
all = "".join(f.readlines()).rsplit('[SUBTITLE]\n')[-1].replace('\n\n',"\n#SEP#").split('#SEP#')
168+
all = [x.strip('\n').splitlines() for x in all]
169+
all = [x for x in all if x != []]
170+
print(all)
171+
for k in range(1, len(all)+1):
172+
n = {}
173+
n['i'] = k
174+
t = all[k-1][0].split(',')
175+
n['s'] = t[0].strip()
176+
n['e'] = t[1].strip()
177+
n['t'] = '[br]'.join(all[k-1][1:])
178+
m.append(n)
179+
f.close()
198180
#print(m)
199181
fps = scene.render.fps
200182

@@ -304,14 +286,10 @@ def draw(self, context):
304286
a = col.operator("marker.sse_add",text="new", icon='ZOOMIN')
305287
a.name = "%03d"%(len(context.scene.sse_sublist)+1)
306288
row = col.row()
307-
row.prop(scene, "sse_msg", text="")
308-
row = col.row()
309289
row.label("Output:")
310290
row.prop(scene, "sse_outfile", text="")
311291
row.operator("marker.sse_export",text="", icon='FILE_TICK')
312292

313-
314-
315293
def register():
316294
bpy.utils.register_class(MarkerImportOperator)
317295
bpy.utils.register_class(MarkerExportOperator)
@@ -322,7 +300,6 @@ def register():
322300
bpy.utils.register_class(SSEPanel)
323301
setattr(bpy.types.Scene, "sse_infile", bpy.props.StringProperty(name="sse_infile", subtype='FILE_PATH', description="filename to import from"))
324302
setattr(bpy.types.Scene, "sse_outfile", bpy.props.StringProperty(name="sse_outfile", subtype='FILE_PATH', description="filename to export into"))
325-
setattr(bpy.types.Scene, "sse_msg", bpy.props.StringProperty(name="sse_msg", subtype='NONE', description="Messages"))
326303
setattr(bpy.types.Scene, "sse_sublist", bpy.props.CollectionProperty(type=SSE_Sublist))
327304

328305
def unregister():
@@ -335,7 +312,6 @@ def unregister():
335312
bpy.utils.unregister_class(SSEPanel)
336313
delattr(bpy.types.Scene, "sse_infile")
337314
delattr(bpy.types.Scene, "sse_outfile")
338-
delattr(bpy.types.Scene, "sse_msg")
339315
delattr(bpy.types.Scene, "sse_sublist")
340316

341317
if __name__ == '__main__':

0 commit comments

Comments
 (0)