Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spanishgum/cc1dedb4f06096898f764e675c9b3e9c to your computer and use it in GitHub Desktop.
Save spanishgum/cc1dedb4f06096898f764e675c9b3e9c to your computer and use it in GitHub Desktop.
Using moviepy v2.x to dub an audio clip over a video clip
from argparse import ArgumentParser
from moviepy import VideoFileClip, AudioFileClip
def dub_video(video_path: str, audio_path: str, audio_start: int, output_path: str):
"""
Dubs audio over a video clip.
Args:
video_path (str): Path to the video file.
audio_path (str): Path to the audio file.
audio_start (int): Where in the audio file to start from.
output_path (str): Path to save the output video file.
"""
try:
video_clip = VideoFileClip(video_path)
audio_clip = AudioFileClip(audio_path)
audio_clip = audio_clip.subclipped(audio_start, audio_start + video_clip.duration)
final_clip: VideoFileClip = video_clip.with_audio(audio_clip)
final_clip.write_videofile(filename=output_path)
print(f"Video with dubbing saved to {output_path}")
except Exception as e:
print(f"Error (video: {video_path}, audio: {audio_path}, output: {output_path}): {e}")
if __name__ == '__main__':
parser = ArgumentParser(description="Dub a video with a new audio track.")
parser.add_argument("-v", "--video", type=str, required=True, help="Path to input video (e.g., input.mp4)")
parser.add_argument("-a", "--audio", type=str, required=True, help="Path to input audio (e.g., dub.mp3)")
parser.add_argument("-s", "--start", type=int, default=0, help="Where to start audio extraction in s (e.g., 82)")
parser.add_argument("-o", "--output", type=str, default="output.mp4", help="Path to output dubbed video (default: output.mp4)")
args = parser.parse_args()
dub_video(args.video, args.audio, args.start, args.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment