more sort

This commit is contained in:
j 2017-08-11 12:08:34 +02:00
parent 5486f6868e
commit 944c68ab3c
2 changed files with 29 additions and 9 deletions

33
edit.py
View file

@ -30,13 +30,27 @@ def get_info(api, oshash):
def normalize(name):
return name.replace(':', '_').replace('/', '_')
def sort_clips(clips, sort):
def sort_clips(edit, sort):
clips = edit['clips']
reverse = sort.startswith('-')
last = '' if reverse else 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
sort = sort.lstrip('-')
s = sorted(clips, key=lambda c: (str(c.get(sort, last)), c['in'], c['out']))
if reverse:
s = reversed(s)
if sort in [
'id', 'index', 'in', 'out', 'duration',
'title', 'director', 'year', 'videoRatio'
]:
s = sorted(clips, key=lambda c: (str(c.get(sort, last)), c['in'], c['out']))
if reverse:
s = reversed(s)
else:
ids = api.sortClips({
'edit': edit['id'],
'sort': [{'key': sort, 'operator': '-' if reverse else '+'}]
})['data']['clips']
print(set(c['id'] for c in clips) - set(ids))
print(set(ids) - set(c['id'] for c in clips))
s = sorted(clips, key=lambda c: ids.index(c['id']) if c['id'] in ids else -1)
return s
@ -54,7 +68,7 @@ if __name__ == '__main__':
videos = []
subtitles = []
position = 0
for clip in sort_clips(edit['clips'], sort_by):
for clip in sort_clips(edit, sort_by):
part_pos = 0
for i, duration in enumerate(clip['durations']):
@ -89,14 +103,17 @@ if __name__ == '__main__':
subtitles.append({
'in': position,
'out': position + (sub['out'] - sub['in']),
'value': sub['value'].replace('<br>', '\n').replace('\n\n', '\n'),
'value': sub['value'].replace('<br/>', '\n').replace('<br>', '\n').replace('\n\n', '\n'),
})
position += clip['duration']
with open('%s.srt' % normalize(edit_id), 'wb') as fd:
name = normalize(edit_id)
if sort_by != 'year':
name += '_' + sort_by
with open('%s.srt' % name, 'wb') as fd:
fd.write(ox.srt.encode(subtitles))
with open('%s.json' % normalize(edit_id), 'w') as fd:
with open('%s.json' % name, 'w') as fd:
json.dump(videos, fd, indent=4, ensure_ascii=False)
with open('files.json', 'w') as fd:

View file

@ -13,12 +13,15 @@ edit_json = sys.argv[1]
edit = json.load(open(edit_json))
render = '/tmp/out'
output = '/tmp/test.mp4'
output = os.path.splitext(edit_json)[0] + '.mp4'
height = 480
aspect = 16/9
width = int(height * aspect)
width -= width % 2
if not os.path.exists(render):
os.makedirs(render)
files = []
for clip in edit:
out = render + '/%s_%0.3f-%0.3f.ts' % (clip['oshash'], clip['in'], clip['out'])