1 回答
TA贡献1820条经验 获得超9个赞
获得不和谐搜索查询后,您可以使用:
import youtube_dl # youtube-dl-2020.3.1
import traceback, os, json
from youtube_search import YoutubeSearch # pip install youtube_search
"""
sources :
https://github.com/ytdl-org/youtube-dl/blob/master/README.md#embedding-youtube-dl
https://stackoverflow.com/questions/23727943/how-to-get-information-from-youtube-dl-in-python/31184514#31184514
https://stackoverflow.com/a/43143553/797495
"""
search = 'carlos paiao playback'
ydl_opts = {
'format': 'beataudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}]
}
yt = YoutubeSearch(search, max_results=1).to_json()
try:
yt_id = str(json.loads(yt)['videos'][0]['id'])
yt_url = 'https://www.youtube.com/watch?v='+yt_id
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([yt_url])
info = ydl.extract_info(yt_url)
songname = info.get('title', None) + "-" + yt_id + ".mp3"
if os.path.isfile(songname):
print("Song Downloaded: " + songname)
else:
print("Error: " + songname)
except:
pass
print(traceback.print_exc())
print("no results")
添加回答
举报