gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:55 25 4
gpt4 key购买 nike

我有一个 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)

其中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'

最佳答案

使用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)

关于python - 通过子进程将python的文件之类的对象传递给ffmpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com