spotify文件下载路径_从计算机的音乐文件夹中自动执行Spotify上的播放列表

本文介绍了如何从计算机的音乐文件夹中自动创建和执行Spotify播放列表,通过链接本地音乐资源实现个性化播放体验。
摘要由CSDN通过智能技术生成

spotify文件下载路径

In modern society music streaming platforms are gaining a lot of popularity compared to the old mechanisms like vinyl records, CDs, mp3, files, etc. That’s why most people are preferring to save their favorite songs on music apps like Spotify. despite some people still have their music on audio files saved on their computer music folders.

在现代社会中,与诸如黑胶唱片,CD,mp3,文件等旧机制相比,音乐流媒体平台正变得越来越流行。这就是为什么大多数人都喜欢将自己喜欢的歌曲保存在Spotify之类的音乐应用程序中的原因。 尽管有些人仍然将音乐保存在计算机音乐文件夹中的音频文件上。

That’s my dad’s case, he is an old school music lover and all his music is located in external disks, pen drives, and CDs. but now he wants to start using streaming platforms because he is tired of wasting a lot of time finding his favorites songs on the bunch of music folders of his computer.

就是我父亲的情况,他是一个老派音乐爱好者,他所有的音乐都位于外置磁盘,笔式驱动器和CD中。 但是现在他想开始使用流媒体平台,因为他已经浪费了很多时间在计算机的音乐文件夹中查找自己喜欢的歌曲。

So using my little knowledge in Python I decided to create a way to automate the exportation of the music located on audio files of his computer to new playlists on Spotify. So let’s do it!.

因此,利用对Python的了解,我决定创建一种方法,可以自动将位于他计算机音频文件中的音乐导出到Spotify上的新播放列表。 因此,让我们开始吧!

必备工具: (Required Tools:)

  • Spotify Credentials to access their Apis and Data acquisition.

    Spotify凭据可访问其Apis和数据获取。

  • Libraries Pandas and Numpy for data analysis.

    图书馆 PandasNumpy用于数据分析。

  • Library Spotipy for creating playlists on Spotify.

    图书馆 Spotipy,用于在Spotify上创建播放列表。

  • Library eyed3 for obtaining features of music files.

    图书馆的眼睛3 用于获取音乐文件的功能。

  • Library OS for manipulating paths on the computer.

    用于操作计算机上路径的库操作系统

  • Libraries RE and CSV for data cleaning and data saving.

    图书馆RE CSV,用于数据清理和数据保存。

1.获取数据。 (1. Obtaining the Data.)

To start my project I decided to use a folder of my dad’s music to make my script. This folder contains 47 songs of different Post-Rock bands. The folder looks something like this:

为了开始我的项目,我决定使用父亲音乐的文件夹来制作脚本。 此文件夹包含47个不同后摇滚乐队的歌曲。 该文件夹如下所示:

Image for post
Post-Rock Music Folder with different songs in MP3 (image by author)
MP3中包含不同歌曲的后摇滚音乐文件夹(作者提供的图像)

I had to obtain the main information of all these files because I need them later to search the track on Spotify. To do this I used the library eyed3, which helps to analyze the features of an mp3 file. I decided to save the Title of the Track, Name of the Artist, and Name of the Album for each track. Some of these tracks didn’t have these features on their detailed information So I had to save the name of the file to tag the track instead. I also use the library OS to access the different paths of each track file.

我必须获取所有这些文件的主要信息,因为以后需要它们才能在Spotify上搜索曲目。 为此,我使用了eyed3库,该库有助于分析mp3文件的功能。 我决定为每个曲目保存曲目标题, 艺术家的 姓名和专辑的名称 。 其中一些轨道的详细信息没有这些功能,因此我不得不保存文件名来标记轨道。 我还使用库OS访问每个轨道文件的不同路径。

Finally Obtaining the features and information of all the track files I saved this data into a CSV file using the CSV library. the complete code is on my Github.

最后,获得所有轨道文件的功能和信息,然后使用CSV库将该数据保存到CSV文件中。 完整的代码在我的Github上

#librariesimport eyed3
import os
import re
import csv
import numpy as np
import time#Let the user introduce the path of the music folderpath = str(input("Please Introduce the Path Folder: "))#Save the name of each mp3 file on a list called "filenames"filenames = []
for r,d,f in os.walk(path):
for file in f:
if file.endswith(".mp3"):
filenames.append(file)#Obtain the features of each mp3 file and saved them on 3 listsname_song = []
name_artist = []
name_album = []for filename in filenames:
pathfile = path + "\\" + filename
audioinfo = eyed3.load(pathfile)
if not audioinfo.tag.title:
temp_name = re.findall("^(.+?).mp3",filename) [0].replace("_"," ")
name_song.append(temp_name)
else:
name_song.append(audioinfo.tag.title)
name_artist.append(audioinfo.tag.artist)if not audioinfo.tag.album:
name_album.append(np.nan)
else:
name_album.append(audioinfo.tag.album)time.sleep(0.05)#print the total number of the tracks obtainedprint(f"{str(len(name_song))} tracks obtained!")#Save the information into a csv file#let the user introduce the name of the new csv filename_file = str(input("Please Introduce the name of the new csv file: "))with open(name_file,"w",encoding="utf-8") as f:
w = csv.writer(f)
w.writerows(zip(name_song,name_artist,name_album))
f.close()print(f"{name_file} saved and closed!")

With this script, I created a CSV file containing all the information required to search those tracks files on the Spotify database and then create the new playlists for my dad!.

使用此脚本,我创建了一个CSV文件,其中包含在Spotify数据库中搜索这些曲目文件并为父亲创建新的播放列表所需的所有信息!

Image for post
Command Prompt view of the script working (image by author)
脚本工作的命令提示符视图(作者提供的图像)

#Lame tag CRC check failed: this line appears when I run eyed3, taking a look at Google I realized is a common message when accessing or modify files too fast, but the total number of results doesn’t affect when appears this message.

#Lame标签CRC检查失败:当我运行eyed3时出现此行,看看Google,我发现访问或修改文件太快时这是一条常见消息,但是结果总数不会影响出现此消息的时间。

2.在Spotify上创建播放列表。 (2. Creating Playlists on Spotify.)

To create new playlists on Spotify I used the library Spotipy that helps to automate processes, get a lot of different features of a wide variety of tracks, etc. You just need to get a Client ID, Client Secret, and Username Number to use the Spotify’s Apis and manipulate your library music and account data. You also need to specify the Authorization Scope of the different methods you want to automate.

为了在Spotify上创建新的播放列表,我使用了Spotipy库,该库有助于自动化流程,获得各种曲目的许多不同功能等。您只需要获取一个Client ID,Client Secret和Username Number即可使用Spotify的Apis并处理您的音乐库和帐户数据。 您还需要指定要自动化的不同方法的授权范围

To search the tracks from my dad’s Post-Rock music folder that I wanted to save on new Spotify playlists, I had to obtain first the ID of all these tracks. To do that I use some credentials to have access and manipulate my account on Spotify. I also use the search function documented on the spotipy library to search if the track is on the Spotify database. the complete code is on my Github.

要从我想保存在新的Spotify播放列表中的父亲的Post-Rock音乐文件夹中搜索曲目,我必须首先获取所有这些曲目的ID。 为此,我使用一些凭据可以访问和操纵我在Spotify上的帐户。 我还使用Spotipy库中记录的搜索功能来搜索音轨是否在Spotify数据库中。 完整的代码在我的Github上

Authorization code to access the Spotify User Library.

访问Spotify用户库的授权代码。

#librariesimport spotipy
from spotipy import SpotifyClientCredentials, util
import pandas as pdclient_id='YOUR_CLIENT_ID'
client_secret='YOUR_CLIENT_SECRET'
redirect_uri='YOUR_REDIRECT_URL'
username = 'YOUR_SPOTIFY_USER_NUMBER'#The scope required to modify the user libraryscope_user = 'user-library-modify'#The scope required to modify the user playlistsscope_playlist = 'playlist-modify-public'#Credentials to access the user library musictoken_user=util.prompt_for_user_token(username,scope_user,client_id,client_secret,redirect_uri)sp_user = spotipy.Spotify(auth=token_user)#Credentiasl to access the user Playlists Musictoken_playlist=util.prompt_for_user_token(username,scope_playlist,client_id,client_secret,redirect_uri)sp_playlist = spotipy.Spotify(auth=token_playlist)

Code to obtain the Spotify ID of each track file and save them into Data Frame.

代码以获取每个轨道文件的Spotify ID并将其保存到数据框中。

#Open the CSV file created with pandas and specify the headersheaders = ['song','artist','album'])
data = pd.read_csv("../music/music-file.csv",names=headers)#Save the ID given by Spotify for each trackids = []for i in range(len(data)):query = data['artist'].loc[i] +" "+ data['song'].loc[i]
songs = sp_user.search(q=query,type='track',limit=2)try:
ids.append(songs['tracks']['items'][0]['id'])except IndexError:
print(f"ID Not Found: Please review the song's title with index {str(i)}")
ids.append(np.nan)

If you noticed I had to handle with Index Errors, that’s because some of the tracks didn’t have IDs on Spotify Database.

如果您注意到我必须处理索引错误,那是因为某些曲目在Spotify数据库上没有ID。

In case the track exists on the database, I saved the track ID given by Spotify on a new list called “ids”. In case the track doesn’t exist, the script gives me back the index of the track storage on my data frame to solve 2 possible issues:

如果轨道存在于数据库中,我将Spotify给出的轨道ID保存在一个名为“ ids”的新列表中。 如果轨道不存在,该脚本会向我返回数据帧上轨道存储的索引,以解决2个可能的问题:

  1. The title of the track is written slightly differently on the Spotify Music Database: The solution for this problem is just to modify the track title on the data frame and write the title located on Spotify. for instance, I had this problem with a song named “Twenty Two Fourteen” on my dad’s files and named “Twentytwofourteen” on Spotify Database. So I just modify the title of the song with the name used on Spotify.

    曲目的标题在Spotify音乐数据库上的书写方式略有不同 :解决此问题的方法只是修改数据帧上的曲目标题,然后将标题写在Spotify上。 例如,我父亲的档案中有一首歌名为“二十二十四岁”,而Spotify数据库中的一首歌名为“二十二十四岁”,我遇到了这个问题。 因此,我只是用Spotify上使用的名称来修改歌曲的标题。

  2. The track doesn’t exist on Spotify Music Database: In this case, the ID is replaced with NaN because Spotify doesn’t have the track on their Music Database.

    该曲目在Spotify音乐数据库上不存在:在这种情况下,ID被NaN替换,因为Spotify在其音乐数据库上没有该曲目。

Finally, I was able to create the playlist with the track files of my dad. Using the playlist scope and 2 functions of the Spotipy library, first I created the playlist called “Post-Rock” and then I use the ID of this new playlist to store all the tracks by its Spotify IDs.

最终,我能够使用父亲的曲目文件创建播放列表。 使用Spotipy库的播放列表作用域和2个功能,首先创建了一个名为“ Post-Rock”的播放列表,然后使用此新播放列表的ID通过其Spotify ID存储所有曲目。

#Create the new playlistnew_playlist = sp_playlist.user_playlist_create(username,"Post-Rock",public=True)#Populate the new playlist with the tracks using the Spotify Ids for each track.sp_playlist.user_playlist_add_tracks(username,playlist_id=new_playlist['id'],tracks=data['id'].tolist())

If you want to listen to the playlist you can access the link below.

如果您想收听播放列表,则可以访问下面的链接。

结论 (Conclusion)

Automating a task that can take a long time to complete, it can be useful in modern society considering the actual world is speeding the processes for any activity in an exponential way. Time becomes precious every day, so why not use the technology to automate slow and boring manual methods and spend the free time with our friends, family, or even creating new scripts to automate more things!.

自动化一项可能需要很长时间才能完成的任务,考虑到现实世界正在以指数方式加快任何活动的流程,因此在现代社会中它很有用。 时间每天都变得宝贵,所以为什么不使用该技术来使缓慢而乏味的手动方法自动化,并与我们的朋友,家人一起度过空闲时间,甚至创建新的脚本来使更多的事情自动化!

我的文章: (My Articles:)

翻译自: https://medium.com/datadriveninvestor/automating-playlists-on-spotify-from-the-music-folders-of-your-computer-c79060eadab2

spotify文件下载路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值