Reputation: 67
I'm using moviepy to cut a clip into multiple equally-sized segments, and it's reading from a txt file of start and end values to cut the clip properly. The following is my code:
required_video_file = VideoFileClip(filename)
with open("times.txt") as f:
times = f.readlines()
times = [x.strip() for x in times]
fullduration = required_video_file.duration
for time in times:
starttime = int(time.split("-")[0])
endtime = int(time.split("-")[1])
subclip = required_video_file.subclip(starttime, endtime)
filen = str(times.index(time)+1)+".mp4"
if starttime >= fullduration:
print(f"Video too short to complete request! Quitting loop..")
break
if endtime >= fullduration:
print(f"Cropping endtime from {endtime} to {fullduration}!")
endtime = None
subclip.write_videofile(filen, audio_codec='aac')
The current filename value is a 20-ish minute clip which has the times.txt
file cut up into 30-second portions:
0-30
30-60
60-90
90-120
120-150
And so on. However, I get an OSerror when it tries to write the final value in the sequence:
OSError: Error in file converted_fg01.mp4, Accessing time t=1352.40-1352.45 seconds, with clip duration=1352.400000 seconds,
The code is registering and activating the if
loop for if the endtime is greater than the full duration, but it's still outputting the error, no matter if i set it to the duration, to None, or any other possible fix I've tried.
FULL ERROR:
Cropping endtime of 1380 to 1352.4!
Moviepy - Building video 46.mp4.
MoviePy - Writing audio in 46TEMP_MPY_wvf_snd.mp4
chunk: 0%| | 0/662 [00:00<?, ?it/s, now=None]Traceback (most recent call last):
File "vidsplitter.py", line 80, in <module>
subclip.write_videofile(filen, audio_codec='aac')
File "<decorator-gen-60>", line 2, in write_videofile
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 56, in requires_duration
return f(clip, *a, **k)
File "<decorator-gen-59>", line 2, in write_videofile
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 135, in use_clip_fps_by_default
return f(clip, *new_a, **new_kw)
File "<decorator-gen-58>", line 2, in write_videofile
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 24, in convert_masks_to_RGB
return f(clip, *a, **k)
File "<decorator-gen-57>", line 2, in write_videofile
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/video/VideoClip.py", line 332, in write_videofile
self.audio.write_audiofile(
File "<decorator-gen-46>", line 2, in write_audiofile
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 56, in requires_duration
return f(clip, *a, **k)
File "<decorator-gen-45>", line 2, in write_audiofile
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/audio/AudioClip.py", line 230, in write_audiofile
return ffmpeg_audiowrite(
File "<decorator-gen-9>", line 2, in ffmpeg_audiowrite
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 56, in requires_duration
return f(clip, *a, **k)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/audio/io/ffmpeg_audiowriter.py", line 204, in ffmpeg_audiowrite
for chunk in clip.iter_chunks(
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/audio/AudioClip.py", line 90, in iter_chunks
yield self.to_soundarray(
File "<decorator-gen-43>", line 2, in to_soundarray
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 56, in requires_duration
return f(clip, *a, **k)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/audio/AudioClip.py", line 138, in to_soundarray
snd_array = self.get_frame(tt)
File "<decorator-gen-11>", line 2, in get_frame
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/Clip.py", line 98, in get_frame
return self.make_frame(t)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/Clip.py", line 141, in <lambda>
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/Clip.py", line 193, in <lambda>
lambda gf, t: gf(t_func(t)), apply_to, keep_duration=keep_duration
File "<decorator-gen-11>", line 2, in get_frame
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/Clip.py", line 98, in get_frame
return self.make_frame(t)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/audio/io/AudioFileClip.py", line 88, in <lambda>
self.make_frame = lambda t: self.reader.get_frame(t)
File "/Users/hypnoticocelot/Library/Python/3.8/lib/python/site-packages/moviepy/audio/io/readers.py", line 183, in get_frame
raise IOError(
OSError: Error in file converted_fg01.mp4, Accessing time t=1352.40-1352.45 seconds, with clip duration=1352.400000 seconds,
Upvotes: 0
Views: 1621
Reputation: 5428
Move the call to .subclip()
so it's after the if
statements, and hence the corrected endtime
is used:
for time in times:
starttime = int(time.split("-")[0])
endtime = int(time.split("-")[1])
filen = str(times.index(time)+1)+".mp4"
if starttime >= fullduration:
print(f"Video too short to complete request! Quitting loop..")
break
if endtime >= fullduration:
print(f"Cropping endtime from {endtime} to {fullduration}!")
endtime = None
subclip = required_video_file.subclip(starttime, endtime) # Moved this line so that it uses the corrected endtime
subclip.write_videofile(filen, audio_codec='aac')
Upvotes: 2