4

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

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

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

AttributeError: 'cStringIO.StringO' object has no attribute 'fileno'

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

编辑:

完整追溯:

[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'
4

2 回答 2

3

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

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

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

command = ['ffmpeg', '-y', '-i', '-', '-f', 'mp3', '-']
process = subprocess.Popen(command, stdin=subprocess.PIPE)
recording_mp3, errordata = process.communicate(recording_wav)
于 2012-07-03T16:48:48.217 回答
1

您需要创建一个管道,将管道的读取端传递给子进程,并将数据转储到写入端。

于 2012-07-02T22:39:33.327 回答