pandora_cdosea/encode.py

66 lines
1.8 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-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']) - 10/60
return '%0.3f' % duration
2017-02-22 16:30:55 +00:00
for xml in sorted(glob('output/*/*.xml')):
2017-02-17 22:07:43 +00:00
if xml.endswith('.audio.xml'):
continue
audio_xml = xml.replace('.xml', '.audio.xml')
2017-02-17 21:42:57 +00:00
mp4 = xml.replace('.xml', '.mp4')
2017-02-17 21:49:32 +00:00
pre = mp4 + '.pre.mp4'
video = mp4 + '.v.mp4'
audio = mp4 + '.wav'
2017-02-17 21:42:57 +00:00
if not os.path.exists(mp4) or os.path.getmtime(xml) > os.path.getmtime(mp4):
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-02-22 16:30:55 +00:00
'''
2017-02-17 21:42:57 +00:00
subprocess.call([
2017-02-17 22:07:43 +00:00
'qmelt', audio_xml, '-consumer', 'avformat:' + audio,
2017-02-17 21:42:57 +00:00
])
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,
'-i', audio,
2017-02-17 21:42:57 +00:00
'-map', '0:v',
'-map', '1:a',
2017-02-17 22:27:12 +00:00
'-c:v', 'copy',
2017-02-17 21:42:57 +00:00
'-strict', '-2',
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)
os.unlink(audio)
shutil.move(pre, mp4)
2017-02-22 16:30:55 +00:00
'''
2017-03-02 21:10:14 +00:00
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)