Regarde ce script : import os import subprocess from moviepy.editor import VideoFileClip…
- User
Regarde ce script :
import os import subprocess from moviepy.editor import VideoFileClip from scipy.io import wavfile import numpy as np
Define video file extensions and the output folder
video_extensions = ['.mp4', '.mkv', '.wmv', '.avi'] output_folder = 'Output'
def calculate_loudness(audio_data): if audio_data.ndim == 1: volume = audio_data ** 2 else: volume = np.mean(audio_data ** 2, axis=1) return np.sqrt(volume)
def find_loudest_moments(audio_filename, num_moments, duration, segment_half_duration): rate, audio_data = wavfile.read(audio_filename) if audio_data.ndim == 2: audio_data = np.mean(audio_data, axis=1) volume = calculate_loudness(audio_data.astype('float32'))
moments = [] searched_indexes = set() # Set pour garder la trace des index déjà traités while len(moments) < num_moments: index = np.argmax(volume) if index in searched_indexes: # Si ce pic a déjà été traité, on l'ignore et on continue volume[index] = 0 # On annule ce pic avant de continuer continue searched_indexes.add(index) # On ajoute l'index traité au set pour s'en souvenir moment = index / rate # Ensure the segment is within the video bounds and has the correct duration start_time = max(moment - segment_half_duration, 0) end_time = min(moment + segment_half_duration, duration) actual_duration = end_time - start_time if actual_duration >= segment_half_duration * 2: moments.append(moment) # Zero out the volume within the range of this moment to avoid re-selection clear_range = int(rate * segment_half_duration) volume[max(0, index - clear_range): min(len(volume), index + clear_range)] = 0 else: volume[index] = 0 # On annule ce pic avant de continuer return momentsdef extract_segments(video_path, moments, segment_duration): if not os.path.exists(output_folder): os.makedirs(output_folder)
base_name = os.path.splitext(os.path.basename(video_path))[0] half_duration = segment_duration / 2 for i, moment in enumerate(moments): start_time = max(moment - half_duration, 0) end_time = min(moment + half_duration, VideoFileClip(video_path).duration) actual_duration = end_time - start_time output_filename = f"{base_name}_moment{i + 1}.mp4" output_path = os.path.join(output_folder, output_filename) command = [ "ffmpeg", "-y", # Automatic overwrite of output files without asking "-i", video_path, # Input video file "-ss", str(start_time), # Start time for segment "-t", str(actual_duration), # Adjusted duration of segment "-c", "copy", # Copy the video and audio streams without re-encoding output_path # Output file ] subprocess.run(command, check=True, stderr=subprocess.PIPE)def process_video_files(num_moments, segment_duration, starting_offset_seconds, ending_offset_seconds): segment_half_duration = segment_duration / 2 for root, _, files in os.walk('.'): for file in files: if file.lower().endswith(tuple(video_extensions)): video_path = os.path.join(root, file) print(f"Processing video {video_path}…")
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.