I have been trying to create a for loop in which Python goes through all of the video files in a folder, and converts each individual video file to a new audio file.
I know that effective code for doing it exists, but only for individual files.
This is my code:
import moviepy
import os
import moviepy.editor
import tempfile
pathdir = "path/to/dir"
for filename in os.listdir(pathdir):
filename.endswith(".mkv")
print(filename)
video = moviepy.editor.VideoFileClip(filename)
audio = video.audio
audio.write_audiofile(filename + ".wav")
else:
print("Finished conversion")
And this is what comes up - notice the correct filename is printed, thus identified.
runfile('path/to/dir/convert video to audio script.py', wdir='path/to/dir/')
fg_av_ger_seg0.mkv
Traceback (most recent call last):
OSError: MoviePy error: the file fg_av_ger_seg0.mkv could not be found!
Please check that you entered the correct path.
For your information, here is the code it is based on, which works fine but only for a single file:
import moviepy.editor
video = moviepy.editor.VideoFileClip('sample.mp4')
audio = video.audio
audio.write_audiofile('audio.mp3')
Thank you for your help. I am a beginner, so I apologies in advance if the error is basic! :)