pandora_cdosea/encode.py

75 lines
2.0 KiB
Python
Executable File

#!/usr/bin/python3
from glob import glob
import os
import json
import shutil
import subprocess
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('VOCALS.json')
)
return vtime < xtime
for xml in sorted(glob('output/*/*.xml')):
if xml.endswith('.audio.xml'):
continue
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 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)