orage存储桶中读取音频文件并在Datalab笔记本中使用ip

orage存储桶中读取音频文件并在Datalab笔记本中使用ip

本文介绍了如何从Google Cloud Storage存储桶中读取音频文件并在Datalab笔记本中使用ipd播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to play a sound file in a datalab notebook which I read from a google cloud storage bucket. How to do this?

推荐答案

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)

这篇关于如何从Google Cloud Storage存储桶中读取音频文件并在Datalab笔记本中使用ipd播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:45