Ce script ne s'arrête pas une fois le nombre décidé par l'utilisateur atteint : import…
- User
Ce script ne s'arrête pas une fois le nombre décidé par l'utilisateur atteint :
import os import subprocess import numpy as np import uuid from moviepy.editor import VideoFileClip from scipy.io import wavfile
Define video file extensions and the output folder
video_extensions = ['.mp4', '.mkv', '.wmv', '.avi'] output_folder = 'Output'
def calculate_loudness(audio_data, rate): """Calculate loudness using ITU-R BS.1770-4 method.""" if audio_data.ndim == 2: audio_data = np.mean(audio_data, axis=1) # convert to mono by averaging channels # Normalization constants for the ITU-R BS.1770-4 method pre_filter = np.array([1.53512485958697, -2.69169618940638, 1.19839281085285]) pre_gain = 1.0 power = 10.0 # Pre-filter audio_data = np.convolve(audio_data, pre_filter) * pre_gain # Calculate power and then LUFS mean_square = np.mean(audio_data ** 2) mean_power = mean_square ** (0.5) loudness = (1 / rate) * np.sum(mean_power ** power) lufs = -0.691 + 10 * np.log10(loudness) return lufs
def find_loudest_segments(audio_data, rate, num_segments, segment_duration, video_duration, starting_offset, ending_offset): """Find the segments with the highest loudness in the audio data.""" # Calculate the loudness of each segment in the audio file segment_loudness = [ calculate_loudness( audio_data[max(0, i * int(rate * segment_duration)) : min(len(audio_data), (i + 1) * int(rate * segment_duration))], rate) for i in range(0, int(video_duration / segment_duration)) ]
code# Combine segment start times with their corresponding loudness loudness_and_segments = [ (segment_loudness[i], i * segment_duration) for i in range(len(segment_loudness)) if i * segment_duration >= starting_offset and (i + 1) * segment_duration <= video_duration - ending_offset ] # Sort the loudness and segments based on loudness loudness_and_segments.sort(key=lambda x: x[0], reverse=True) # Return the top num_segments segments sorted by loudness return loudness_and_segments[:num_segments]def find_sound_amplitude_peak(audio_data): """Find the position of the highest sound amplitude peak.""" peak_position = np.argmax(np.abs(audio_data)) return peak_position
def sort_segments(segments_with_loudness, preference): if preference == 1: return sorted(segments_with_loudness, key=lambda x: x[1]) elif preference == 2: return sorted(segments_with_loudness, key=lambda x: x[1], reverse=True) elif preference == 3: return sorted(segments_with_loudness, key=lambda x: x[0]) elif preference == 4: return sorted(segments_with_loudness, key=lambda x: x[0], reverse=True)
- Assistant
The script provided is designed to extract video segments based on their loudness from video files within the current directory. It prompts the user for several parameters via the console such as the number of segments to extract, the duration of each segment, and preferences regarding the sorting of the output and position of sound amplitude peak within the segments.
The reason the script doesn't stop after reaching the user-decided number of segments (
num_segments) is that it loops through all video files in the current directory and applies the process to each one of them. The is respected for each individual video file processed. - User
peut-on faire en sorte qu'il ne scanne pas les sous-dossiers ?
- Assistant
Oui, vous pouvez modifier le script pour qu'il ne scanne pas les sous-dossiers lors de la recherche de fichiers vidéo. Pour cela, vous devez remplacer la boucle actuelle qui utilise
os.walk('.'), laquelle parcourt récursivement le dossier actuel et ses sous-dossiers, par une simple itération sur les fichiers du répertoire courant.