import os import subprocess import numpy as np import uuid from moviepy.editor import…
- User
import os import subprocess import numpy as np import uuid from moviepy.editor import VideoFileClip from scipy.io import wavfile from scipy.signal import find_peaks
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)) ]
# 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 find_largest_longest_peak(audio_data, rate, threshold=0.7, min_duration=0.1): if audio_data.ndim > 1: # Convertir le signal stéréo en mono en faisant la moyenne des canaux audio_data = np.mean(audio_data, axis=1) """Find the largest and longest amplitude peak above a threshold.""" # Normaliser les données sonores norm_audio_data = audio_data / np.max(np.abs(audio_data))
# Trouver les positions où l'amplitude dépasse le seuil peak_indices, _ = find_peaks(np.abs(norm_audio_data), height=threshold) # Identifier les durées des pics durations = [] start_idx = peak_indices[0] for i in range(1, len(peak_indices)): if peak_indices[i] > (start_idx + min_duration * rate): end_idx = peak_indices[i-1] durations.append((start_idx, end_idx, end_idx - start_idx + 1)) start_idx = peak_indices[i] durations.append((start_idx, peak_indices[-1], peak_indices[-1] - start_idx + 1)) # Trouver le pic de plus longue durée longest_peak = max(durations, key=lambda x: x[2], default=(0, 0, 0)) # Trouver le pic avec la plus grande amplitude parmi les pics identifiés largest_peak_amplitude = max(norm_audio_data[peak_indices]) largest_peak = peak_indices[np.argmax(norm_audio_data[peak_indices])] return largest_peak, longest_peak - Assistant
- User
Peut-on ne pas prendre en compte le bruit de fond ?
- Assistant
- User
Peut-on implémenter une fonction qui détecte les voix humaines ?
- Assistant
- User
peut-on détecter les cris?
- Assistant
- User
Peux-tu intégrer la détection de cris à mon script ?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.