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

27
edit.py
View file

@ -30,13 +30,27 @@ def get_info(api, oshash):
def normalize(name): def normalize(name):
return name.replace(':', '_').replace('/', '_') return name.replace(':', '_').replace('/', '_')
def sort_clips(clips, sort): def sort_clips(edit, sort):
clips = edit['clips']
reverse = sort.startswith('-') reverse = sort.startswith('-')
last = '' if reverse else 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' last = '' if reverse else 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
sort = sort.lstrip('-') sort = sort.lstrip('-')
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'])) s = sorted(clips, key=lambda c: (str(c.get(sort, last)), c['in'], c['out']))
if reverse: if reverse:
s = reversed(s) 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 return s
@ -54,7 +68,7 @@ if __name__ == '__main__':
videos = [] videos = []
subtitles = [] subtitles = []
position = 0 position = 0
for clip in sort_clips(edit['clips'], sort_by): for clip in sort_clips(edit, sort_by):
part_pos = 0 part_pos = 0
for i, duration in enumerate(clip['durations']): for i, duration in enumerate(clip['durations']):
@ -89,14 +103,17 @@ if __name__ == '__main__':
subtitles.append({ subtitles.append({
'in': position, 'in': position,
'out': position + (sub['out'] - sub['in']), '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'] 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)) 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) json.dump(videos, fd, indent=4, ensure_ascii=False)
with open('files.json', 'w') as fd: with open('files.json', 'w') as fd:

View file

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