I am trying to separate a song into 4 parts and slide the parts in random parts of a video. The problem with my code is that the final output video is muted. I want to play parts of the song at random intervals and while the song is playing the original video shall be muted. Thanks to everyone who helps
import random
from moviepy.editor import *
def split_audio_into_parts(mp3_path, num_parts):
audio = AudioFileClip(mp3_path)
duration = audio.duration
part_duration = duration / num_parts
parts = []
for i in range(num_parts):
start_time = i * part_duration
end_time = start_time + part_duration if i < num_parts - 1 else duration
part = audio.subclip(start_time, end_time)
parts.append(part)
return parts
def split_video_into_segments(video_path, num_segments):
video = VideoFileClip(video_path)
duration = video.duration
segment_duration = duration / num_segments
segments = []
for i in range(num_segments):
start_time = i * segment_duration
end_time = start_time + segment_duration if i < num_segments - 1 else duration
segment = video.subclip(start_time, end_time)
segments.append(segment)
return segments
def insert_audio_into_segments(segments, audio_parts):
modified_segments = []
for segment, audio_part in zip(segments, audio_parts):
audio_part = audio_part.volumex(0) # Mute the audio part
modified_segment = segment.set_audio(audio_part)
modified_segments.append(modified_segment)
return modified_segments
def combine_segments(segments):
final_video = concatenate_videoclips(segments)
return final_video
# Example usage
mp3_file_path = "C:/Users/Kris/PycharmProjects/videoeditingscript124234/DENKATA - Podvodnica Demo (1).mp3"
video_file_path = "C:/Users/Kris/PycharmProjects/videoeditingscript124234/family.guy.s21e13.1080p.web.h264-cakes[eztv.re].mkv"
num_parts = 4
audio_parts = split_audio_into_parts(mp3_file_path, num_parts)
segments = split_video_into_segments(video_file_path, num_parts)
segments = insert_audio_into_segments(segments, audio_parts)
final_video = combine_segments(segments)
final_video.write_videofile("output.mp4", codec="libx264", audio_codec="aac")
I tried entering most stuff into chatGPT and asking questions around forums but without sucess, so lets hope I can see my solution here