From 16cbe7db878e2ecf7c28c06da198869b1d01090d Mon Sep 17 00:00:00 2001 From: j Date: Wed, 9 Aug 2017 19:02:37 +0200 Subject: [PATCH] cleanup --- edit.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ffmpeg.py | 7 ++-- 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100755 edit.py diff --git a/edit.py b/edit.py new file mode 100755 index 0000000..f8bb834 --- /dev/null +++ b/edit.py @@ -0,0 +1,99 @@ +#!/usr/bin/python3 +import os +import json +import ox +import ox.web.auth + +base_url = 'https://0xdb.org' +credentials = ox.web.auth.get('0xdb.org') +prefix = '/Cinema' + +if os.path.exists('files.json'): + files = json.load(open('files.json')) +else: + files = {} + +def get_info(api, oshash): + if oshash not in files: + r = api.findMedia({ + 'query': { + 'conditions': [{'key': 'oshash', 'value': oshash}] + }, + 'keys': ['id', 'instances', 'resolution'] + })['data'] + files[oshash] = { + 'path': r['items'][0]['instances'][0]['path'], + 'resolution': r['items'][0]['resolution'] + } + return files[oshash] + +def sort_clips(clips, sort): + 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) + return s + + +if __name__ == '__main__': + import sys + edit_id = sys.argv[1] + + api = ox.API(base_url + '/api/') + api.signin(**credentials) + edit = api.getEdit(id=edit_id)['data'] + with open('%s_info.json' % edit_id, 'w') as fd: + json.dump(edit, fd, indent=4, ensure_ascii=False) + + videos = [] + subtitles = [] + position = 0 + for clip in sort_clips(edit['clips'], 'year'): + + part_pos = 0 + for i, duration in enumerate(clip['durations']): + if part_pos + duration < clip['in']: + part_pos += duration + elif part_pos <= clip['in']: + stream_in = clip['in'] - part_pos + stream_out = min(clip['out'] - part_pos, duration) + part_pos += duration + info = get_info(api, clip['streams'][i]) + videos.append({ + 'oshash': clip['streams'][i], + 'path': os.path.join(prefix, info['path']), + 'resolution': info['resolution'], + 'in': stream_in, + 'out': stream_out + }) + elif clip['out'] > part_pos: + stream_in = part_pos + stream_out = min(clip['out'] - part_pos, duration) + part_pos += duration + info = get_info(api, clip['streams'][i]) + videos.append({ + 'oshash': clip['streams'][i], + 'path': info['path'], + 'resolution': info['resolution'], + 'in': stream_in, + 'out': stream_out + }) + + for sub in clip['layers']['subtitles']: + subtitles.append({ + 'in': sub['in'] + position, + 'out': sub['out'] + position, + 'value': sub['value'], + }) + + position += clip['duration'] + + with open('%s.srt' % edit_id, 'wb') as fd: + fd.write(ox.srt.encode(subtitles)) + with open('%s.json' % edit_id, 'w') as fd: + json.dump(videos, fd, indent=4, ensure_ascii=False) + + with open('files.json', 'w') as fd: + json.dump(files, fd, indent=4, ensure_ascii=False) diff --git a/ffmpeg.py b/ffmpeg.py index 7a6f526..48a7d48 100755 --- a/ffmpeg.py +++ b/ffmpeg.py @@ -14,7 +14,7 @@ edit = json.load(open(edit_json)) render = '/tmp/out' output = '/tmp/test.mp4' -height = 270 +height = 480 aspect = 16/9 width = int(height * aspect) @@ -35,13 +35,14 @@ for clip in edit: if x != width: vf += ',crop=%s:%s' % (width, height) # crop center elif y != height: - offset = int((y - height) / 2) + offset = int(((y - height) / 3)) vf += ',crop=w=%s:h=%s:x=0:y=%s' % (width, height, offset) options = [ '-vf', vf, '-aspect', str(aspect), '-c:v', 'libx264', - '-b:v', '8M', + '-b:v', '2M', + '-preset:v', 'medium', '-profile:v', 'high', '-level:v', '4.0', '-r:v', '25', '-c:a', 'aac', '-ar', '48000',