Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,48 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from moviepy.editor import
|
| 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
|
| 9 |
-
st.title("🖼️➡️🎞️
|
| 10 |
|
| 11 |
-
uploaded_files = st.file_uploader("Upload Images", type=["jpg", "
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
if uploaded_files:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
for i,
|
| 19 |
-
st.image(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if st.button("Create Video"):
|
| 26 |
-
with st.spinner("Generating video..."):
|
| 27 |
temp_dir = tempfile.mkdtemp()
|
| 28 |
clips = []
|
| 29 |
|
| 30 |
-
for i,
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
img_path = os.path.join(temp_dir, f"img_{i}.png")
|
| 34 |
img.save(img_path)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
image_clip = ImageClip(img_path).set_duration(duration)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
final_clip = CompositeVideoClip([image_clip, txt_clip])
|
| 44 |
-
else:
|
| 45 |
-
final_clip = image_clip
|
| 46 |
|
| 47 |
-
clips.append(
|
| 48 |
|
| 49 |
final_video = concatenate_videoclips(clips, method="compose")
|
| 50 |
-
|
| 51 |
-
final_video.write_videofile(
|
| 52 |
|
| 53 |
-
with open(
|
| 54 |
-
video_bytes =
|
| 55 |
st.video(video_bytes)
|
| 56 |
-
st.download_button("Download Video",
|
| 57 |
-
|
| 58 |
-
st.success("✅ Video created successfully!")
|
| 59 |
-
|
| 60 |
-
else:
|
| 61 |
-
st.info("Upload some images to begin.")
|
| 62 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from moviepy.editor import *
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="Image to Video", layout="centered")
|
| 8 |
+
st.title("🖼️➡️🎞️ Convert Images to Video with Captions")
|
| 9 |
|
| 10 |
+
uploaded_files = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 11 |
|
| 12 |
+
duration = st.slider("Duration per image (sec)", 2, 10, 4)
|
| 13 |
|
| 14 |
if uploaded_files:
|
| 15 |
+
captions = []
|
| 16 |
+
st.subheader("Add a caption for each image:")
|
| 17 |
+
for i, image_file in enumerate(uploaded_files):
|
| 18 |
+
st.image(image_file, width=150)
|
| 19 |
+
cap = st.text_input(f"Caption for image {i+1}", key=f"caption_{i}")
|
| 20 |
+
captions.append(cap)
|
| 21 |
+
|
| 22 |
+
if st.button("Generate Video"):
|
| 23 |
+
with st.spinner("Processing..."):
|
|
|
|
|
|
|
| 24 |
temp_dir = tempfile.mkdtemp()
|
| 25 |
clips = []
|
| 26 |
|
| 27 |
+
for i, file in enumerate(uploaded_files):
|
| 28 |
+
img = Image.open(file).convert("RGB")
|
| 29 |
+
img_path = os.path.join(temp_dir, f"img_{i}.jpg")
|
|
|
|
| 30 |
img.save(img_path)
|
| 31 |
|
| 32 |
+
clip = ImageClip(img_path).set_duration(duration)
|
|
|
|
| 33 |
|
| 34 |
+
if captions[i]:
|
| 35 |
+
txt = TextClip(captions[i], fontsize=24, color='white', bg_color='black', size=clip.size)
|
| 36 |
+
txt = txt.set_duration(duration).set_position(("center", "bottom"))
|
| 37 |
+
clip = CompositeVideoClip([clip, txt])
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
clips.append(clip)
|
| 40 |
|
| 41 |
final_video = concatenate_videoclips(clips, method="compose")
|
| 42 |
+
video_path = os.path.join(temp_dir, "output.mp4")
|
| 43 |
+
final_video.write_videofile(video_path, fps=24)
|
| 44 |
|
| 45 |
+
with open(video_path, "rb") as f:
|
| 46 |
+
video_bytes = f.read()
|
| 47 |
st.video(video_bytes)
|
| 48 |
+
st.download_button("Download Video", video_bytes, file_name="video.mp4", mime="video/mp4")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|