File size: 2,021 Bytes
b0dae94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gradio as gr
import torch
import os
import numpy as np
import ffmpeg
from moviepy.editor import AudioFileClip, ImageSequenceClip
from diffusers import StableDiffusionPipeline
from tempfile import TemporaryDirectory

# Load AI Image Generation Model (Stable Diffusion)
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device)

def generate_frames(prompt, num_frames=30):
    """Generate a series of images based on the given prompt."""
    images = []
    for i in range(num_frames):
        image = pipe(prompt).images[0]
        images.append(image)
    return images

def create_music_video(song, prompt):
    """Generate AI-based video from the uploaded song."""
    
    with TemporaryDirectory() as temp_dir:
        audio_path = os.path.join(temp_dir, "song.mp3")
        video_path = os.path.join(temp_dir, "output.mp4")

        # Save uploaded song
        with open(audio_path, "wb") as f:
            f.write(song)

        # Extract duration
        audio_clip = AudioFileClip(audio_path)
        duration = audio_clip.duration
        fps = 10
        num_frames = int(duration * fps)

        # Generate images
        images = generate_frames(prompt, num_frames)

        # Create video from images
        image_sequence_clip = ImageSequenceClip([np.array(img) for img in images], fps=fps)
        image_sequence_clip = image_sequence_clip.set_audio(audio_clip)
        image_sequence_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")

        return video_path

# Gradio UI
interface = gr.Interface(
    fn=create_music_video,
    inputs=[
        gr.Audio(type="file", label="Upload Song"),
        gr.Textbox(label="Describe the Music Video (Prompt)")
    ],
    outputs=gr.Video(label="Generated AI Music Video"),
    title="AI Music Video Generator",
    description="Upload a song and describe the music video you want. AI will generate a video using Stable Diffusion.",
)

# Launc