Spaces:
Sleeping
Sleeping
from moviepy.editor import * | |
import os | |
def merge_audio_image(mp3_path, image_path, output_dir, unique_id): | |
# Load the image | |
image_clip = ImageClip(image_path) | |
# Load the audio | |
audio_clip = AudioFileClip(mp3_path) | |
# Set the duration of the image clip to match the audio duration | |
image_clip = image_clip.set_duration(audio_clip.duration) | |
# Resize the image clip to Instagram's square dimensions (1080, 1080) | |
image_clip = image_clip.resize((1080, 1080)) | |
# Set the audio to the image clip | |
final_clip = image_clip.set_audio(audio_clip) | |
# Generate output file path | |
output_path = os.path.join(output_dir, f"{unique_id}.mp4") | |
# Write the output video file | |
final_clip.write_videofile(output_path, codec='libx264', audio_codec='aac') | |
return output_path | |
# Example usage: | |
def main(): | |
# Example paths (adjust as necessary) | |
processed_audio_storage = {} | |
unique_id = 'example_id' | |
output_path1 = '/path/to/processed_audio.mp3' # Replace with actual path | |
processed_audio_storage[unique_id] = output_path1 | |
mp3_path = processed_audio_storage[unique_id] | |
image_path = 'singer.jpg' # Ensure this image is in the same directory as this script | |
output_dir = './' # Or specify a different directory | |
# Generate MP4 and get the output path | |
mp4_path = merge_audio_image(mp3_path, image_path, output_dir, unique_id) | |
print(f"Generated video at: {mp4_path}") | |
if __name__ == '__main__': | |
main() | |