How can I rewrite my code, so I will be able to use a lot of starttime and endtime (I…
- User
How can I rewrite my code, so I will be able to use a lot of start_time and end_time (I will be able to create a lot of clips in my code istead of makinh just one): from moviepy.video.io.VideoFileClip import VideoFileClip
input_video_path = r"C:\Users\user\Downloads\shedevry.mp4" output_video_path = r"C:\Users\user\Downloads\out\out1.mp4"
video = VideoFileClip(input_video_path)
start_time = 1 * 60 + 20 end_time = 5 * 60 + 30
cut_video = video.subclip(start_time, end_time)
cut_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac")
video.close() cut_video.close()
- Assistant
To create multiple clips from a video using different start and end times, you could define a list of tuples (or another collection) that contains the start and end times for each clip you want to create. Then, you can iterate over this collection, creating clips and saving them one by one. Here's how you can do this:
from moviepy.video.io.VideoFileClip import VideoFileClip input_video_path = r"C:\Users\user\Downloads\shedevry.mp4" output_directory = r"C:\Users\user\Downloads\out" # Define a list of (start_time, end_time) tuples clip_times = [ (1 * 60 + 20, 5 * 60 + 30), # Clip 1 (10 * 60 + 15, 15 * 60 + 45), # Clip 2 (20 * 60 + 0, 25 * 60 + 0), # Clip 3 # Add more tuples as needed ] # Load the video file video = VideoFileClip(input_video_path) # Loop over the list of clips for i, (start_time, end_time) in enumerate(clip_times): cut_video = video.subclip(start_time, end_time) output_video_path = f"{output_directory}\\out_clip_{i+1}.mp4" # Write the clip to a file cut_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac") # Close the clip cut_video.close() # Close the original video file video.close()Explanation:
- clip_times: A list of tuples where each tuple contains
(start_time, end_time)for the clips you want to create. - enumerate: This is used to keep track of the index
ifor naming the output files uniquely. - f-string: Used to format the output file name dynamically based on the loop index.
This way, you can easily manage multiple clips from the same video file. You can add more
(start_time, end_time)pairs to theclip_timeslist as needed. - clip_times: A list of tuples where each tuple contains
- User
What is wrong? from moviepy.video.io.VideoFileClip import VideoFileClip
input_video_path = r"C:\Users\user\Downloads\shedevry.mp4" output_directory = r"C:\Users\user\Downloads\out"
Define a list of (start_time, end_time) tuples
clip_times = [ (0 * 0 + 0, 1 * 60 + 32), # intro (1 * 60 + 32, 5 * 60 + 20), # 1-1 (5 * 60 + 20, 9 * 60 + 0), # 1-2 (9 * 60 + 0, 14 * 60 + 44), # 1-3 (14 * 60 + 44, 18 * 60 + 0), # 2-1 (18 * 60 + 0, 22 * 60 + 40), # 2-2 (22 * 60 + 40, 26 * 60 + 27), # 2-3 (26 * 60 + 27, 32 * 60 + 46), # 3-1 (32 * 60 + 46, 35 * 60 + 58), # 3-2 (35 * 60 + 58, 41 * 60 + 12), # 3-3 (41 * 60 + 12, 46 * 60 + 43), # 4-1 (46 * 60 + 43, 54 * 60 + 21), # 4-2 (54 * 60 + 21, 59 * 60 + 0), # 5-1 (59 * 60 + 0, 63 * 60 + 50), # 5-2 (63 * 60 + 50, 69 * 60 + 0), # 6-1 (69 * 60 + 0, 77 * 60 + 58), # 6-2 (77 * 60 + 58, 84 * 60 + 20), # 7-1 (84 * 60 + 20, 89 * 60 + 54), # 7-2 (89 * 60 + 54, 93 * 60 + 6), # 7-3 (93 * 60 + 6, 96 * 60 + 52), # 8-1 (96 * 60 + 52, 102 * 60 + 58), # 8-2 (102 * 60 + 58, 105 * 60 + 20), # 8-3 (105 * 60 + 20, 108 * 60 + 11), # 8-4 (108 * 60 + 11, 113 * 60 + 24), # 9-1 (113 * 60 + 24, 116 * 60 + 44), # 9-2 (116 * 60 + 44, 120 * 60 + 46), # 9-3 (120 * 60 + 46, 125 * 60 + 50), # 10-1 (125 * 60 + 50, 134 * 60 + 12), # 10-2 ]
Load the video file
video = VideoFileClip(input_video_path)
Loop over the list of clips
for i, (start_time, end_time) in enumerate(clip_times): cut_video = video.subclipped(start_time, end_time) output_video_path = f"{output_directory}\out_clip_{i+1}.mp4"
# Write the clip to a file cut_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac") # Close the clip cut_video.close()Close the original video file
video.close()
Errors: MoviePy - Done ! MoviePy - video ready C:\Users\user\Downloads\out\out_clip_1.mp4 Proc not detected {'video_found': True, 'audio_found': True, 'metadata': {'major_brand': 'mp42', 'minor_version': '0', 'compatible_brands': 'isommp42', 'encoder': 'Google'}, 'inputs': [{'streams': [{'input_number': 0, 'stream_number': 0, 'stream_type': 'video', 'language': None, 'default': True, 'size': [640, 360], 'bitrate': 254, 'fps': 25.0, 'codec_name': 'h264', 'profile': '(Main)', 'metadata': {'handler_name': 'ISO Media file produced by Google Inc.'}}, {'input_number': 0, 'stream_number': 1, 'stream_type': 'audio', 'language': 'eng', 'default': True, 'fps': 44100, 'bitrate': 95, 'metadata': {'handler_name': 'ISO Media file produced by Google Inc.'}}], 'input_number': 0}], 'duration': 8053.93, 'bitrate': 352, 'start': 0.0, 'default_video_input_number': 0, 'default_video_stream_number': 0, 'video_codec_name': 'h264', 'video_profile': '(Main)', 'video_size': [640, 360], 'video_bitrate': 254, 'video_fps': 25.0, 'default_audio_input_number': 0, 'default_audio_stream_number': 1, 'audio_fps': 44100, 'audio_bitrate': 95, 'video_duration': 8053.93, 'video_n_frames': 201348} C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\imageio_ffmpeg\binaries\ffmpeg-win64-v4.2.2.exe -ss 91.000000 -i C:\Users\user\Downloads\shedevry.mp4 -ss 1.000000 -loglevel error -f image2pipe -vf scale=640:360 -sws_flags bicubic -pix_fmt rgb24 -vcodec rawvideo - MoviePy - Building video C:\Users\user\Downloads\out\out_clip_2.mp4. MoviePy - Writing audio in out_clip_2TEMP_MPY_wvf_snd.mp4
chunk: 0%| | 0/5028 [00:00<?, ?it/s, now=None] chunk: 0%| | 2/5028 [00:00<08:30, 9.85it/s, now=None]Traceback (most recent call last): File "c:\Users\user\Desktop\python\main.py", line 47, in cut_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac") File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 53, in requires_duration return func(clip, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 143, in use_clip_fps_by_default return func(clip, *new_args, **new_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 24, in convert_masks_to_RGB return func(clip, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 94, in wrapper return func(*new_args, **new_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\video\VideoClip.py", line 379, in write_videofile self.audio.write_audiofile( File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 53, in requires_duration return func(clip, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 94, in wrapper return func(*new_args, **new_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\AudioClip.py", line 258, in write_audiofile return ffmpeg_audiowrite( ^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 53, in requires_duration return func(clip, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\io\ffmpeg_audiowriter.py", line 220, in ffmpeg_audiowrite for chunk in clip.iter_chunks( File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\AudioClip.py", line 112, in iter_chunks yield self.to_soundarray( ^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 53, in requires_duration return func(clip, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\AudioClip.py", line 160, in to_soundarray snd_array = self.get_frame(tt) ^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 94, in wrapper return func(*new_args, **new_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\Clip.py", line 87, in get_frame return self.frame_function(t) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\Clip.py", line 130, in new_clip = self.with_updated_frame_function(lambda t: func(self.get_frame, t)) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\Clip.py", line 186, in lambda get_frame, t: get_frame(time_func(t)), ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\decorators.py", line 94, in wrapper return func(*new_args, **new_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\Clip.py", line 87, in get_frame return self.frame_function(t) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\io\AudioFileClip.py", line 78, in self.frame_function = lambda t: self.reader.get_frame(t) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\io\readers.py", line 235, in get_frame self.buffer_around(fr_max) File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\io\readers.py", line 281, in buffer_around array = self.read_chunk(chunksize) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\moviepy\audio\io\readers.py", line 155, in read_chunk s = self.proc.stdout.read(self.nchannels * chunksize * self.nbytes) ^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'stdout'
[Done] exited with code=1 in 81.495 seconds
- Assistant
The error you are encountering is a common issue when working with MoviePy and arises due to a few potential reasons. The error message indicates that there’s a problem with reading audio chunks, which often relates to the audio processing part of the video file.
Based on your code and the error logs, there are a few things you might want to check or change:
-
Update MoviePy and FFmpeg: Ensure you are using the latest version of MoviePy. Additionally, make sure the FFmpeg binary used by MoviePy is properly set up. You can update MoviePy using pip:
pip install moviepy --upgradeMake sure the ffmpeg executable is in your system's PATH or configured correctly in MoviePy.
-
Correct Method Name: In your loop where you create the clip, you mistakenly used
video.subclipped()instead ofvideo.subclip(). Change this line:cut_video = video.subclip(start_time, end_time) # Correct method name -
Check the Output Directory: Ensure that the output directory (
output_directory) exists before you run the script. If it does not exist, create it. If the directory doesn't exist, MoviePy may not be able to write the output files. -
Audio Codec Compatibility: Sometimes, certain audio codecs may not work properly with certain formats. You might want to try specifying the audio codec explicitly or try a different codec that is more compatible with your input video.
-
Handling Errors Gracefully: Consider adding error handling around your processing loop. This way, even if one clip fails to process, the program can continue with the others.
Here’s the corrected version of your code with the method name fixed and basic error handling added:
from moviepy.video.io.VideoFileClip import VideoFileClip import os input_video_path = r"C:\Users\user\Downloads\shedevry.mp4" output_directory = r"C:\Users\user\Downloads\out" # Check if the output directory exists, if not, create it if not os.path.exists(output_directory): os.makedirs(output_directory) # Define a list of (start_time, end_time) tuples clip_times = [ (0 * 0 + 0, 1 * 60 + 32), # intro (1 * 60 + 32, 5 * 60 + 20), # 1-1 (5 * 60 + 20, 9 * 60 + 0), # 1-2 (9 * 60 + 0, 14 * 60 + 44), # 1-3 (14 * 60 + 44, 18 * 60 + 0), # 2-1 (18 * 60 + 0, 22 * 60 + 40), # 2-2 (22 * 60 + 40, 26 * 60 + 27), # 2-3 (26 * 60 + 27, 32 * 60 + 46), # 3-1 (32 * 60 + 46, 35 * 60 + 58), # 3-2 (35 * 60 + 58, 41 * 60 + 12), # 3-3 (41 * 60 + 12, 46 * 60 + 43), # 4-1 (46 * 60 + 43, 54 * 60 + 21), # 4-2 (54 * 60 + 21, 59 * 60 + 0), # 5-1 (59 * 60 + 0, 63 * 60 + 50), # 5-2 (63 * 60 + 50, 69 * 60 + 0), # 6-1 (69 * 60 + 0, 77 * 60 + 58), # 6-2 (77 * 60 + 58, 84 * 60 + 20), # 7-1 (84 * 60 + 20, 89 * 60 + 54), # 7-2 (89 * 60 + 54, 93 * 60 + 6), # 7-3 (93 * 60 + 6, 96 * 60 + 52), # 8-1 (96 * 60 + 52, 102 * 60 + 58), # 8-2 (102 * 60 + 58, 105 * 60 + 20), # 8-3 (105 * 60 + 20, 108 * 60 + 11), # 8-4 (108 * 60 + 11, 113 * 60 + 24), # 9-1 (113 * 60 + 24, 116 * 60 + 44), # 9-2 (116 * 60 + 44, 120 * 60 + 46), # 9-3 (120 * 60 + 46, 125 * 60 + 50), # 10-1 (125 * 60 + 50, 134 * 60 + 12), # 10-2 ] # Load the video file video = VideoFileClip(input_video_path) # Loop over the list of clips for i, (start_time, end_time) in enumerate(clip_times): try: cut_video = video.subclip(start_time, end_time) # Corrected method name output_video_path = f"{output_directory}\\out_clip_{i + 1}.mp4" # Write the clip to a file cut_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac") # Close the clip cut_video.close() except Exception as e: print(f"Error creating clip {i + 1}: {e}") # Close the original video file video.close()Additional Notes:
- If you continue to experience problems, consider checking the source video file itself to ensure it is not corrupted.
- If the video processing is particularly resource-intensive, make sure that your machine has adequate resources (RAM, CPU) available.
- If the error persists with some clips but not others, it may be related to specific time ranges or frames within the source video that are problematic. Adjusting the start and end times for those clips might help.
-