Voici mon script : import os import subprocess import numpy as np import uuid from…
- User
Voici mon script :
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' amplitude_peaks = set()
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, max_segments): """Find the segments in the audio data without sorting.""" 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, max_segments) ]
codeloudness_and_segments = [ (segment_loudness[i], i * segment_duration) for i in range(num_segments) # limit to the desired number of segments if i * segment_duration >= starting_offset and (i + 1) * segment_duration <= video_duration - ending_offset ] # The segments are returned without any sorting return loudness_and_segmentsdef find_peak_amplitude_position(audio_segment): """Find the position of the peak amplitude within the given audio segment.""" peak_position = np.argmax(np.abs(audio_segment)) return peak_position
def adjust_start_time_for_peak(start_time, peak_position, segment_length, rate, peak_preference): """Adjust the segment's start time so the peak appears at the user's preferred position.""" desired_peak_position_in_segment = segment_length * (peak_preference / 6) new_start_time = start_time + (peak_position / rate) - desired_peak_position_in_segment # Ensure new start time does not go beyond the video's start or end new_start_time = max(0, new_start_time) return new_start_time
def sort_and_name_segments(segments_with_loudness, preference): # Tri basé sur la préférence if preference == 1: # Par ordre d’apparition (pas besoin de trier car find_loudest_segments retourne déjà dans cet ordre) named_segments = segments_with_loudness elif preference == 2: # Dans l’ordre inverse de l’apparition named_segments = list(reversed(segments_with_loudness)) elif preference == 3: # Par loudness croissante named_segments = sorted(segments_with_loudness, key=lambda x: x[0]) elif preference == 4: # Par loudness décroissante named_segments = sorted(segments_with_loudness, key=lambda x: x[0], reverse=True)
- Assistant
- User
Ceci se produit quand j'essaie d'extraire 30 segments, en omettant 6000 secondes du début et 0 de la fin. Aucune vidéo n'est extraite : pourtant, le fichier fait 120 minutes, soit 7200 secondes. Pourquoi ?
Please enter the start offset (sec): 6000 Please enter the end offset (sec): 0 Processing video .\bbf770b441b540a03ab82aedc50f6eeee2c759f37cca41aa51dbc038e1347b52.avi… Please enter the desired length for each video segment (sec): 10 Maximum number of segments obtainable: 163 Enter '1' to confirm, or any other key to enter a different segment length: 1 At which point do you want the peak loudness to occur within the segment? (Enter 1-5 for 1/6 to 5/6): 3 Please choose the order of the segments based on loudness:
- In order of appearance
- In reverse order
- By increasing loudness
- By decreasing loudness Enter your choice (1-4): 4 How many segments do you want to extract? 30 MoviePy - Writing audio in temp_audio_ff2be434-6463-473d-b706-f7afa758a649.wav MoviePy - Done. All videos have been processed.
- Assistant