import os import subprocess def add_silence_to_audio_ffmpeg(audio_path, tmp_audio_path, silence_duration_s=0.5): # 使用 ffmpeg 命令在音频前加上静音 cmd = [ 'ffmpeg', '-i', audio_path, # 输入音频文件路径 '-f', 'lavfi', # 使用 lavfi 虚拟输入设备生成静音 '-t', str(silence_duration_s), # 静音时长,单位秒 '-i', 'anullsrc=r=16000:cl=stereo', # 创建静音片段(假设音频为 stereo,采样率 44100) '-filter_complex', '[1][0]concat=n=2:v=0:a=1[out]', # 合并静音和原音频 '-map', '[out]', # 输出合并后的音频 '-y', tmp_audio_path, # 输出文件路径 '-loglevel', 'error' ] try: subprocess.run(cmd, check=True, capture_output=True, text=True) except subprocess.CalledProcessError as e: raise RuntimeError(f"ffmpeg failed ({e.returncode}): {e.stderr.strip()}") def convert_video_to_15fps_ffmpeg(video_path, output_path=None): """ Convert a video to 15 FPS using ffmpeg. Parameters ---------- video_path : str Path to the input .mp4 video. output_path : str, optional Path for the output video. If None, a new file will be created next to the input. Returns ------- str The output video path. """ if not os.path.exists(video_path): raise FileNotFoundError(f"Input video not found: {video_path}") # Auto-generate output path if not provided if output_path is None: base, ext = os.path.splitext(video_path) output_path = base + "_15fps.mp4" cmd = [ "ffmpeg", "-i", video_path, "-filter:v", "fps=15", # Set frame rate "-c:a", "copy", # Copy audio without re-encoding "-y", output_path, # Overwrite output "-loglevel", "error" ] try: subprocess.run(cmd, check=True, capture_output=True, text=True) except subprocess.CalledProcessError as e: raise RuntimeError(f"ffmpeg failed ({e.returncode}): {e.stderr.strip()}") return output_path