pandora_render/edit.py

103 lines
3.2 KiB
Python
Raw Normal View History

2017-08-09 17:02:37 +00:00
#!/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] = {
2017-08-10 10:04:54 +00:00
'path': os.path.join(prefix, r['items'][0]['instances'][0]['path']),
2017-08-09 17:02:37 +00:00
'resolution': r['items'][0]['resolution']
}
return files[oshash]
2017-08-10 10:04:54 +00:00
def normalize(name):
return name.replace(':', '_').replace('/', '_')
2017-08-09 17:02:37 +00:00
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']
2017-08-10 10:04:54 +00:00
with open('%s_info.json' % normalize(edit_id), 'w') as fd:
2017-08-09 17:02:37 +00:00
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']
2017-08-10 10:04:54 +00:00
with open('%s.srt' % normalize(edit_id), 'wb') as fd:
2017-08-09 17:02:37 +00:00
fd.write(ox.srt.encode(subtitles))
2017-08-10 10:04:54 +00:00
with open('%s.json' % normalize(edit_id), 'w') as fd:
2017-08-09 17:02:37 +00:00
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)