""" Script to extract the first 30 seconds from the output video. This script takes the detection_output_improved.mp4 file and creates a demo video containing only the first 30 seconds for presentation purposes. """ from moviepy import VideoFileClip import os def cut_first_30_seconds(input_video_path, output_video_path, duration_to_cut=30): """ Extract the first N seconds from a video and save to a new file. This creates a demo clip of the specified duration from the start of the video. Args: input_video_path: Path to the input video file output_video_path: Path where the output video will be saved duration_to_cut: Number of seconds to extract from the beginning (default: 30) """ # Check if input file exists if not os.path.exists(input_video_path): print(f"Error: Input video file not found: {input_video_path}") return False print(f"Loading video: {input_video_path}") # Load the video video = VideoFileClip(input_video_path) # Get the total duration total_duration = video.duration print(f"Original video duration: {total_duration:.2f} seconds") # Check if video is long enough if total_duration <= duration_to_cut: print(f"Error: Video is only {total_duration:.2f} seconds long, cannot cut {duration_to_cut} seconds") video.close() return False # Extract the first N seconds (keep only the first duration_to_cut seconds) print(f"Extracting first {duration_to_cut} seconds...") # In moviepy 2.x, use slicing notation to extract video segment cut_video = video[:duration_to_cut] # Write the output video print(f"Saving output video to: {output_video_path}") cut_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac') # Clean up cut_video.close() video.close() print(f"Successfully extracted first {duration_to_cut} seconds!") print(f"Demo video duration: {cut_video.duration:.2f} seconds") return True if __name__ == "__main__": # Define paths input_folder = "input and output" input_video = os.path.join(input_folder, "detection_output_improved.mp4") output_video = os.path.join(input_folder, "detection_output_demo_30sec.mp4") # Extract the first 30 seconds success = cut_first_30_seconds(input_video, output_video, duration_to_cut=30) if success: print("\nDone! Check the 'input and output' folder for 'detection_output_demo_30sec.mp4'") else: print("\nFailed to process video. Please check the error messages above.")