I've been trying to implement with python 2.7 a function to pass a command to cli to convert videos with ffmpeg, until now works almost great, the problem is that sometimes throws IOError: [Errno 32] Broken pipe and I'm not shure why it's happening. Can you help me with this?
This is my function to pass a command to the 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
This is how I call it:
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
And this is the exception:
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
Line 79 is where I call the function, and line 216 is in the function where I print the stderrdata