Comment rendre le placement du pic temporel plus précis dans la vidéo extraite ? import…
- User
Comment rendre le placement du pic temporel plus précis dans la vidéo extraite ?
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 extract_segments(video_path, moments, segment_duration, video_duration): 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) output_filename = f"{base_name}_moment{i + 1}.mp4" 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:v", "libx264", # Specify video codec for output "-preset", "medium", # Specify the encoding preset (trade-off between encoding speed and quality) "-crf", "23", # Specify the Constant Rate Factor for quality (lower means better quality) "-c:a", "aac", # Specify audio codec for output "-strict", "-2", # Necessary for some versions of ffmpeg to use experimental aac encoder "-b:a", "192k", # Specify the audio bitrate output_path # Output path ] subprocess.run(command, check=True, stderr=subprocess.PIPE) print(f"Extracted and re-encoded {output_filename}") - Assistant
- User
Je voudrais que ffmpeg, lors de l'extraction, place le pic d'amplitude sonore pile au milieu de la vidéo extraite
- Assistant
- User
avec cela, suisèje sûr que le milieu de la vidéo extraite sera occupé par le pic d'amplitude dans le segment ?
- Assistant
La logique dans la fonction modifiée
extract_segmentsvise à placer le pic d'amplitude sonore au milieu du segment vidéo extrait. Cependant, il y a un scénario possible où le pic ne serait peut-être pas exactement centré: si le pic se trouve trop près du début ou de la fin de la vidéo originale. Votre code gère déjà ce scénario en ajustant lestart_timepour s'assurer qu'il n'est pas négatif et que le segment extrait ne dépasse pas la durée de la vidéo originale.Voici la partie clé qui réalise le centrage:
start_time = moment - half_segment_duration if start_time < 0: start_time = 0 end_time = start_time + segment_duration if end_time > video_duration: end_time = video_duration start_time = max(end_time - segment_duration, 0)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.