首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从google云存储桶中读取音频文件并在datalab笔记本中播放ipd

如何从google云存储桶中读取音频文件并在datalab笔记本中播放ipd
EN

Stack Overflow用户
提问于 2018-07-19 09:47:08
回答 1查看 1.6K关注 0票数 2

我想在从google云存储桶中读取的datalab笔记本中播放声音文件。该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-19 09:53:07

代码语言:javascript
复制
import numpy as np
import IPython.display as ipd
import librosa
import soundfile as sf
import io
from google.cloud import storage

BUCKET = 'some-bucket'

# Create a Cloud Storage client.
gcs = storage.Client()

# Get the bucket that the file will be uploaded to.
bucket = gcs.get_bucket(BUCKET)

# specify a filename
file_name = 'some_dir/some_audio.wav'

# read a blob
blob = bucket.blob(file_name)
file_as_string = blob.download_as_string()

# convert the string to bytes and then finally to audio samples as floats 
# and the audio sample rate
data, sample_rate = sf.read(io.BytesIO(file_as_string))

left_channel = data[:,0]  # I assume the left channel is column zero

# enable play button in datalab notebook
ipd.Audio(left_channel, rate=sample_rate)
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51413209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档