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 moments

def 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}…")

                audio_path = 'temp_audio.wav'
                video_clip = VideoFileClip(video_path)
                
                # Apply starting and ending offsets to the video duration
                adjusted_duration = video_clip.duration - (starting_offset_seconds + ending_offset_seconds)
                video_clip.audio.subclip(starting_offset_seconds, starting_offset_seconds + adjusted_duration).write_audiofile(audio_path, codec='pcm_s16le', fps=44100, nbytes=2, buffersize=2000)
                
                moments = find_loudest_moments(audio_path, num_moments, adjusted_duration, segment_half_duration)
                video_clip.close()  # Close the video clip to free up resources

                extract_segments(video_path, moments, segment_duration)
                
                os.remove(audio_path)  # Delete the temporary audio file

                print(f"Finished processing video {video_path}")

if __name__ == "__main__":
    starting_offset_seconds = float(input("Combien de secondes à ignorer au début pour l'analyse ? "))
    ending_offset_seconds = float(input("Combien de secondes à ignorer à la fin pour l'analyse ? "))
    num_moments = int(input("Combien de moments forts souhaiteriez-vous extraire de chaque vidéo ? "))
    segment_duration = float(input("Quelle est la durée (en secondes) de chaque segment vidéo à extraire ? "))
    
    process_video_files(num_moments, segment_duration, starting_offset_seconds, ending_offset_seconds)

    print("All videos have been processed.")



Je crois qu'il y a un paramètre qui fait que les vidéos extraites ne doivent pas se chevaucher. Peut-on faire en sorte d'ajouter dans le code, une variante qui n'empêche pas le chevauchement des vidéos, et demander en début de script à l'utilisateur s'il veut que les vidéos se chevauchent ou non ?