Skip to content

Instantly share code, notes, and snippets.

@fakeyanss
Created November 7, 2024 08:34
Show Gist options
  • Save fakeyanss/44adf17ab58112442c311b46a7e89ef0 to your computer and use it in GitHub Desktop.
Save fakeyanss/44adf17ab58112442c311b46a7e89ef0 to your computer and use it in GitHub Desktop.
mp3下载脚本--youtube搜索歌名并下载音频
  1. 准备环境 pip install youtube-search-python pip install yt-dlp

  2. chrome 下载插件 Get cookies.txt Clean

  3. 登录 youtube.com,使用 Get cookies.txt 插件到处 cookies.txt 文件,大概长这样

# Netscape HTTP Cookie File
# This file is generated by yt-dlp.  Do not edit.

.youtube.com    TRUE    /       TRUE    1763257635      LOGIN_INFO      xxxxxx
.youtube.com    TRUE    /       FALSE   0       PREF    tz=UTC&f4=4000000&f7=100&hl=en
.youtube.com    TRUE    /       FALSE   1731486633      xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1730885535      xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1746433934      xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1746433934      xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    0       YSC     xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1762417339      __Secure-1PSIDTS        xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1763257634      __Secure-3PAPISID       xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1763257634      __Secure-3PSID  xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1762418524      __Secure-3PSIDCC        xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       TRUE    1762417339      __Secure-3PSIDTS        xxxxxxxxxxxxxxx
.youtube.com    TRUE    /       FALSE   0       wide    1

  1. 准备要下载的歌曲名和作者名,写入到 playlist.txt,大概长这样
爱多一次痛多一次 - 谭咏麟
沉默是金(Live) - 许冠杰
在星星峡遇暴风雨 - 无边界音乐
少年锦时 - 赵雷
Euterpe - EGOIST
Brand New Story (GENERATIONS LIVE TOUR 2019 "少年クロニクル" Live at NAGOYA DOME 2019.11.16) - GENERATIONS from EXILE TRIBE
A Waltz for a Night - Julie Delpy
Ship In The Sand - Marble Sounds
import os
from yt_dlp import YoutubeDL
from youtubesearchpython import VideosSearch
# 配置:YouTube下载选项
YDL_OPTIONS = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': '%(title)s.%(ext)s', # 文件保存路径
'quiet': True,
'cookiefile': 'cookies.txt',
}
def search_youtube(query):
"""搜索YouTube并返回第一个匹配的视频URL"""
search = VideosSearch(query, limit=1)
results = search.result()
if results['result']:
return results['result'][0]['link']
else:
return None
def download_audio(url):
"""使用yt-dlp下载音频"""
with YoutubeDL(YDL_OPTIONS) as ydl:
ydl.download([url])
def main():
# 从歌单文件中读取歌曲列表
with open("playlist.txt", "r", encoding="utf-8") as file:
songs = file.readlines()
for song in songs:
song = song.strip()
print(f"正在搜索: {song}")
# 搜索YouTube
url = search_youtube(song)
if url:
print(f"找到视频: {url}, 开始下载...")
try:
download_audio(url)
print(f"{song} 下载完成!\n")
except Exception as e:
print(f"下载失败: {e}")
else:
print(f"未找到 {song} 的相关视频。")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment