| from moviepy import VideoFileClip, CompositeAudioClip |
|
|
| def shift_audio(video_path, output_path, shift_seconds=2.0): |
| print(f"🎬 正在加载视频: {video_path}") |
| |
| |
| video = VideoFileClip(video_path) |
| audio = video.audio |
| |
| |
| |
| |
| print(f"⏱️ 正在将音频平移 {shift_seconds} 秒...") |
| if shift_seconds >= 0: |
| shifted_audio = CompositeAudioClip([audio.with_start(shift_seconds)]).with_duration(video.duration) |
| else: |
| advance_seconds = abs(shift_seconds) |
| if advance_seconds >= video.duration: |
| shifted_audio = CompositeAudioClip([]).with_duration(video.duration) |
| else: |
| trimmed_audio = audio.subclipped(advance_seconds, video.duration) |
| shifted_audio = CompositeAudioClip([trimmed_audio.with_start(0)]).with_duration(video.duration) |
| |
| |
| final_video = video.with_audio(shifted_audio) |
| |
| |
| print(f"💾 正在导出对抗样本: {output_path}") |
| final_video.write_videofile(output_path, codec="libx264", audio_codec="aac") |
| print("✅ 搞定!CT-DPO 负样本生成成功!") |
|
|
| |
| shift_audio( |
| "/home/ubuntu/uag_oops_data/uag_oops/34 Funny Kid Nominees - FailArmy Hall Of Fame (May 2017)9.mp4", |
| "/home/ubuntu/poisoned_output_delay2.mp4", |
| shift_seconds=2.0, |
| ) |
|
|
| |
| shift_audio( |
| "/home/ubuntu/uag_oops_data/uag_oops/34 Funny Kid Nominees - FailArmy Hall Of Fame (May 2017)9.mp4", |
| "/home/ubuntu/poisoned_output_early2.mp4", |
| shift_seconds=-2.0, |
| ) |