我一直在尝试用python 2.7实现一个函数,该命令将命令传递给cli以使用ffmpeg转换视频,直到现在效果非常好,问题是有时会引发IOError:[Errno 32]管道损坏,我我不知道为什么会这样。你能帮我吗?
这是我向cli传递命令的功能:
def _cli(cmd):
# TODO validate function
errors = False
import subprocess
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdoutdata, stderrdata = p.communicate()
if p.wait() != 0:
# Handle error / raise exception
# errors used to know conversion success or not
errors = True
print stderrdata
return stdoutdata, stderrdata, errors
except OSError as e:
print 'error'
traceback.print_exc()
return e.strerror, e.strerror, errors
这就是我所说的:
c = '/usr/bin/ffmpeg -hide_banner -nostats -i /home/user/original/390f8455-8657-4452-9fd9-0167d1025389.flv -c:v libx264 -c:a aac -strict -2 /home/user/original/390f8455-8657-4452-9fd9-0167d1025389.mp4'
print 'Converting command: %s' % c
stdoutdata, stderrdata, errors = _cli(c)
if errors:
print 'Converting video error'
# obj is django object
obj.convert_status = 'error'
obj.msg = stderrdata
这是一个例外:
Traceback (most recent call last):
File "/home/user/myapp/env/local/lib/python2.7/site-packages/rq/worker.py", line 799, in perform_job
rv = job.perform()
File "/home/user/myapp/env/local/lib/python2.7/site-packages/rq/job.py", line 600, in perform
self._result = self._execute()
File "/home/user/myapp/env/local/lib/python2.7/site-packages/rq/job.py", line 606, in _execute
return self.func(*self.args, **self.kwargs)
File "/home/user/myapp/cffmpeg/task.py", line 79, in convert_video
stdoutdata, stderrdata, errors = _cli(c)
File "/home/user/myapp/cffmpeg/task.py", line 216, in _cli
print stderrdata
IOError: [Errno 32] Broken pipe
第79行是我调用函数的地方,而第216行是我打印stderrdata的函数中的