使用youtube-dl下载YouTube音频遇URL无效错误,求解决方案
问题:youtube-dl 使用
-x参数下载音频时出错 我想用youtube-dl仅下载YouTube视频的音频,误以为将命令中的参数-o替换为-x即可实现该功能,但实际操作时出现错误(使用参数-q时无此错误),错误提示如下:
'~/Desktop/Music/name.%(ext)s' is not a valid URL. Set --default-search "ytsearch" (or run youtube-dl "ytsearch:~/Desktop/Music/name.%(ext)s" ) to search YouTube.
以下是我的Python代码,希望获得解决建议:
from os import system import sys from selenium import webdriver from selenium.webdriver.common.by import By def titleCase(s): ''' A function to convert the given song name to title case. The default function in Python convert the words 'the', 'and', 'of', etc. And I find that annoying ''' l = s.split() str = l[0][0].upper() + l[0][1:] for word in l[1:]: if word not in ['in', 'the', 'for', 'of', 'a', 'at', 'an', 'is', 'and']: str += ' ' + word[0].upper() + word[1:] else: str += ' ' + word return str def getVidID(song, URL): ''' This function gets the ID of the Video you have to download. ''' driver = webdriver.Chrome() driver.implicitly_wait(5) links = [] search = song + ' lyrics' searchQuery = '+'.join(search.split()) searchURL = URL + searchQuery driver.get(searchURL) link_webelement = driver.find_element(By.CSS_SELECTOR, 'div#contents ytd-item-section-renderer>div#contents a#thumbnail') links += [link_webelement.get_attribute('href')] vidID=links[0] return vidID def doStuff(song): print("Downloading " + titleCase(song)) URL = 'https://www.youtube.com/results?search_query=' link = getVidID(song, URL) pathToSave = "~/Desktop/Music/"+titleCase(song) system("youtube-dl -o \'" + pathToSave + ".%(ext)s\' \'" + link + "\'") print("Downloaded " + titleCase(song) + "\n") def main(): print('-------------------------------------------------------------') if (len(sys.argv) == 3 and (sys.argv[1] == '-i' or sys.argv[1] == '-I')): for song in open(sys.argv[2]).readlines(): doStuff(song) else: for song in sys.argv[1:]: doStuff(song) if __name__ == '__main__': main();
问题原因
你误解了参数用法:-x是提取音频的参数,并非用来替换-o。两者是配合关系,而非互斥替换——-o负责指定输出路径,-x负责将视频转换为音频格式。错误出现是因为你只传入-x加路径,youtube-dl误将保存路径识别为要下载的URL,因此抛出无效URL的错误。
修复方案
- 修正youtube-dl命令参数
修改doStuff函数中的命令,同时保留-x和-o参数:
system("youtube-dl -x -o \'" + pathToSave + ".%(ext)s\' \'" + link + "\'")
- 处理文件名中的换行符
readlines()会保留每行末尾的换行符,导致文件名包含无效字符,需在doStuff开头添加字符串清理:
def doStuff(song): song = song.strip() # 去除首尾空白和换行符 print("Downloading " + titleCase(song)) # 后续代码不变
- 关闭Chrome浏览器进程
getVidID函数中使用完浏览器后未关闭,会残留大量进程,需添加关闭逻辑:
def getVidID(song, URL): driver = webdriver.Chrome() try: driver.implicitly_wait(5) links = [] search = song + ' lyrics' searchQuery = '+'.join(search.split()) searchURL = URL + searchQuery driver.get(searchURL) link_webelement = driver.find_element(By.CSS_SELECTOR, 'div#contents ytd-item-section-renderer>div#contents a#thumbnail') links += [link_webelement.get_attribute('href')] vidID=links[0] return vidID finally: driver.quit() # 确保浏览器进程被关闭
- 过滤文件名特殊字符
若歌曲名包含/、:等系统禁止的字符,会导致路径无效,可添加过滤逻辑:
import re # 需在文件顶部导入re模块 def doStuff(song): song = song.strip() formatted_title = titleCase(song) # 过滤文件名中的特殊字符 safe_filename = re.sub(r'[^\w\s-]', '', formatted_title).strip() pathToSave = "~/Desktop/Music/" + safe_filename # 后续代码不变
内容的提问来源于stack exchange,提问作者Christopher Panizzolo
相关产品推荐
相关产品推荐

