通过子进程将python的文件类对象传递给ffmpeg

tej*_*rss 5 python django ffmpeg celery

我有一个 django FileField,用于在 Amazon s3 服务器上存储 wav 文件。我已经设置了 celery 任务来读取该文件并将其转换为 mp3 并将其存储到另一个 FileField。我面临的问题是我无法将输入文件传递给 ffmpeg,因为该文件不是硬盘驱动器上的物理文件。为了避免这种情况,我使用 stdin 将 django 的文件字段提供给文件的输入流。这是示例:

output_file = NamedTemporaryFile(suffix='.mp3')
subprocess.call(['ffmpeg', '-y', '-i', '-', output_file.name], stdin=recording_wav)
Run Code Online (Sandbox Code Playgroud)

其中recording_wav文件是: ,它实际上存储在amazon s3服务器上。上述子进程调用的错误是:

AttributeError: 'cStringIO.StringO' object has no attribute 'fileno'
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?先谢谢您的帮助。

编辑:

完整回溯:

[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'
Run Code Online (Sandbox Code Playgroud)

esw*_*ald 4

用于subprocess.Popen.communicate将输入传递给子流程:

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

为了获得额外的乐趣,您可以使用 ffmpeg 的输出来避免使用 NamedTemporaryFile:

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