Python 3.6 - 使用youtube_dl获取播放列表中所有视频的名称

时间:2017-06-24 23:21:24

标签: python playlist youtube-dl

我一直在尝试使用tkinteryoutube_dl创建一个程序,用户可以通过提供URL轻松地从YouTube下载视频。我的程序还会将这些视频分类到播放列表中供以后使用。下载单个视频时,我的应用程序运行正常,但是当用户输入播放列表而不是视频的链接时,我希望它能够正常工作。所有视频下载都正确,但是,一旦下载它们并且我的程序试图将它们移动到文件夹中它就会停止工作,因为我无法找到播放列表中视频的所有标题。

这是我的下载代码:

def downloadVideo(event = None):
    ydlOpts = {}
    url = urlEntry.get()
    urlParts = url.split("=")
    nameVid = nameVidEntry.get()
    with youtube_dl.YoutubeDL(ydlOpts) as ydl:
        info = ydl.extract_info(url) #Gets info about and starts the download
        print(info)
        title = info.get("title", None)
        if nameVid == "":
            nameVid = title.replace("|", "_")
        for filename in os.listdir("."):
            if filename.endswith(urlParts[1] + ".mp4"):
                os.rename(filename, nameVid + ".mp4")
    playlist = playlistEntry.get()
    if playlist != "":
        if not os.path.exists(os.path.join(os.getcwd(), playlist)):
            os.mkdir(os.path.join(os.getcwd(), playlist))
        shutil.copy(nameVid + ".mp4", os.path.join(os.getcwd(), playlist))
    if not os.path.exists(os.path.join(os.getcwd(), "videos")):
        os.mkdir(os.path.join(os.getcwd(), "videos"))        
    shutil.move(nameVid + ".mp4", os.path.join(os.getcwd(), "videos"))

发生的错误如下: Tkinter回调中的异常 Traceback(最近一次调用最后一次):   文件" C:\ Users \ Michael \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ shutil.py",第544行,移动     os.rename(src,real_dst) FileNotFoundError:[WinError 2]系统找不到指定的文件:' Flight Simulator.mp4' - > ' D:\我的编程\ Python \其他程序\ DownloadPlayer \ videos \ Flight Simulator.mp4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "D:\My Programming\Python\Other Programs\DownloadPlayer\DownloadPlayer.py", line 31, in downloadVideo
    shutil.move(nameVid + ".mp4", os.path.join(os.getcwd(), "videos"))
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 558, in move
    copy_function(src, real_dst)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 257, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'Flight Simulator.mp4'

我基本上只是想在用户输入播放列表链接和视频链接时使用此代码。提前谢谢!

编辑:飞行模拟器是我下载的播放列表的名称,因此没有名为Flight Simulator.mp4的文件,因为播放列表中的视频不同。

0 个答案:

没有答案