我在做什么:
在一定时间内循环播放视频。
但是,我得到了以下错误(该文件没有损坏):
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.
在我的代码中的注释行:
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
有人知道出了什么问题吗?任何帮助都是非常感谢的,谢谢。
我正在使用的文件:
https://drive.google.com/drive/folders/1n8ReLmPj8cIUi6og_GgmlRoumCMB7zIL?usp=sharing
全错误:
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.
发布于 2022-06-05 07:48:39
在write_videofile
之前更改文件夹--这可能会造成问题。
代码可以是“懒惰”的,当您定义VideoFileClip()
时,它可能不会读取文件,但是当您想在更改目录后编写新文件时,它可能会尝试从新位置读取文件。
您应该使用/full/path/to/Pexels Videos 1292738.mp4
而不是chdir()
完整的工作代码:
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)
https://stackoverflow.com/questions/72503468
复制相似问题