pandora_cdosea/encode.py

65 lines
1.8 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
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')
pre = mp4 + '.pre.mp4'
video = mp4 + '.v.mp4'
audio = mp4 + '.wav'
if not os.path.exists(mp4) or os.path.getmtime(xml) > os.path.getmtime(mp4):
subprocess.call([
'qmelt', xml, '-consumer', 'avformat:' + video, 'vcodec=libx264', 'strict=-2'
])
'''
subprocess.call([
'qmelt', audio_xml, '-consumer', 'avformat:' + audio,
])
cmd = [
'ffmpeg', '-y',
'-i', video,
'-i', audio,
'-map', '0:v',
'-map', '1:a',
'-c:v', 'copy',
'-strict', '-2',
pre
]
subprocess.call(cmd)
os.unlink(video)
os.unlink(audio)
shutil.move(pre, mp4)
'''
duration = get_videoduration(video)
cmd = [
'ffmpeg', '-y',
'-i', video,
'-c:a', 'copy',
'-c:v', 'copy',
'-t', duration,
pre
]
subprocess.call(cmd)
os.unlink(video)
shutil.move(pre, mp4)