pandora_render/ffmpeg.py

71 lines
1.7 KiB
Python
Raw Normal View History

2017-08-09 10:30:58 +00:00
#!/usr/bin/python3
import json
import os
import subprocess
import sys
def run(cmd):
2017-08-09 14:40:28 +00:00
#print(' '.join('"%s"' % c for c in cmd))
subprocess.call(cmd)
2017-08-09 10:30:58 +00:00
edit_json = sys.argv[1]
edit = json.load(open(edit_json))
render = '/tmp/out'
output = '/tmp/test.mp4'
2017-08-09 17:02:37 +00:00
height = 480
2017-08-09 10:30:58 +00:00
aspect = 16/9
width = int(height * aspect)
2017-08-09 18:27:23 +00:00
width -= width % 2
2017-08-09 10:30:58 +00:00
files = []
for clip in edit:
2017-08-09 14:40:28 +00:00
out = render + '/%s_%0.3f-%0.3f.ts' % (clip['oshash'], clip['in'], clip['out'])
files.append(out)
if os.path.exists(out):
continue
2017-08-09 10:30:58 +00:00
clip_aspect = clip['resolution'][0] / clip['resolution'][1]
if clip_aspect < aspect:
x = width
y = int(x / clip_aspect)
2017-08-09 18:27:23 +00:00
y -= y % 2
2017-08-09 10:30:58 +00:00
else:
y = height
x = int(y * clip_aspect)
2017-08-09 18:27:23 +00:00
x -= x % 2
2017-08-09 10:30:58 +00:00
vf = 'scale=%s:%s' % (x, y)
if x != width:
vf += ',crop=%s:%s' % (width, height) # crop center
elif y != height:
2017-08-09 17:02:37 +00:00
offset = int(((y - height) / 3))
2017-08-09 10:30:58 +00:00
vf += ',crop=w=%s:h=%s:x=0:y=%s' % (width, height, offset)
options = [
'-vf', vf,
2017-08-09 14:40:28 +00:00
'-aspect', str(aspect),
2017-08-09 10:30:58 +00:00
'-c:v', 'libx264',
2017-08-09 17:02:37 +00:00
'-b:v', '2M',
'-preset:v', 'medium', '-profile:v', 'high', '-level:v', '4.0',
2017-08-09 10:30:58 +00:00
'-r:v', '25',
'-c:a', 'aac',
'-ar', '48000',
'-ac', '2',
'-b:a', '192k',
]
cmd = [
'ffmpeg',
'-ss', str(clip['in']),
'-i', clip['path']
] + options + [
'-t', str(clip['out'] - clip['in']),
out
]
run(cmd)
2017-08-09 14:40:28 +00:00
txt = output + '.txt'
with open(txt, 'w') as fd:
fd.write('file ' + '\nfile '.join(files))
cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', txt, '-c', 'copy', output]
2017-08-09 10:30:58 +00:00
run(cmd)
2017-08-09 14:40:28 +00:00
os.unlink(txt)