pandora_cdosea/encode.py

87 lines
2.3 KiB
Python
Raw Normal View History

2017-02-17 21:42:57 +00:00
#!/usr/bin/python3
from glob import glob
2017-02-17 21:49:32 +00:00
import os
2017-03-02 21:10:14 +00:00
import json
2017-02-17 21:49:32 +00:00
import shutil
import subprocess
2017-03-20 01:28:18 +00:00
import sys
2017-02-17 21:42:57 +00:00
2017-03-02 21:10:14 +00:00
def get_videoduration(video):
cmd = [
'ffprobe',
'-show_format',
'-show_chapters',
'-show_streams',
'-print_format', 'json',
'-i', video
]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stdin = p.communicate()
data = json.loads(stdout.decode())
duration = float([s for s in data['streams'] if 'width' in s][0]['duration']) - 70/60
2017-03-02 21:10:14 +00:00
return '%0.3f' % duration
2017-03-15 10:15:45 +00:00
def is_new(xml, mp4):
if not os.path.exists(mp4):
return True
xtime = os.path.getmtime(xml)
vtime = max(
os.path.getmtime(mp4),
os.path.getmtime('text.html'),
2017-03-15 12:00:12 +00:00
os.path.getmtime('DRONES.json'),
os.path.getmtime('VOCALS.json'),
2017-03-15 10:15:45 +00:00
)
return vtime < xtime
2017-03-20 01:28:18 +00:00
def encode(xml, force=False):
2017-02-17 22:07:43 +00:00
audio_xml = xml.replace('.xml', '.audio.xml')
2017-02-17 21:42:57 +00:00
mp4 = xml.replace('.xml', '.mp4')
2017-03-06 08:36:39 +00:00
mp4_480p = mp4.replace('.mp4', '.480p.mp4')
2017-02-17 21:49:32 +00:00
pre = mp4 + '.pre.mp4'
2017-03-06 08:36:39 +00:00
pre_480p = mp4_480p + '.pre.mp4'
2017-02-17 21:49:32 +00:00
video = mp4 + '.v.mp4'
audio = mp4 + '.wav'
2017-03-20 01:28:18 +00:00
if force or is_new(xml, mp4):
2017-02-17 21:42:57 +00:00
subprocess.call([
2017-02-17 21:51:54 +00:00
'qmelt', xml, '-consumer', 'avformat:' + video, 'vcodec=libx264', 'strict=-2'
2017-02-17 21:42:57 +00:00
])
2017-03-06 08:36:39 +00:00
duration = get_videoduration(video)
2017-02-17 22:27:12 +00:00
cmd = [
2017-02-17 21:42:57 +00:00
'ffmpeg', '-y',
2017-02-17 21:49:32 +00:00
'-i', video,
2017-03-06 08:36:39 +00:00
'-c:a', 'copy',
2017-02-17 22:27:12 +00:00
'-c:v', 'copy',
2017-03-06 08:36:39 +00:00
'-t', duration,
'-movflags', '+faststart',
2017-02-17 21:49:32 +00:00
pre
2017-02-17 22:27:12 +00:00
]
subprocess.call(cmd)
2017-02-17 21:49:32 +00:00
os.unlink(video)
shutil.move(pre, mp4)
2017-03-02 21:10:14 +00:00
cmd = [
'ffmpeg', '-y',
2017-03-06 08:36:39 +00:00
'-i', mp4,
2017-03-02 21:10:14 +00:00
'-c:a', 'copy',
2017-03-06 08:36:39 +00:00
'-vf', 'scale=854:480',
'-c:v', 'libx264',
'-preset', 'medium',
'-b:v', '750k',
'-profile:v', 'high',
'-movflags', '+faststart',
pre_480p
2017-03-02 21:10:14 +00:00
]
subprocess.call(cmd)
2017-03-06 08:36:39 +00:00
shutil.move(pre_480p, mp4_480p)
2017-03-20 01:28:18 +00:00
def encode_all():
for xml in sorted(glob('output/*/*.xml')):
if xml.endswith('.audio.xml') or xml.endswith('.vocals.xml'):
continue
encode(xml)
if __name__ == '__main__':
if len(sys.argv) == 2:
encode(sys.argv[1], True)
else:
encode_all()