Moviepy错误:找不到文件***

问题描述

我正在尝试使用MoviePy将mp4视频转换为mp3音频。在下面的“ convertToMP3”函数中,我传入一个情节,该情节是具有“ title”属性RSS条目对象。我已经下载了一个mp4视频,该视频共享情节标题,但文件名周围有一些引号。

    from moviepy.editor import AudioFileClip,VideoFileClip
    import os
    import Feedparser


    #Input: episode,an RSS entry object; cleanup (boolean),a  flag indicating whether 
    or not the old video file should be deleted 
    #Output: None
    #Creates an MP3 version of the video file and optionally deletes the video (cleanup)
    def convertToMP3(self,episode,cleanup = False):

        #Establish path to files
            
        #Try absolute path with quotes
        try:
            path_to_files = os.path.abspath(self.dest)
            video_title = ("\'%s.mp4\'" % episode.title)
            video_dir = os.path.join(path_to_files,video_title)   
            video_version = VideoFileClip(video_dir) 
        except Exception as e: 
            print("ERROR: Absolute path with quotes doesn't work. %s" % e)
       #Try absolute path without quotes
        try:
            path_to_files = os.path.abspath(self.dest)
            video_title = ("%s.mp4" % episode.title)
            video_dir = os.path.join(path_to_files,video_title)   
            video_version = VideoFileClip(video_dir) 
        except Exception as e: 
            print("ERROR: Absolute path without doesn't work. %s" % e)

        #Try implicit path with quotes
        try:
            path_to_files = (self.dest)
            video_title = ("\'%s.mp4\'" % episode.title)
            video_dir = os.path.join(path_to_files,video_title)
            video_version = VideoFileClip(video_dir)
        except Exception as e:
            print("ERROR: Implicit path with quotes doesn't work. %s" % e)

        #Try implicit path without quotes
      
        try:
            path_to_files = (self.dest)
            video_title = ("%s.mp4" % episode.title)
            video_dir = os.path.join(path_to_files,video_title)
            video_version = VideoFileClip(video_dir)
        except Exception as e:
            print("ERROR: Implicit path without quotes doesn't work. %s" % e)  
        try:
            #Create MP3 version of episode
            #video_dir = str(video_dir)
            #Todo: Below line of code cannot find the video file
            mp3_version = video_version.audio
            mp3_version.write_audiofile(path_to_files / episode.title)
            
            #Close the files
            mp3_version.close()
            video_version.close()
        except Exception as e:
            print("ERROR: File conversion Failed. %s" % e)

我遇到的问题是MoviePy找不到视频文件。如您所见,我同时使用了隐式路径和绝对路径,但均无效。我正在使用Python 3.8.2的Ubuntu 20.04计算机上运行,​​并且安装了所有可选的MoviePy依赖项。还有其他我想念的东西吗?

跟踪:

dLoader1b: Testing file conversion from MP4 to MP3
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py",line 193,in _run_module_as_main
    return _run_code(code,main_globals,None,File "/usr/lib/python3.8/runpy.py",line 86,in _run_code
    exec(code,run_globals)
  File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.105369/pythonFiles/lib/python/debugpy/__main__.py",line 45,in <module>
    cli.main()
  File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.105369/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py",line 430,in main
    run()
  File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.105369/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py",line 267,in run_file
    runpy.run_path(options.target,run_name=compat.force_str("__main__"))
  File "/usr/lib/python3.8/runpy.py",line 263,in run_path
    return _run_module_code(code,init_globals,run_name,line 96,in _run_module_code
    _run_code(code,mod_globals,run_globals)
  File "/home/caleb/Documents/Programming_Projects/podcast_DLoader/dLoader_tester.py",line 46,in <module>
    dLoader1c.convertToMP3(latest_ep)
  File "/home/caleb/Documents/Programming_Projects/podcast_DLoader/podcast_DLoader.py",line 128,in convertToMP3
    video_version = VideoFileClip(video_dir) 
  File "/usr/local/lib/python3.8/dist-packages/moviepy/video/io/VideoFileClip.py",line 88,in __init__
    self.reader = FFMPEG_VideoReader(filename,pix_fmt=pix_fmt,File "/usr/local/lib/python3.8/dist-packages/moviepy/video/io/ffmpeg_reader.py",line 35,in __init__
    infos = ffmpeg_parse_infos(filename,print_infos,check_duration,line 270,in ffmpeg_parse_infos
    raise IOError(("MoviePy error: the file %s Could not be found!\n"
OSError: MoviePy error: the file /home/caleb/Documents/Programming_Projects/podcast_DLoader/'NVIDIA CONFIRMS Your PSU Can't Handle RTX 3000 - WAN Show August 28,2020.mp4' Could not be found!
Please check that you entered the correct path.

编辑

def convertToMP3(self,cleanup = False):

path_to_files = (self.dest)
video_title = episode.title + ".mp4"
video_version = VideoFileClip(video_title)

try:
    #Create MP3 version of episode
    #video_dir = str(video_dir)
    #Todo: Below line of code cannot find the video file
    mp3_version = video_version.audio
    mp3_version.write_audiofile(path_to_files / episode.title)
    
    #Close the files
    mp3_version.close()
    video_version.close()
except Exception as e:
    print("ERROR: File conversion Failed. %s" % e)

这仍然会产生相同的错误

Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py",run_globals)
  File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.106424/pythonFiles/lib/python/debugpy/__main__.py",in <module>
    cli.main()
  File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.106424/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py",in main
    run()
  File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.106424/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py",line 49,line 156,in convertToMP3
    video_version = VideoFileClip(video_title)
  File "/usr/local/lib/python3.8/dist-packages/moviepy/video/io/VideoFileClip.py",in ffmpeg_parse_infos
    raise IOError(("MoviePy error: the file %s Could not be found!\n"
OSError: MoviePy error: the file NVIDIA CONFIRMS Your PSU Can't Handle RTX 3000 - WAN Show August 28,2020.mp4 Could not be found!
Please check that you entered the correct path.

解决方法

最可能发生的是这一行:

video_title = ("\'%s.mp4\'" % episode.title)

生成的文件名如下:(假设path_to_files/opt/moviedir,而episode.titleepisode1

`/opt/moviedir/'episode1.mp4'`

可能与预期的文件名不同:/opt/moviedir/episode1.mp4

编辑:

查看回溯轨迹并查看moviepy的来源之后,尤其是here(第244-272行),看起来文件名已通过参数传递给ffmpeg命令,并且filename不正确。因此,这绝对与我们如何形成文件名有关。

看起来,当前尝试形成video_title的方法是将单引号作为文件名的一部分,使其与不带单引号的预期文件名不同。

能否请您尝试将video_title =行更改为此:

video_title = episode.title + ".mp4"

因为看起来好像在没有设置subprocess.Popen的情况下将其传递给shell=True时,我们不需要对字符串进行显式转义(这是moviepy库传递的方式filename参数)。请参阅this了解更多信息。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...