Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from moviepy.editor import ImageClip, concatenate_videoclips, TextClip, CompositeVideoClip
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="Image to Video Creator", layout="wide")
|
| 9 |
+
st.title("🖼️➡️🎞️ Image to Video Generator with Context")
|
| 10 |
+
|
| 11 |
+
uploaded_files = st.file_uploader("Upload Images", type=["jpg", "png", "jpeg"], accept_multiple_files=True)
|
| 12 |
+
|
| 13 |
+
context_list = []
|
| 14 |
+
|
| 15 |
+
if uploaded_files:
|
| 16 |
+
st.subheader("📝 Enter Context or Caption for Each Image")
|
| 17 |
+
|
| 18 |
+
for i, img_file in enumerate(uploaded_files):
|
| 19 |
+
st.image(img_file, width=200, caption=f"Image {i+1}")
|
| 20 |
+
context = st.text_area(f"Context for Image {i+1}", key=f"context_{i}")
|
| 21 |
+
context_list.append(context)
|
| 22 |
+
|
| 23 |
+
duration = st.slider("Duration per image (seconds)", 2, 10, 4)
|
| 24 |
+
|
| 25 |
+
if st.button("Create Video"):
|
| 26 |
+
with st.spinner("Generating video..."):
|
| 27 |
+
temp_dir = tempfile.mkdtemp()
|
| 28 |
+
clips = []
|
| 29 |
+
|
| 30 |
+
for i, img_file in enumerate(uploaded_files):
|
| 31 |
+
# Save image temporarily
|
| 32 |
+
img = Image.open(img_file).convert("RGB")
|
| 33 |
+
img_path = os.path.join(temp_dir, f"img_{i}.png")
|
| 34 |
+
img.save(img_path)
|
| 35 |
+
|
| 36 |
+
# Create ImageClip
|
| 37 |
+
image_clip = ImageClip(img_path).set_duration(duration)
|
| 38 |
+
|
| 39 |
+
# Add context text if any
|
| 40 |
+
if context_list[i]:
|
| 41 |
+
txt_clip = TextClip(context_list[i], fontsize=24, color='white', bg_color='black', size=image_clip.size)
|
| 42 |
+
txt_clip = txt_clip.set_duration(duration).set_position(("center", "bottom"))
|
| 43 |
+
final_clip = CompositeVideoClip([image_clip, txt_clip])
|
| 44 |
+
else:
|
| 45 |
+
final_clip = image_clip
|
| 46 |
+
|
| 47 |
+
clips.append(final_clip)
|
| 48 |
+
|
| 49 |
+
final_video = concatenate_videoclips(clips, method="compose")
|
| 50 |
+
output_path = os.path.join(temp_dir, "final_video.mp4")
|
| 51 |
+
final_video.write_videofile(output_path, fps=24)
|
| 52 |
+
|
| 53 |
+
with open(output_path, "rb") as file:
|
| 54 |
+
video_bytes = file.read()
|
| 55 |
+
st.video(video_bytes)
|
| 56 |
+
st.download_button("Download Video", data=video_bytes, file_name="image_video.mp4", mime="video/mp4")
|
| 57 |
+
|
| 58 |
+
st.success("✅ Video created successfully!")
|
| 59 |
+
|
| 60 |
+
else:
|
| 61 |
+
st.info("Upload some images to begin.")
|
| 62 |
+
|