簡體   English   中英

是否可以使用 Zapier 代碼(Python)步驟獲得音頻持續時間?

[英]Is it possible to get Audio duration with Zapier Code (Python) Step?

是否可以使用 Python 或 Javascript 獲得具有 Zapier 代碼步驟的音頻文件的持續時間?

我已將文件上傳到谷歌驅動器,現在我需要文件的持續時間。 如果谷歌驅動器文件無法做到這一點。 作為替代方案,我可以使用 Dropbox 或 Amazon AWS。

我嘗試過使用 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)

但這不起作用。 我收到錯誤消息:

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

這是一個很好的問題,我認為這是不可能的。 但事實證明確實如此。 您的代碼無法正常工作,因為fileurl不是文件名,因此庫崩潰了(不幸的是,沒有出現有用的錯誤)。

訣竅是wave.open需要一個文件名或類似 object 的文件。 Code by Zapier的代碼不能真正使用文件系統,但我們可以創建一個內存“文件”,我們可以將其輸入到wave中。

嘗試這個:

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}

對於那個測試文件,我得到了33.529625duration ——我的瀏覽器說它大約是 33 秒長,所以看起來是正確的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM