首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用跟踪URI从Spotify提取音频特征

使用跟踪URI从Spotify提取音频特征
EN

Stack Overflow用户
提问于 2019-11-20 09:12:09
回答 1查看 301关注 0票数 0

我正在尝试使用track URI从Spotify中提取音频特征。我有一个500k的列表,并希望为所有人提取音频特征。我有一个可行的代码下面,可以提取80首歌曲的特征。我需要一些帮助来修改下面的代码,一次提取80,这样我就不会违反Spotify的限制。下面是该列表的一个示例

代码语言:javascript
复制
['spotify:track:2d7LPtieXdIYzf7yHPooWd',
 'spotify:track:0y4TKcc7p2H6P0GJlt01EI',
 'spotify:track:6q4c1vPRZREh7nw3wG7Ixz',
 'spotify:track:54KFQB6N4pn926IUUYZGzK',
 'spotify:track:0NeJjNlprGfZpeX2LQuN6c']
代码语言:javascript
复制
client_id = 'xxx'
client_secret = 'xxx'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
代码语言:javascript
复制
def get_audio_features(saved_uris):

    artist = []
    track = []

    danceability = []
    energy = []
    key = []
    loudness = []
    mode = []
    speechiness = []
    acousticness = []
    instrumentalness = []
    liveness = []
    valence = []
    tempo = []
    duration_ms = []


    for uri in saved_uris:

            x = sp.audio_features(uri)
            y = sp.track(uri)

        for audio_features in x:
            danceability.append(audio_features['danceability'])
            energy.append(audio_features['energy'])
            key.append(audio_features['key'])
            loudness.append(audio_features['loudness'])
            mode.append(audio_features['mode'])
            speechiness.append(audio_features['speechiness'])
            acousticness.append(audio_features['acousticness'])
            instrumentalness.append(audio_features['instrumentalness'])
            liveness.append(audio_features['liveness'])
            valence.append(audio_features['valence'])
            tempo.append(audio_features['tempo'])
            duration_ms.append(audio_features['duration_ms'])

        artist.append(y['album']['artists'][0]['name'])
        track.append(y['name'])

    df = pd.DataFrame()
    df['artist'] = artist
    df['track'] = track
    df['danceability'] = danceability
    df['energy'] = energy
    df['key'] = key
    df['loudness'] = loudness
    df['mode'] = mode
    df['speechiness'] = speechiness
    df['acousticness'] = acousticness
    df['instrumentalness'] = instrumentalness
    df['liveness'] = liveness
    df['valence'] = valence
    df['tempo'] = tempo
    df['duration_ms'] = duration_ms

    df.to_csv('data/xxx.csv')

    return df

我的输出是一个dataframe,它看起来像这样,为了提高可靠性,我删减了一些列:

代码语言:javascript
复制
artist               track   danceability   energy  key     loudness    
Sleeping At Last    Chasing    Cars          0.467  0.157      11 
EN

回答 1

Stack Overflow用户

发布于 2019-11-20 11:58:45

此代码将返回所需的数据帧。

代码语言:javascript
复制
import spotipy
import time
from spotipy.oauth2 import SpotifyClientCredentials #To access authorised Spotify data4
import pandas as pd

client_id = 'paste client_id here'
client_secret = 'paste client_secret here'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sp.trace=False

#your uri list goes here
s_list = ['spotify:track:2d7LPtieXdIYzf7yHPooWd','spotify:track:0y4TKcc7p2H6P0GJlt01EI','spotify:track:6q4c1vPRZREh7nw3wG7Ixz','spotify:track:54KFQB6N4pn926IUUYZGzK','spotify:track:0NeJjNlprGfZpeX2LQuN6c']

#put uri to dataframe
df = pd.DataFrame(s_list)
df.columns = ['URI']

df['energy'] = ''*df.shape[0]
df['loudness'] = ''*df.shape[0]
df['speechiness'] = ''*df.shape[0]
df['valence'] = ''*df.shape[0]
df['liveness'] = ''*df.shape[0]
df['tempo'] = ''*df.shape[0]
df['danceability'] = ''*df.shape[0]

for i in range(0,df.shape[0]):
  time.sleep(random.uniform(3, 6))
  URI = df.URI[i]
  features = sp.audio_features(URI)
  df.loc[i,'energy'] = features[0]['energy']
  df.loc[i,'speechiness'] = features[0]['speechiness']
  df.loc[i,'liveness'] = features[0]['liveness']
  df.loc[i,'loudness'] = features[0]['loudness']
  df.loc[i,'danceability'] = features[0]['danceability']
  df.loc[i,'tempo'] = features[0]['tempo']
  df.loc[i,'valence'] = features[0]['valence']
  uri=0

输出:

希望这解决了你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58945197

复制
相关文章

相似问题

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