一天前我开始了一个项目,你可以选择使用pytube库下载任何Youtube视频,问题是它不支持mp3文件,所以我尝试使用moviepy,但没有成功
这是我的密码
def download_video():
link = url.get()
yt = YouTube(link)
if audio_download.get() == 1:
t=yt.streams.filter(only_audio=True)
t[0].download(dir)
mp4_file = (dir + yt.title + '.mp4')
mp3_file = (dir + yt.title + '.mp3')
video = VideoFileClip(mp4_file)
audioclip = video.audio
audioclip.write_audiofile(mp3_file)
video.close()
audioclip.close()
这是项目错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\kelvi\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "c:\Users\kelvi\Documents\Code\youtubedownloader.py", line 28, in download_video
video = VideoFileClip(mp4_file)
File "C:\Users\kelvi\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
File "C:\Users\kelvi\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 35, in __init__
infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
File "C:\Users\kelvi\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 270, in ffmpeg_parse_infos
raise IOError(("MoviePy error: the file %s could not be found!\n"
OSError: MoviePy error: the file C:/Users/kelvi/Documents/Video/Alicia Keys - If I Ain't Got You (Official Video).mp4 could not be found!
Please check that you entered the correct path.
这是完整的代码
from tkinter import *
from tkinter import filedialog
from pytube import YouTube
import time
from moviepy.editor import *
# Setup
root = Tk()
root.title("Youtube Downloader")
root.resizable(0,0)
audio_download = IntVar()
root.iconbitmap("logo.ico")
with open("directory.txt", 'r') as File:
dir = File.read()
File.close()
# Functions
def download_video():
link = url.get()
yt = YouTube(link)
if audio_download.get() == 1:
t=yt.streams.filter(only_audio=True)
t[0].download(dir)
mp4_file = (dir + yt.title + '.mp4')
mp3_file = (dir + yt.title + '.mp3')
video = VideoFileClip(mp4_file)
audioclip = video.audio
audioclip.write_audiofile(mp3_file)
video.close()
audioclip.close()
else:
t=yt.streams.get_highest_resolution()
t.download(dir)
def browse_file():
global dir
dir = filedialog.askdirectory()
with open("directory.txt", 'w') as File:
File.write(dir + '/')
File.close()
# Layout
header = Label(root,text="Youtube Downloader", font="Arial 28 bold",)
header.grid(row=0,column=0)
url = Entry(root, width=60)
url.grid(row=1,column=0)
audio_only = Checkbutton(root, text="Audio Only?", variable=audio_download, onvalue=1, offvalue=0)
audio_only.grid(row=1, column=1)
browse = Button(root,text="Browse", width=10,command=browse_file)
browse.grid(row=2, column=1)
button = Button(root, text="Download", width=30,command=download_video)
button.grid(row=2,column=0)
root.mainloop()
问题: yt.title不等于保存的文件名,因为pytube在保存之前对视频标题使用函数safe_filename()。在您的情况下,safe_filename()删除了文件名中的撇号
解决方案
加
改变
到
或者只下载不带音频的mp4并将其重命名为mp3
如果你有这样的东西
相关问题 更多 >
编程相关推荐