-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_speech_overlap_video.py
More file actions
64 lines (50 loc) · 2.17 KB
/
Copy pathrun_speech_overlap_video.py
File metadata and controls
64 lines (50 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import os
import glob
import sys
from moviepy.editor import VideoFileClip, AudioFileClip
from multiprocessing import Pool, cpu_count
os.environ["IMAGEIO_FFMPEG_EXE"] = "ffmpeg"
video_base_dir = sys.argv[1]
audio_base_dir = sys.argv[2]
output_base_dir = sys.argv[3]
# Function to overlay audio on video
def overlay_audio_on_video(audio_path):
try:
# Extract components from audio filename
filename = os.path.basename(audio_path)
parts = filename.split("-")
mp4_file = parts[-2] # Second-to-last part as mp4 filename (e.g., 00485)
mp4_file_1 = parts[-1].split("_")[0]
# Construct video path based on extracted components
video_path = os.path.join(video_base_dir, mp4_file, f"{mp4_file_1}.mp4")
# Check if the video file exists before proceeding
if not os.path.exists(video_path):
print(f"Video file not found: {video_path}")
return
# Load the video and audio clips
video_clip = VideoFileClip(video_path)
new_audio_clip = AudioFileClip(audio_path)
# Set the new audio to the video
video_with_new_audio = video_clip.set_audio(new_audio_clip)
# Construct the output path to mirror the video path structure
output_path = os.path.join(output_base_dir, f"{mp4_file}_{mp4_file_1}.mp4")
# Ensure the output directory exists
os.makedirs(os.path.dirname(output_path), exist_ok=True)
# Write the new video file with the new audio
video_with_new_audio.write_videofile(
output_path, codec="libx264", audio_codec="aac"
)
print(f"New video saved at {output_path}")
except Exception as e:
print(f"Failed to process {audio_path}: {e}")
if __name__ == "__main__":
# Get all normalized wav files in the specified directory
audio_files = glob.glob(os.path.join(audio_base_dir, "*_normalized.wav"))
num_cores = cpu_count()
# Use multiprocessing to process each audio file in parallel
with Pool(processes=num_cores) as pool:
pool.map(overlay_audio_on_video, audio_files)