From 944c68ab3c28cd17fe290001ef99429a2c480fff Mon Sep 17 00:00:00 2001 From: j Date: Fri, 11 Aug 2017 12:08:34 +0200 Subject: [PATCH] more sort --- edit.py | 33 +++++++++++++++++++++++++-------- ffmpeg.py | 5 ++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/edit.py b/edit.py index 492ff15..3c0443e 100755 --- a/edit.py +++ b/edit.py @@ -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('
', '\n').replace('\n\n', '\n'), + 'value': sub['value'].replace('
', '\n').replace('
', '\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: diff --git a/ffmpeg.py b/ffmpeg.py index 3075ad7..2eed9a0 100755 --- a/ffmpeg.py +++ b/ffmpeg.py @@ -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'])