#!/usr/bin/python3 from glob import glob import os import json import shutil import subprocess import sys 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 return '%0.3f' % duration 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'), os.path.getmtime('DRONES.json'), os.path.getmtime('VOCALS.json'), ) return vtime < xtime def encode(xml, force=False): audio_xml = xml.replace('.xml', '.audio.xml') mp4 = xml.replace('.xml', '.mp4') mp4_480p = mp4.replace('.mp4', '.480p.mp4') pre = mp4 + '.pre.mp4' pre_480p = mp4_480p + '.pre.mp4' video = mp4 + '.v.mp4' audio = mp4 + '.wav' if force or is_new(xml, mp4): subprocess.call([ 'qmelt', xml, '-consumer', 'avformat:' + video, 'vcodec=libx264', 'strict=-2' ]) duration = get_videoduration(video) cmd = [ 'ffmpeg', '-y', '-i', video, '-c:a', 'copy', '-c:v', 'copy', '-t', duration, '-movflags', '+faststart', pre ] subprocess.call(cmd) os.unlink(video) shutil.move(pre, mp4) cmd = [ 'ffmpeg', '-y', '-i', mp4, '-c:a', 'copy', '-vf', 'scale=854:480', '-c:v', 'libx264', '-preset', 'medium', '-b:v', '750k', '-profile:v', 'high', '-movflags', '+faststart', pre_480p ] subprocess.call(cmd) shutil.move(pre_480p, mp4_480p) 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()