Reputation: 1
I am trying to use MoviePy to convert an mp4 video to mp3 audio. In the below 'convertToMP3' function I pass in episode which is an RSS entry object that has a "title" attribute. I've already downloaded an mp4 video that shares the episode title save for some quotes around the file name.
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)
The problem I'm running into is that MoviePy can't find the video file. As you can see I'm using both implicit and absolute paths but neither works. I'm running on an Ubuntu 20.04 machine using Python 3.8.2 and I have all the optional MoviePy dependencies installed. Is there something else that I'm missing?
Traceback:
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,
File "/usr/lib/python3.8/runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.8/runpy.py", line 86, in _run_code
exec(code, 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,
File "/usr/local/lib/python3.8/dist-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 /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.
EDIT
def convertToMP3(self, episode, 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)
This still produced the same error:
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.106424/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
cli.main()
File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.106424/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/home/caleb/.vscode/extensions/ms-python.python-2020.8.106424/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,
File "/usr/lib/python3.8/runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.8/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/home/caleb/Documents/Programming_Projects/Podcast_DLoader/dLoader_tester.py", line 49, in <module>
dLoader1c.convertToMP3(latest_ep)
File "/home/caleb/Documents/Programming_Projects/Podcast_DLoader/Podcast_DLoader.py", line 156, in convertToMP3
video_version = VideoFileClip(video_title)
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,
File "/usr/local/lib/python3.8/dist-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 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.
Upvotes: 0
Views: 1623
Reputation: 21
Check that the working directory is the proper one. My problem was that I was in the venv environment directory instead of the main project directory.
import os
print(os.getcwd())
Run this to check the Current Working Directory.
Then, you can ensure you are in the desired directory by using Change Directory
os.chdir("/the/actual/path/to/your/folder") # Linux-style
os.chdir("C:\\the\\actual\\path\\to\\your\\folder") # Windows-style
os.chdir(r"C:\the\actual\path\to\your\folder") # Raw-string-style
You can also use the Terminal command cd
(aka change directory) before calling Python to run the script.
Upvotes: 0
Reputation: 1
So I came upon the same problem. What worked for me was to rename the file to a simple name. After processing I renamed the file back to it's original filename.
mp4_title = f"{yt.title}.mp4"
renamed_title = "VideoConvertToMP3.mp4"
video_file_path = os.path.join(self.current_dir, mp4_title)
renamed_video_file_path = os.path.join(self.current_dir, renamed_title)
audio_file_path = os.path.join(self.current_dir, f"{yt.title}.mp3")
When later I had the video file located in my folder I renamed the video_file_path
to the renamed version with a simpler string renamed_video_file_path
.
# Rename the video file
if os.path.exists(video_file_path):
os.rename(video_file_path, renamed_video_file_path)
# Convert the MP4 file to an MP3 file
self.convert_video_to_audio(renamed_video_file_path,
audio_file_path)
# Remove the MP4 file
os.remove(renamed_video_file_path)
def convert_video_to_audio(self, mp4, mp3):
# Save the MP4 file in a variable, use a function to
convert to MP3 >> Close the MP4 file
file_to_convert = AudioFileClip(mp4)
file_to_convert.write_audiofile(mp3)
file_to_convert.close()
Which essentially worked for me:
PS C:\Users\Guillaume\OneDrive\Documenten\SF\YTDownloader> & C:/Users/Guillaume/AppData/Local/Programs/Python/Python311/python.exe c:/Users/Guillaume/OneDrive/Documenten/SF/YTDownloader/main.py
C:\Users\Guillaume\OneDrive\Documenten\SF\YTDownloader\VideoConvertToMP3.mp4
MoviePy - Writing audio in
C:\Users\Guillaume\OneDrive\Documenten\SF\YTDownloader\Eminem - Tobey (feat. Big Sean & Babytron) [Official Audio].mp3
MoviePy - Done.
Upvotes: 0
Reputation: 11
I got this worked by adding complete address to the video file such as:
c = VideoFileClip(r"C:\Users\...\test.mp4").subclip(0, 5)
Upvotes: 1
Reputation: 1213
Most likely what is happening is that, this line:
video_title = ("\'%s.mp4\'" % episode.title)
is resulting in a file name like this: (assuming path_to_files
is /opt/moviedir
and episode.title
is episode1
)
`/opt/moviedir/'episode1.mp4'`
which may be different from the intended file name: /opt/moviedir/episode1.mp4
After looking at the back trace and looking at the source of moviepy, in particular here (lines 244-272), it looks like the filename is passed a parameter to the ffmpeg
command and the filename
is not correct. So it is definitely an issue with how we are forming the file names.
Looks like the current way of trying to form a video_title
is making single quotes as part of the file name, making it a different from the intended file name without single quotes.
Can you please try changing the video_title =
line to this:
video_title = episode.title + ".mp4"
because it looks like we don't need to do explicit escaping of the string when it is being passed to subprocess.Popen
without shell=True
set (this is how the moviepy
library is passing the filename
argument). please see this for more information.
Upvotes: 0