Passing python's file like object to ffmpeg via subprocess

celery, django, ffmpeg, python

Solution

Use `subprocess.Popen.communicate` to pass the input to your subprocess:

command = ['ffmpeg', '-y', '-i', '-', output_file.name]
process = subprocess.Popen(command, stdin=subprocess.PIPE)
process.communicate(recording_wav)

For extra fun, you could use the ffmpeg's output to avoid your NamedTemporaryFile:

command = ['ffmpeg', '-y', '-i', '-', '-f', 'mp3', '-']
process = subprocess.Popen(command, stdin=subprocess.PIPE)
recording_mp3, errordata = process.communicate(recording_wav)

Problem

I have a django FileField, which i use to store wav files on the Amazon s3 server. I have set up the celery task to read that file and convert it to mp3 and store it to another FileField. Problem i am facing is that i am unable to pass the input file to ffmpeg as the file is not the physical file on the hard disk drive. To circumvent that, i used stdin to feed the input stream of the file with the django's filefield. Here is the example: ``` output_file = NamedTemporaryFile(suffix='.mp3') subprocess.call(['ffmpeg', '-y', '-i', '-', output_file.name], stdin=recording_wav) ``` where recording_wav file is: , which is actually stored on the amazon s3 server. The error for the above subprocess call is: ``` AttributeError: 'cStringIO.StringO' object has no attribute 'fileno' ``` How can i do this? Thanks in advance for the help. Edit: Full traceback: ``` [2012-07-03 04:09:50,336: ERROR/MainProcess] Task api.tasks.convert_audio[b7ab4192-2bff-4ea4-9421-b664c8d6ae2e] raised exception: AttributeError("'cStringIO.StringO' object has no attribute 'fileno'",) Traceback (most recent call last): File "/home/tejinder/envs/tmai/local/lib/python2.7/site-packages/celery/execute/trace.py", line 181, in trace_task R = retval = fun(*args, **kwargs) File "/home/tejinder/projects/tmai/../tmai/apps/api/tasks.py", line 56, in convert_audio subprocess.Popen(['ffmpeg', '-y', '-i', '-', output_file.name], stdin=recording_wav) File "/usr/lib/python2.7/subprocess.py", line 672, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "/usr/lib/python2.7/subprocess.py", line 1043, in _get_handles p2cread = stdin.fileno() File "/home/tejinder/envs/tmai/local/lib/python2.7/site-packages/django/core/files/utils.py", line 12, in <lambda> fileno = property(lambda self: self.file.fileno) File "/home/tejinder/envs/tmai/local/lib/python2.7/site-packages/django/core/files/utils.py", line 12, in <lambda> fileno = property(lambda self: self.file.fileno) AttributeError: 'cStringIO.StringO' object has no attribute 'fileno' ```

Original source