File size: 1,868 Bytes
fca0a08
ba3b05a
fca0a08
 
ba3b05a
fca0a08
ba3b05a
 
fca0a08
ba3b05a
fca0a08
ba3b05a
fca0a08
 
ba3b05a
 
 
 
 
 
 
 
 
fca0a08
 
 
ba3b05a
 
 
fca0a08
 
ba3b05a
fca0a08
ba3b05a
 
 
 
fca0a08
ba3b05a
fca0a08
 
ba3b05a
 
fca0a08
ba3b05a
 
fca0a08
ba3b05a
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
import streamlit as st
from moviepy.editor import *
from PIL import Image
import tempfile
import os

st.set_page_config(page_title="Image to Video", layout="centered")
st.title("🖼️➡️🎞️ Convert Images to Video with Captions")

uploaded_files = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)

duration = st.slider("Duration per image (sec)", 2, 10, 4)

if uploaded_files:
    captions = []
    st.subheader("Add a caption for each image:")
    for i, image_file in enumerate(uploaded_files):
        st.image(image_file, width=150)
        cap = st.text_input(f"Caption for image {i+1}", key=f"caption_{i}")
        captions.append(cap)

    if st.button("Generate Video"):
        with st.spinner("Processing..."):
            temp_dir = tempfile.mkdtemp()
            clips = []

            for i, file in enumerate(uploaded_files):
                img = Image.open(file).convert("RGB")
                img_path = os.path.join(temp_dir, f"img_{i}.jpg")
                img.save(img_path)

                clip = ImageClip(img_path).set_duration(duration)

                if captions[i]:
                    txt = TextClip(captions[i], fontsize=24, color='white', bg_color='black', size=clip.size)
                    txt = txt.set_duration(duration).set_position(("center", "bottom"))
                    clip = CompositeVideoClip([clip, txt])

                clips.append(clip)

            final_video = concatenate_videoclips(clips, method="compose")
            video_path = os.path.join(temp_dir, "output.mp4")
            final_video.write_videofile(video_path, fps=24)

            with open(video_path, "rb") as f:
                video_bytes = f.read()
                st.video(video_bytes)
                st.download_button("Download Video", video_bytes, file_name="video.mp4", mime="video/mp4")