import os import subprocess import numpy as np import uuid # Import uuid to generate…
- User
import os import subprocess import numpy as np import uuid # Import uuid to generate unique IDs from moviepy.editor import VideoFileClip from scipy.io import wavfile
temporary_audio_files = []
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, segment_duration, video_duration, starting_offset, ending_offset): 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'))
segment_half_duration = segment_duration / 2.0 start_index = int(starting_offset * rate) end_index = int((video_duration - ending_offset) * rate) moments = [] while len(moments) < num_moments: index = np.argmax(volume[start_index:end_index]) moment = (start_index + index) / rate # Skip the moment if it is too close to the start or the end of the video if moment - segment_half_duration < starting_offset or moment + segment_half_duration > video_duration - ending_offset: volume[start_index + index] = 0 continue moments.append(moment) clear_start = max(start_index, start_index + index - int(rate * segment_half_duration)) clear_end = min(end_index, start_index + index + int(rate * segment_half_duration)) volume[clear_start:clear_end] = 0 return momentsdef find_volume_peaks(audio_filename, num_peaks, 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')) peak_indices = np.argpartition(-volume, num_peaks)[:num_peaks] peak_moments = np.sort(peak_indices / rate) return peak_moments
def extract_segments(video_path, moments, segment_duration, video_duration, prefix="moment"): if not os.path.exists(output_folder): os.makedirs(output_folder) base_name = os.path.splitext(os.path.basename(video_path))[0] half_segment_duration = segment_duration / 2
for i, moment in enumerate(moments): start_time = max(moment - half_segment_duration, 0) end_time = min(start_time + segment_duration, video_duration) # Utilisez video_duration ici output_filename = f"{base_name}_moment{i + 1}.mp4" output_path = os.path.join(output_folder, output_filename) output_filename = f"{base_name}{prefix}{i + 1}.mp4" # Include prefix in filename output_path = os.path.join(output_folder, output_filename) command = [ "ffmpeg", "-y", # Overwrite output files without asking "-ss", str(start_time), # Start time "-i", video_path, # Input file "-t", str(min(segment_duration, video_duration - start_time)), # Duration or remaining video "-c", "copy", # Copy streams without re-encoding output_path # Output path ] subprocess.run(command, check=True, stderr=subprocess.PIPE) print(f"Extracted {output_filename}") - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.