This commit is contained in:
j 2017-08-09 19:02:37 +02:00
parent f07318bea7
commit 16cbe7db87
2 changed files with 103 additions and 3 deletions

99
edit.py Executable file
View file

@ -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)

View file

@ -14,7 +14,7 @@ edit = json.load(open(edit_json))
render = '/tmp/out' render = '/tmp/out'
output = '/tmp/test.mp4' output = '/tmp/test.mp4'
height = 270 height = 480
aspect = 16/9 aspect = 16/9
width = int(height * aspect) width = int(height * aspect)
@ -35,13 +35,14 @@ for clip in edit:
if x != width: if x != width:
vf += ',crop=%s:%s' % (width, height) # crop center vf += ',crop=%s:%s' % (width, height) # crop center
elif y != height: 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) vf += ',crop=w=%s:h=%s:x=0:y=%s' % (width, height, offset)
options = [ options = [
'-vf', vf, '-vf', vf,
'-aspect', str(aspect), '-aspect', str(aspect),
'-c:v', 'libx264', '-c:v', 'libx264',
'-b:v', '8M', '-b:v', '2M',
'-preset:v', 'medium', '-profile:v', 'high', '-level:v', '4.0',
'-r:v', '25', '-r:v', '25',
'-c:a', 'aac', '-c:a', 'aac',
'-ar', '48000', '-ar', '48000',