-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolve_merge_timelines.py
446 lines (368 loc) · 16 KB
/
resolve_merge_timelines.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
# -*- coding:utf-8 -*-
# Author: Jiri Sindelar
# Thanks to Zhang Laichi for numerous code pieces
# https://github.com/laciechang/resolve_batch_io_point/tree/main
# Thanks to Igor Ridanovic for providing the timecode conversion method
# https://github.com/IgorRidanovic/smpte
from operator import index
import pprint
fu = bmd.scriptapp('Fusion')
ui = fu.UIManager
disp = bmd.UIDispatcher(ui)
resolve = bmd.scriptapp('Resolve')
clipcolor_names = [
'Orange',
'Apricot',
'Yellow',
'Lime',
'Olive',
'Green',
'Teal',
'Navy',
'Blue',
'Purple',
'Violet',
'Pink',
'Tan',
'Beige',
'Brown',
'Chocolate'
]
merge_names = [
'Reel Name',
'Source File'
]
class SMPTE(object):
'''Frames to SMPTE timecode converter and reverse.'''
def __init__(self):
self.fps = 24.0
self.df = False
def get_frames(self, tc):
'''Converts SMPTE timecode to frame count.'''
if not tc or tc == '':
return None
if int(tc[9:]) > self.fps:
raise ValueError('SMPTE timecode to frame rate mismatch.', tc, self.fps)
hours = int(tc[:2])
minutes = int(tc[3:5])
seconds = int(tc[6:8])
frames = int(tc[9:])
totalMinutes = int(60 * hours + minutes)
# Drop frame calculation using the Duncan/Heidelberger method.
if self.df:
dropFrames = int(round(self.fps * 0.066666))
timeBase = int(round(self.fps))
hourFrames = int(timeBase * 60 * 60)
minuteFrames = int(timeBase * 60)
frm = int(((hourFrames * hours) + (minuteFrames * minutes) + (timeBase * seconds) + frames) - (dropFrames * (totalMinutes - (totalMinutes // 10))))
# Non drop frame calculation.
else:
self.fps = int(round(self.fps))
frm = int((totalMinutes * 60 + seconds) * self.fps + frames)
return frm
def get_tc(self, frames):
'''Converts frame count to SMPTE timecode.'''
frames = abs(frames)
# Drop frame calculation using the Duncan/Heidelberger method.
if self.df:
spacer = ':'
spacer2 = ';'
dropFrames = int(round(self.fps * .066666))
framesPerHour = int(round(self.fps * 3600))
framesPer24Hours = framesPerHour * 24
framesPer10Minutes = int(round(self.fps * 600))
framesPerMinute = int(round(self.fps) * 60 - dropFrames)
frames = frames % framesPer24Hours
d = frames // framesPer10Minutes
m = frames % framesPer10Minutes
if m > dropFrames:
frames = frames + (dropFrames * 9 * d) + dropFrames * ((m - dropFrames) // framesPerMinute)
else:
frames = frames + dropFrames * 9 * d
frRound = int(round(self.fps))
hr = int(frames // frRound // 60 // 60)
mn = int((frames // frRound // 60) % 60)
sc = int((frames // frRound) % 60)
fr = int(frames % frRound)
# Non drop frame calculation.
else:
self.fps = int(round(self.fps))
spacer = ':'
spacer2 = spacer
frHour = self.fps * 3600
frMin = self.fps * 60
hr = int(frames // frHour)
mn = int((frames - hr * frHour) // frMin)
sc = int((frames - hr * frHour - mn * frMin) // self.fps)
fr = int(round(frames - hr * frHour - mn * frMin - sc * self.fps))
# Return SMPTE timecode string.
return(
str(hr).zfill(2) + spacer +
str(mn).zfill(2) + spacer +
str(sc).zfill(2) + spacer2 +
str(fr).zfill(2)
)
class ResolveProject:
def __init__(self) -> None:
self.project_manager = resolve.GetProjectManager()
self.current_project = self.project_manager.GetCurrentProject()
self.project_name = self.current_project.GetName()
# list of timelines in the project
self.all_timelines = []
# filtered timeline names
self.selected_tl_names = []
self.selected_timelines = []
# list of all plates that are in filtered timelines
self.plates = []
# plates grouped by key
# keys are reel id's or source files
self.plate_groups = {}
# reel : long name <- included long names
self.merge_summary = {}
self.fps_mapping = {
'16': 16.0, '18': 18.0,
'23': 23.976, '24': 24.0,
'24.0': 24.0,
'25': 25.0, '29': 29.97,
'30': 30.0, '30.0': 30.0,
'47': 47.952,
'48': 48.0, '50': 50.0,
'59': 59.94, '60': 60.0,
'72': 72.0, '95': 95.904,
'96': 96.0, '100': 100.0,
'119': 119.88, '120': 120.0
}
self.smpte = SMPTE()
def current_timeline(self):
return self.project_manager.GetCurrentProject().GetCurrentTimeline()
def get_all_timelines_in_current_project(self):
all_tl = []
for i in range(1, self.current_project.GetTimelineCount()+1):
tl = self.current_project.GetTimelineByIndex(i)
tl_info = {
'item': tl,
'name': str(tl.GetName()),
'fps': str(tl.GetSetting('timelineFrameRate')),
'drop': bool(int(tl.GetSetting('timelineDropFrameTimecode'))),
'in': int(tl.GetStartFrame()),
'out': int(tl.GetEndFrame()),
'v_tracks': int(tl.GetTrackCount('video')),
'markers': tl.GetMarkers(),
'to_merge' : False
}
all_tl.append(tl_info)
self.all_timelines = sorted(all_tl, key=lambda k: (k['name'], k['in']))
return self.all_timelines
def filter_timelines(self, include=''):
self.get_all_timelines_in_current_project()
tl_to_merge = []
if self.all_timelines:
for one in self.all_timelines:
if (str(include) in str(one['name'])) or include == '':
one['to_merge'] = True
tl_to_merge.append(one['name'])
else:
one['to_merge'] = False
self.selected_tl_names = sorted(tl_to_merge)
return self.selected_tl_names
def get_plates(self, skip_color='Orange'):
plate_list = []
for one_tl in self.all_timelines:
if not one_tl['to_merge']:
continue
self.smpte.fps = self.fps_mapping[one_tl['fps']]
self.smpte.df = one_tl['drop']
for trck in range(1, one_tl['v_tracks'] + 1):
trck_name = one_tl['item'].GetTrackName('video', trck)
trck_items = one_tl['item'].GetItemListInTrack('video', trck)
for itm in trck_items:
pool_item = itm.GetMediaPoolItem()
clip_color = itm.GetClipColor()
ignore = False
if skip_color:
if skip_color == clip_color:
ignore = True
if not ignore:
clip_info = {
'timeline': one_tl['item'],
'timeline_name': one_tl['name'],
'item': itm,
'pool_item': pool_item,
'track_number': trck,
'track_name': trck_name,
'track_index': str(trck_items.index(itm)).zfill(4),
'name': itm.GetName(),
'color': clip_color,
'in': itm.GetStart(),
'out': itm.GetEnd(),
'head': itm.GetLeftOffset(),
'tail': itm.GetRightOffset(),
'duration': itm.GetDuration(),
'pool_name': pool_item.GetClipProperty('Clip Name') if pool_item is not None else None,
'pool_file_name': pool_item.GetClipProperty('File Name') if pool_item is not None else None,
'pool_reel': pool_item.GetClipProperty('Reel Name') if pool_item is not None else None,
'start_tc': pool_item.GetClipProperty('Start TC') if pool_item is not None else None,
'end_tc': pool_item.GetClipProperty('End TC') if pool_item is not None else None,
'merge_children': [],
'merge_children_names': [],
'merge_parent': None,
'merge_out':0
}
clip_info['long_name'] = '-'.join([clip_info['timeline_name'], str(clip_info['track_number']), clip_info['track_index'], clip_info['name']])
clip_info['start_tc_num'] = self.smpte.get_frames(clip_info['start_tc'])
clip_info['end_tc_num'] = self.smpte.get_frames(clip_info['end_tc'])
plate_list.append(clip_info)
self.plates = plate_list
def split_plates_by_reel(self, key='pool_reel'):
# group plates by reel
plate_grps = {}
if self.plates and len(self.plates) > 0:
for one in self.plates:
if one[key] not in plate_grps.keys():
plate_grps[one[key]] = [one]
else:
plate_grps[one[key]].append(one)
# sort plate groups by in point
for k, v in plate_grps.items():
plate_grps[k] = sorted(v, key=lambda kk: (kk['in'], kk['duration']))
self.plate_groups = plate_grps
def merge_plates(self, max_gap=10):
self.merge_summary = {}
if self.plate_groups and len(self.plate_groups) > 0:
for k, v in self.plate_groups.items():
merge_index = 0
for plate in v:
group_index = v.index(plate)
if group_index == 0:
plate['merge_children'] = []
plate['merge_parent'] = None
plate['merge_out'] = plate['out']
merge_index = group_index
continue
if plate['in'] - max_gap <= v[merge_index]['merge_out']:
# to be merged
v[merge_index]['merge_children'].append(group_index)
v[merge_index]['merge_children_names'].append(plate['long_name'])
plate['merge_parent'] = merge_index
if plate['out'] > v[merge_index]['merge_out']:
v[merge_index]['merge_out'] = plate['out']
else:
# not merged, new parent
plate['merge_parent'] = None
plate['merge_out'] = plate['out']
merge_index = group_index
# sum up plates that are "included"
for k, v in self.plate_groups.items():
if k not in self.merge_summary.keys():
self.merge_summary[k] = []
for plate in v:
plate_index = v.index(plate)
if plate['merge_parent']:
pass
else:
if plate['merge_children_names']:
self.merge_summary[k].append('{} <- {}'.format(plate['long_name'], ', '.join(plate['merge_children_names'])))
else:
self.merge_summary[k].append('{} ^'.format(plate['long_name']))
selection_group = ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.VGroup({"Spacing": 5, "Weight": 1},[
ui.Label({"StyleSheet": "max-height: 1px; background-color: rgb(10,10,10)"}),
ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.Label({"Text": "Timeline Filter:", "Alignment": {"AlignLeft": True}, "Weight": 0.1,}),
ui.LineEdit({"ID": 'include_only', "Text": "", "Weight": 0.5,}),
]),
ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.Label({"Text": "Pick Master Timeline:", "Alignment": {"AlignLeft": True}, "Weight": 0.1,}),
ui.ComboBox({"ID": 'timelines', "Alignment": {"AlignLeft": True}, "Weight": 0.5,}),
]),
ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.Label({"Text": "Merged Timeline Name:", "Alignment": {"AlignLeft": True}, "Weight": 0.1,}),
ui.LineEdit({"ID": 'merged_tl_name', "Text": "merged", "Weight": 0.5,}),
]),
ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.CheckBox({"ID": 'skip_clip_color', "Text": "Skip Clip Color:", "Checked": False, "AutoExclusive": True, "Checkable": True, "Events": {"Toggled": True}}),
ui.ComboBox({"ID": 'clip_colors', "Weight": 0.8,}),
]),
ui.Label({"StyleSheet": "max-height: 1px; background-color: rgb(10,10,10)"}),
ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.Label({"Text": 'Merge Gap:', "Weight": 0}),
ui.SpinBox({"ID": 'merge_gap', "Value": 10, "Minimum": 0, "Maximum": 100000, "SingleStep": 1}),
]),
ui.HGroup({"Spacing": 5, "Weight": 0},[
ui.Label({"Text": "Merge By:", "Alignment": {"AlignLeft": True}, "Weight": 0.1,}),
ui.ComboBox({"ID": 'merge_key', "Alignment": {"AlignLeft": True}, "Weight": 0.5,}),
]),
ui.Label({"StyleSheet": "max-height: 1px; background-color: rgb(10,10,10)"}),
])
])
window_01 = ui.VGroup([
ui.HGroup({"Spacing": 1},
[
ui.VGroup({"Spacing": 15, "Weight": 3},[
selection_group,
ui.Button({"ID": "merge_button", "Text": "Merge", "Weight": 0, "Enabled": True}),
ui.Label({"ID": 'status', "Text": "", "Alignment": {"AlignCenter": True}}),
ui.Label({"StyleSheet": "max-height: 5px;"}),
]),
]
)
])
dlg = disp.AddWindow({
'WindowTitle': 'Merge Timelines',
'ID': 'MyWin',
'Geometry': [
800, 500, # position when starting
450, 275 # width, height
],
},
window_01)
def _timelines_update(*ev):
itm['timelines'].Clear()
itm['timelines'].AddItems(PRJ.filter_timelines(str(dlg.Find('include_only').Text)))
dlg.Find('status').Text = "{} timelines to merge.".format(len(PRJ.selected_tl_names))
def _merge(ev):
filter_color = bool(itm['skip_clip_color'].Checked)
filtered_color = itm['clip_colors'].CurrentText
if not filter_color:
filtered_color = None
mrg_by = itm['merge_key'].CurrentText
if mrg_by == 'Reel Name':
mrg = 'pool_reel'
elif mrg_by == 'Source File':
mrg = 'pool_file_name'
gap = int(itm['merge_gap'].Value)
PRJ.get_plates(filtered_color)
PRJ.split_plates_by_reel(mrg)
PRJ.merge_plates(gap)
pprint.pprint(PRJ.plate_groups)
pprint.pprint(PRJ.merge_summary)
reels = 0
shots = 0
for k, v in PRJ.plate_groups.items():
reels +=1
for o in v:
shots +=1
plates = 0
for k, v in PRJ.merge_summary.items():
plates = plates + len(v)
_m = "{} sources, {} shots {} plates".format(reels, shots, plates)
print(_m)
dlg.Find('status').Text = _m
# TODO actually make a new timeline and add plates to it
def _exit(ev):
disp.ExitLoop()
def _run(ev):
print('I run!')
PRJ = ResolveProject()
itm = dlg.GetItems()
itm['clip_colors'].AddItems(clipcolor_names)
itm['merge_key'].AddItems(merge_names)
dlg.On.MyWin.Close = _exit
dlg.On["Run"].Clicked = _run
dlg.On["merge_button"].Clicked = _merge
dlg.On['include_only'].TextChanged = _timelines_update
_timelines_update()
current_folder_name = str(PRJ.project_manager.GetCurrentFolder())
dlg.Show()
disp.RunLoop()
dlg.Hide()