-
Notifications
You must be signed in to change notification settings - Fork 4
/
proxy_tools.py
333 lines (252 loc) · 10.3 KB
/
proxy_tools.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
import bpy, os
from bpy.props import IntProperty, StringProperty, BoolProperty
import subprocess
from . import functions
proxy_qualities = [ ( "1", "25%", "" ), ( "2", "50%", "" ),
( "3", "75%", "" ), ( "4", "100%", "" ),
( "5", "none", "" )]
# functions
def setup_proxy(context, strip, size):
preferences = context.user_preferences
prefs = preferences.addons[__package__].preferences
# set up proxy settings
strip.use_proxy = True
if prefs.use_bi_custom_directory:
strip.use_proxy_custom_directory = True
filename = strip.filepath.rpartition("/")[2].rpartition(".")[0]
strip.proxy.directory = bpy.path.relpath(prefs.proxy_dir+filename)
else:
strip.use_proxy_custom_directory = False
if strip.use_proxy_custom_file == True:
strip.use_proxy_custom_file = False
strip.proxy.quality = prefs.quality
strip.proxy.timecode = prefs.timecode
if size == 5:
strip.use_proxy = False
strip.proxy.build_25 = False
strip.proxy.build_50 = False
strip.proxy.build_75 = False
strip.proxy.build_100 = False
else:
proxysuffix = proxy_qualities[size-1][1].split("%")[0]
if (proxysuffix == "25"):
strip.proxy.build_25 = True
if (proxysuffix == "50"):
strip.proxy.build_50 = True
if (proxysuffix == "75"):
strip.proxy.build_75 = True
if (proxysuffix == "100"):
strip.proxy.build_100 = True
return {"FINISHED"}
def create_proxy(context, strip, size, res):
# calculate proxy resolution
div = 4/size
newres = (int(int(res[0])/div), int(int(res[1])/div))
preferences = context.user_preferences
proxy_dir = preferences.addons[__package__].preferences.proxy_dir
scripts = preferences.addons[__package__].preferences.proxy_scripts
ffmpeg_command = preferences.addons[__package__].preferences.ffmpeg_command
functions.create_folder(proxy_dir)
if scripts:
commands = []
# get filename
if strip.type == "MOVIE":
filename = bpy.path.abspath(strip.filepath)
proxysuffix = proxy_qualities[size-1][1].split("%")[0]
proxy_dir = bpy.path.abspath(proxy_dir)
newfilename = os.path.join(proxy_dir,filename.rpartition("/")[2])
fileoutput = newfilename.rpartition(".")[0]+"-"+proxysuffix+".avi"
#default value for ffmpeg_command = "fmpeg -i {} -vcodec mjpeg -qv 1 -s {}x{} -y {}"
command = ffmpeg_command.format(filename, newres[0], newres[1], fileoutput)
print(command)
if scripts:
commands.append(command)
else:
# check for existing file
if not os.path.isfile(fileoutput):
subprocess.call(command, shell=True)
else:
print("ya existe")
# set up proxy settings
strip.use_proxy = True
strip.use_proxy_custom_file = True
strip.proxy.filepath = bpy.path.relpath(fileoutput)
if (proxysuffix == "25"):
strip.proxy.build_25 = True
if (proxysuffix == "50"):
strip.proxy.build_50 = True
if (proxysuffix == "75"):
strip.proxy.build_75 = True
if (proxysuffix == "100"):
strip.proxy.build_100 = True
if scripts:
return commands
else:
return None
def create_proxy_scripts(scripts_dir, commands, strip_name = None):
functions.create_folder(bpy.path.abspath(scripts_dir))
for i in commands:
#print(i)
filename = "{}/proxy_script_{}.sh".format(scripts_dir, strip_name)
text_file = open(bpy.path.abspath(filename), "w")
#print(filename)
text_file.write(i)
text_file.close()
# classes
class CreateProxyOperator(bpy.types.Operator):
""" Use ffmpeg to create a proxy from video and setup proxies \
for selected strip"""
bl_idname = "sequencer.create_proxy_operator"
bl_label = " Create proxy"
size = IntProperty(
name='proxysize',
default=1)
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(self, context):
strip = functions.act_strip(context)
scn = context.scene
if scn and scn.sequence_editor and scn.sequence_editor.active_strip:
return strip.type in ('MOVIE')
else:
return False
def execute(self, context):
preferences = context.user_preferences
proxy_dir = preferences.addons[__package__].preferences.proxy_dir
scripts = preferences.addons[__package__].preferences.proxy_scripts
proxy_scripts_path = preferences.addons[__package__].preferences.proxy_scripts_path
for strip in context.selected_editable_sequences:
# get resolution from active strip
bpy.ops.sequencerextra.read_exif()
sce = context.scene
try:
res = sce['metadata'][0]['Composite:ImageSize'].split("x")
except:
res=(sce.render.resolution_x, sce.render.resolution_y)
#print(res)
commands = create_proxy(context, strip, self.size, res)
if commands == None:
# Update scene
context.scene.update()
newstrip = context.scene.sequence_editor.active_strip
# deselect all other strips
for i in context.selected_editable_sequences:
if i.name != newstrip.name:
i.select=False
# Update scene
context.scene.update()
else:
create_proxy_scripts(proxy_scripts_path, commands, strip.name)
return {'FINISHED'}
class CreateBIProxyOperator(bpy.types.Operator):
""" Use BI system to create a proxy"""
bl_idname = "sequencer.create_bi_proxy_operator"
bl_label = " Create proxy with blender internal"
size = IntProperty(
name='proxysize',
default=1)
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(self, context):
strip = functions.act_strip(context)
scn = context.scene
if scn and scn.sequence_editor and scn.sequence_editor.active_strip:
return strip.type in ('MOVIE')
else:
return False
def execute(self, context):
preferences = context.user_preferences
strips = functions.get_selected_strips(context)
for strip in strips:
#deselect all other strips
for i in strips: i.select = False
#select current strip
strip.select = True
if strip.type == "MOVIE":
setup_proxy(context, strip, self.size)
#select all strips again
for strip in strips:
try:
strip.select=True
except ReferenceError:
pass
bpy.ops.sequencer.reload()
return {'FINISHED'}
class CreateProxyToolPanel(bpy.types.Panel):
""" """
bl_label = "Proxy Tools"
bl_idname = "OBJECT_PT_ProxyTool"
bl_space_type = 'SEQUENCE_EDITOR'
bl_region_type = 'UI'
@classmethod
def poll(self, context):
if context.space_data.view_type in {'SEQUENCER',
'SEQUENCER_PREVIEW'}:
strip = functions.act_strip(context)
scn = context.scene
preferences = context.user_preferences
prefs = preferences.addons[__package__].preferences
if scn and scn.sequence_editor and scn.sequence_editor.active_strip:
if prefs.use_proxy_tools:
return strip.type in ('MOVIE')
else:
return False
def draw_header(self, context):
layout = self.layout
layout.label(text="", icon="AUTO")
def draw(self, context):
preferences = context.user_preferences
prefs = preferences.addons[__package__].preferences
layout = self.layout
layout.prop(prefs, "use_internal_proxy", text="use BI proxy builder")
strip = functions.act_strip(context)
if prefs.use_internal_proxy:
layout = self.layout
row = layout.row(align=True)
row.prop(prefs, "use_bi_custom_directory")
if prefs.use_bi_custom_directory:
row.prop(prefs, "proxy_dir", text="")
filename = strip.filepath.rpartition("/")[2].rpartition(".")[0]
layout.label("sample dir: //"+bpy.path.abspath(prefs.proxy_dir+filename))
layout = self.layout
col = layout.column()
col.label(text="Build JPEG quality")
col.prop(prefs, "quality")
if strip.type == 'MOVIE':
col = layout.column()
col.label(text="Use timecode index:")
col.prop(prefs, "timecode")
layout = self.layout
layout.label("setup and create BI proxy:")
row = layout.row(align=True)
for i in range(4):
proxysuffix = proxy_qualities[i][1]
row.operator("sequencer.create_bi_proxy_operator",
text=proxysuffix).size=i+1
layout = self.layout
layout.operator("sequencer.create_bi_proxy_operator",
text="Clear proxy sizes").size=5
else:
layout = self.layout
layout.prop(prefs, "proxy_dir", text="path for proxies")
layout = self.layout
layout.label("create and import proxy from clip:")
row = layout.row(align=True)
layout = self.layout
layout.prop(prefs, "ffmpeg_command", text="command")
layout.label("{} = filename, with, height, fileoutput")
label = prefs.ffmpeg_command.format("filename", "with", "height", "fileoutput")
layout.label(label)
for i in range(4):
proxysuffix = proxy_qualities[i][1]
row.operator("sequencer.create_proxy_operator",text=proxysuffix).size=i+1
layout = self.layout
layout.prop(prefs, "proxy_scripts")
if prefs.proxy_scripts:
layout = self.layout
layout.prop(prefs, "proxy_scripts_path", text="path for scripts")
layout = self.layout
box = layout.box()
box.prop(context.space_data, "proxy_render_size")
box.operator("sequencer.rebuild_proxy",
text="Rebuild Proxies and TC")