29 lines
935 B
Python
29 lines
935 B
Python
|
from moviepy.editor import VideoFileClip, AudioFileClip
|
||
|
import random
|
||
|
|
||
|
def combine_audio_video(audio_file, video_file, output_file):
|
||
|
# Load the audio and video files
|
||
|
audio = AudioFileClip(audio_file)
|
||
|
video = VideoFileClip(video_file)
|
||
|
|
||
|
# Get the duration of the audio and video files
|
||
|
audio_duration = audio.duration
|
||
|
video_duration = video.duration
|
||
|
|
||
|
# Generate a random start time for the video clip
|
||
|
start_time = random.uniform(0, video_duration - audio_duration)
|
||
|
|
||
|
# Trim the video to match the audio duration and start from the random point
|
||
|
trimmed_video = video.subclip(start_time, start_time + audio_duration)
|
||
|
|
||
|
# Combine the trimmed video and audio
|
||
|
final_clip = trimmed_video.set_audio(audio)
|
||
|
|
||
|
# Write the combined clip to a new video file
|
||
|
final_clip.write_videofile(output_file)
|
||
|
|
||
|
# Close the audio and video files
|
||
|
audio.close()
|
||
|
video.close()
|
||
|
final_clip.close()
|