Reputation: 624
WHAT I'M DOING:
Looping a video for a certain amount of time.
However, I'm getting the following error (The file is not corrupt):
Exception has occurred: OSError
MoviePy error: failed to read the first frame of video file Pexels Videos 1292738.mp4. That might mean that the file is corrupted.
at the commented line in my code:
chdir(r'C:\Users\jack_l\Downloads\makeAVideo\stock')
myStock = next(walk(r'C:\Users\jack_l\Downloads\makeAVideo\stock'), (None, None, []))[2]
stockFile = VideoFileClip(str(myStock[0]), target_resolution=(1080, 1920), audio=False)
stockFile = stockFile.loop(duration = 300)
stockFile = stockFile.set_fps(30)
chdir(r'C:\Users\jack_l\Downloads\makeAVideo')
stockFile.write_videofile('theVideo.mp4') # this line
Does anyone know what's going wrong? Any help is greatly appreciated, thank you.
THE FILE I'M USING:
https://drive.google.com/drive/folders/1n8ReLmPj8cIUi6og_GgmlRoumCMB7zIL?usp=sharing
FULL ERROR:
Traceback (most recent call last):
File "c:\Users\jack_l\Downloads\makeVideos.py", line 40, in <module>
stockFile.write_videofile('theVideo.mp4')
File "<decorator-gen-55>", line 2, in write_videofile
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
return f(clip, *a, **k)
File "<decorator-gen-54>", line 2, in write_videofile
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 135, in use_clip_fps_by_default
return f(clip, *new_a, **new_kw)
File "<decorator-gen-53>", line 2, in write_videofile
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 22, in convert_masks_to_RGB
return f(clip, *a, **k)
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\VideoClip.py", line 300,
in write_videofile
ffmpeg_write_video(self, filename, fps, codec,
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 220, in ffmpeg_write_video
for t,frame in clip.iter_frames(logger=logger, with_times=True,
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 472, in iter_frames
frame = self.get_frame(t)
File "<decorator-gen-11>", line 2, in get_frame
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 93, in get_frame return self.make_frame(t)
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 136, in <lambda> newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 187, in <lambda> return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
File "<decorator-gen-11>", line 2, in get_frame
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\Clip.py", line 93, in get_frame return self.make_frame(t)
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 113, in <lambda>
self.make_frame = lambda t: self.reader.get_frame(t)
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 184, in get_frame
result = self.read_frame()
File "c:\users\jack_l\appdata\local\programs\python\python310\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 133, in read_frame
raise IOError(("MoviePy error: failed to read the first frame of "
OSError: MoviePy error: failed to read the first frame of video file Pexels Videos 1292738.mp4. That might mean that the file is corrupted. That may also mean that you are using a deprecated version of FFMPEG. On Ubuntu/Debian for instance
the version in the repos is deprecated. Please update to a recent version from the website.
Upvotes: 3
Views: 5852
Reputation: 361
I ended up having TWO circumstances in my code cause this.
One was trying to export a video while simultaneously over-writing a video that was a source OF the video I was creating. Basically, the code edited a video by swapping out a part of it with another part -- and it used the original full video as part of the concatenation that I was then trying to export using the same filename. So basically, overwriting while trying to use the thing I'm over-writing to create the thing doing the overwriting. Maybe it's moronic that I even tried it, but it made for slightly more easy-to-write code to try it this way. (This was inside of a loop).
The other circumstance that triggered this was totally different: Trying to specify an "after clip" that didn't exist. Oops! Basically, I was concatenating videos, but in this case, there WAS no "after_clip" since I was swapping in the end of the video for another video ending. Adding a conditional to handle those cases solved the problem:
if video_part == (last_used_row-1):
# does NOT need an "after clip" if this is the last part of the video
# trying to include this in those circumstances caused an error.
updated_final_video = concatenate_videoclips([before_clip, new_clip])
else:
after_clip = previous_full_video.subclip(original_end_time, previous_full_video.duration)
# the below line prepares the new video sequence in MEMORY. it doesn't actually export the video.
# it can be thought of as preparing the instructions for what to do WHEN we export it.
updated_final_video = concatenate_videoclips([before_clip, new_clip, after_clip])
Note that this error message is NOT very helpful. I had to just, use my brain and think hard about my code the old-fashioned way to figure out what was causing this. It felt like an IMPOSSIBLE task, to actually have to think and solve a problem using my own brain cells, but somehow, I was able to pull it off.
Upvotes: 0
Reputation: 142661
You change folder before write_videofile
- and this can make problem.
Code can be "lazy" and it may NOT read file when you define VideoFileClip()
but when you want to write new file after changing directory - and it may try to read file from new place.
You should use /full/path/to/Pexels Videos 1292738.mp4
instead of using chdir()
Full working code:
import os
from moviepy.editor import *
# --- info ---
import moviepy
print('moviepy:', moviepy.__version__)
print('ffmpeg :', moviepy.config.FFMPEG_BINARY)
# --- main ---
input_dir = r'C:\Users\jack_l\Downloads\makeAVideo\stock'
output_dir = r'C:\Users\jack_l\Downloads\makeAVideo'
#print('chdir:', input_dir)
#os.chdir(input_dir)
root, dirs, files = next(os.walk(input_dir), (None, None, []))
#print(files)
if files:
#input_path = os.path.join(root, files[0])
input_path = os.path.join(input_dir, files[0])
output_path = os.path.join(output_dir, 'theVideo.mp4')
print('input :', input_path)
print('output:', output_path)
stock_file = VideoFileClip(input_path, target_resolution=(1080, 1920), audio=False)
stock_file = stock_file.loop(duration=300)
stock_file = stock_file.set_fps(30)
#print('chdir:', output_dir)
#os.chdir(output_dir)
stock_file.write_videofile(output_path)
Upvotes: 1