无法找到 mp4 文件的路径

时间:2021-06-26 13:02:18

标签: python moviepy

我前天开始了一个项目,你可以选择使用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()

1 个答案:

答案 0 :(得分:0)

问题: yt.title 不等于保存的文件名,因为 pytube 在保存之前对视频标题使用函数 safe_filename()。在您的情况下,safe_filename() 从文件名中删除了撇号 '。

解决方案

添加

from pytube.helpers import safe_filename

改变

yt.streams.filter(only_audio=True)

mp4_file = (dir + yt.title + '.mp4')
mp3_file = (dir + yt.title + '.mp3') 

yt.streams.filter()

mp4_file = (r"{}{}{}".format(dir, safe_filename(yt.title), '.mp4'))
mp3_file = (r"{}{}{}".format(dir, safe_filename(yt.title), '.mp3'))

或者只是下载没有音频的 mp4 并将其重命名为 mp3

import os

里面如果你应该有类似的东西

t = yt.streams.filter(only_audio=True)
t[0].download(dir)
mp4_file = (r"{}{}{}".format(dir, safe_filename(yt.title), '.mp4'))
mp3_file = (r"{}{}{}".format(dir, safe_filename(yt.title), '.mp3'))
os.rename(mp4_file, mp3_file)
相关问题