import os
import random
from moviepy.editor import VideoFileClip, AudioFileClip, ImageClip, CompositeVideoClip
def overlay_image_on_video(image_directory, video_directory, audio_directory, output_directory, image_width=1000, video_width=1080, duration=7):
if not os.path.isdir(image_directory):
print(f"The image directory {image_directory} does not exist.")
return
if not os.path.isdir(video_directory):
print(f"The video directory {video_directory} does not exist.")
return
if not os.path.isdir(audio_directory):
print(f"The audio directory {audio_directory} does not exist.")
return
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# List all video files
video_files = [os.path.join(video_directory, f) for f in os.listdir(video_directory) if f.lower().endswith(('mp4'))]
if not video_files:
print(f"No videos found in {video_directory}.")
return
# List all image files
image_files = [os.path.join(image_directory, f) for f in os.listdir(image_directory) if f.lower().endswith(('png', 'jpg', 'jpeg', 'bmp', 'gif'))]
if not image_files:
print(f"No images found in {image_directory}.")
return
# List all audio files
audio_files = [os.path.join(audio_directory, f) for f in os.listdir(audio_directory) if f.lower().endswith(('mp3', 'm4a', 'wav', 'ogg'))]
if not audio_files:
print(f"No audio files found in {audio_directory}.")
return
# Randomly select one image, one video, and one audio
image_path = random.choice(image_files)
video_path = random.choice(video_files)
audio_path = random.choice(audio_files)
# Load the video and audio clips
video_clip = VideoFileClip(video_path).subclip(0, duration)
audio_clip = AudioFileClip(audio_path).subclip(0, duration)
img = ImageClip(image_path)
# Resize the image
img = img.resize(width=image_width)
# Calculate position to center the image vertically and with 40px padding on the sides
x_pos = (video_width - image_width) // 2
y_pos = (video_clip.h - img.h) // 2
# Set the image position, duration, and apply fade-in effect
img = img.set_position((x_pos, y_pos)).set_duration(video_clip.duration)
img = img.fadein(0.5)
# Overlay the image on the video
final_clip = CompositeVideoClip([video_clip, img])
final_clip = final_clip.set_audio(audio_clip)
# Save the final video
output_path = os.path.join(output_directory, os.path.basename(image_path).replace(".", "_") + ".mp4")
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=video_clip.fps)
print(f"Overlayed {os.path.basename(image_path)} on {os.path.basename(video_path)} with audio from {os.path.basename(audio_path)} and saved to {output_path}")
# Specify the directories
image_directory = 'scraped'
video_directory = 'vidyas'
audio_directory = 'audya'
output_directory = 'silo'
# Call the function to overlay a random image on a random video with random audio
overlay_image_on_video(image_directory, video_directory, audio_directory, output_directory)```
this is the code's script. rn, i'm facing issue with the fadein part
```py
# Set the image position, duration, and apply fade-in effect
img = img.set_position((x_pos, y_pos)).set_duration(video_clip.duration)
img = img.fadein(0.5)```
the problem is that fadein appears from black. But I don't see a way to make it appear from transparency instead.
Like, I can't find a way to edit the alpha channel
whatever attempt i've made thus far has been pointless
media attached is how rendered videos look like. there is a black box at the start which is annoying.
#🔒 Moviepy fadein effect alpha channel issue
4 messages · Page 1 of 1 (latest)
@pliant zealot
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
@pliant zealot
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.