pandora_cdosea/encode.py

64 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']) - 70/60
2017-03-02 21:10:14 +00:00
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-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-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-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)