Created
August 5, 2023 20:48
-
-
Save JupyterJones/11ea0401c9514ecdd71d2666bc8af7b2 to your computer and use it in GitHub Desktop.
Use Moviepy to fade in and fade out music on mp4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from moviepy.editor import * | |
from moviepy.audio.fx.all import audio_fadein | |
from MUSIC import music | |
# Load video and audio clips | |
video_clip = VideoFileClip("/home/jack/Desktop/StoryMaker/static/images/Elektra-Weapons/Final_End.mp4") | |
audio_clip = AudioFileClip(music()) | |
# Ensure the audio clip is the same duration as the video | |
if audio_clip.duration > video_clip.duration: | |
audio_clip = audio_clip.subclip(0, video_clip.duration) | |
else: | |
padding_duration = video_clip.duration - audio_clip.duration | |
padding = AudioFileClip.silence(duration=padding_duration) | |
audio_clip = concatenate_audioclips([audio_clip, padding]) | |
# Define fade durations | |
fade_in_duration = 10 # seconds | |
fade_out_duration = 15 # seconds | |
# Apply fade in effect to audio | |
faded_audio = audio_clip.audio_fadein(fade_in_duration) | |
# Apply fade out effect to audio at the end | |
final_audio = faded_audio.audio_fadeout(fade_out_duration) | |
# Set audio of the video to the processed audio | |
video_with_audio = video_clip.set_audio(final_audio) | |
# Write the final video with audio to a file | |
output_path = "/home/jack/Desktop/StoryMaker/static/images/Elektra-Weapons/Final_End_Zoomed.mp4" | |
video_with_audio.write_videofile(output_path, codec="libx264") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment