首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >youtube_dl.utils.SameFileError _ python,youtube_dl

youtube_dl.utils.SameFileError _ python,youtube_dl
EN

Stack Overflow用户
提问于 2022-01-11 08:45:09
回答 1查看 305关注 0票数 0

我是youtube_dl的新手,我想在mp3上下载一个youtube播放列表,经过大量研究,我能够编写以下代码:

代码语言:javascript
复制
import youtube_dl
from time import sleep
# the playlist url
yt_url = input('Enter playlist URL: ')

print('Fetching playlist...')
# Gets the playlist and stores the video info in a list
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
with ydl:
    result = ydl.extract_info(yt_url, download=False)
    if 'entries' in result:
        video = result['entries']
        
        playlist = []
        for i, item in enumerate(video):
            video = result['entries'][i]
            playlist.append(video)

# Looping the list to download all the videos one by
# one, named as 1.mp3, 2.mp3, 3.mp3 and so on
a = 0
for video in playlist:
    a = a + 1
    ydl_opts = {'outtmpl': f'{a}.mp3'}
    print(f"\nDownloading {video['title']} (https://youtube.com/watch?v={video['id']}) as {a}.mp3...")
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(f"https://youtube.com/watch?v={video['id']}")
    sleep(0.5)
    print('Done.')
print('\n\nTask Finished.')

但是我不断地发现这个错误:

代码语言:javascript
复制
Downloading otherside (https://youtube.com/watch?v=kK81m-A3qpU) as 1.mp3...
Traceback (most recent call last):
  File "/storage/emulated/0/! workspace/goffy/files/bots/music/a/download.py", line 27, in <module>
    ydl.download(f"https://youtube.com/watch?v={video['id']}")
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 2063, in download
    raise SameFileError(outtmpl)
youtube_dl.utils.SameFileError: 1.mp3

我甚至没有任何名为1.mp3的文件,我只有一个名为download.py的文件,其中包含了该代码。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-11 09:11:44

尝试将下载URL放在列表中,因为这是download函数所期望的:

代码语言:javascript
复制
        ydl.download([f"https://youtube.com/watch?v={video['id']}"])

那么引发SameFileError异常的SameFileError应该通过,因为len(url_list)将是1。

目前,len(url_list)是URL中的字符数,因为它没有包装在list中。

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

https://stackoverflow.com/questions/70663881

复制
相关文章

相似问题

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