[英]Is it possible to get Audio duration with Zapier Code (Python) Step?
is it possible to get the duration of an audio file with Zapier Code Step with Python or Javascript?是否可以使用 Python 或 Javascript 获得具有 Zapier 代码步骤的音频文件的持续时间?
I have uploaded a file to google drive and now I need the duration of the file.我已将文件上传到谷歌驱动器,现在我需要文件的持续时间。 If this is not possible with google drive files.如果谷歌驱动器文件无法做到这一点。 As an alternative I can use Dropbox or Amazon AWS.作为替代方案,我可以使用 Dropbox 或 Amazon AWS。
I have tried with the Zapier Code Step(Python):我尝试过使用 Zapier 代码步骤(Python):
import wave
import contextlib
fname = input.get('fileurl')
with contextlib.closing(wave.open(fname,'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
output = print(duration)
But this doesn't work.但这不起作用。 I got the error message:我收到错误消息:
Traceback (most recent call last):
File "<string>", line 11, in the_function
File "/var/lang/lib/python3.7/wave.py", line 510, in open
return Wave_read(f)
File "/var/lang/lib/python3.7/wave.py", line 164, in
This is a great question, I thought it wouldn't be possible.这是一个很好的问题,我认为这是不可能的。 but it turns out it is.但事实证明确实如此。 Your code isn't working because the fileurl
isn't a file name, so the library blows up (without a helpful error, unfortunately).您的代码无法正常工作,因为fileurl
不是文件名,因此库崩溃了(不幸的是,没有出现有用的错误)。
The trick is that wave.open
expects a filename or a file-like object.诀窍是wave.open
需要一个文件名或类似 object 的文件。 Code by Zapier
can't really use the filesystem, but we can create an in-memory "file" that we can feed into wave
. Code by Zapier
的代码不能真正使用文件系统,但我们可以创建一个内存“文件”,我们可以将其输入到wave
中。
Try this:尝试这个:
import wave
from io import BytesIO
# I pulled a file from
# https://file-examples.com/index.php/sample-audio-files/sample-wav-download/
# but you can use input_data['file_url'] or something instead
file_url = 'https://file-examples-com.github.io/uploads/2017/11/file_example_WAV_1MG.wav'
wave_resp = requests.get(file_url)
wav_file = BytesIO(wave_resp.content)
with wave.open(wav_file) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
return {'duration': duration}
For that test file, I get a duration
of 33.529625
- my browser says it's ~ 33 second long, so that looks correct!对于那个测试文件,我得到了33.529625
的duration
——我的浏览器说它大约是 33 秒长,所以看起来是正确的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.