encode to tmp name and rename once done

This commit is contained in:
j 2014-11-04 15:38:00 +02:00
parent 62b07fcc8e
commit fb32ec7d96

View file

@ -10,6 +10,7 @@ import subprocess
import tempfile import tempfile
import time import time
import math import math
import shutil
from glob import glob from glob import glob
import numpy as np import numpy as np
@ -283,12 +284,17 @@ def stream(video, target, profile, info, avconv=None, audio_track=0):
+ video_settings + video_settings
if format == 'webm': if format == 'webm':
cmd += ['-f', 'webm', target] enc_target = target + '.tmp.webm'
else:
enc_target = target
if format == 'webm':
cmd += ['-f', 'webm', enc_target]
elif format == 'mp4': elif format == 'mp4':
#mp4 needs postprocessing(qt-faststart), write to temp file #mp4 needs postprocessing(qt-faststart), write to temp file
cmd += ["%s.mp4" % target] cmd += ["%s.mp4" % enc_target]
else: else:
cmd += [target] cmd += [enc_target]
#print cmd #print cmd
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
@ -298,20 +304,24 @@ def stream(video, target, profile, info, avconv=None, audio_track=0):
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
if p.returncode != 0: if p.returncode != 0:
t = "%s.mp4" % target if format == 'mp4' else target t = "%s.mp4" % enc_target if format == 'mp4' else enc_target
if os.path.exists(t): if os.path.exists(t):
os.unlink(t) os.unlink(t)
if os.path.exists(target):
os.unlink(target)
stdout = stdout.replace('\r\n', '\n').replace('\r', '\n') stdout = stdout.replace('\r\n', '\n').replace('\r', '\n')
return False, stdout return False, stdout
if format == 'mp4': if format == 'mp4':
cmd = ['qt-faststart', "%s.mp4" % target, target] cmd = ['qt-faststart', "%s.mp4" % enc_target, enc_target]
#print cmd #print cmd
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=open('/dev/null', 'w'), stdout=open('/dev/null', 'w'),
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
close_fds=True) close_fds=True)
p.communicate() p.communicate()
os.unlink("%s.mp4" % target) os.unlink("%s.mp4" % enc_target)
if p.returncode == 0 and enc_target != target:
shutil.move(enc_target, target)
return True, None return True, None