Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import tempfile
|
4 |
+
import cv2
|
5 |
+
import imageio
|
6 |
+
|
7 |
+
# Function to extract frames and create GIF
|
8 |
+
def extract_frames(video_path, output_path, start_time, end_time, speed_multiplier):
|
9 |
+
vidcap = cv2.VideoCapture(video_path)
|
10 |
+
|
11 |
+
# Get the FPS of the video
|
12 |
+
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
13 |
+
|
14 |
+
# Calculate the start and end frames
|
15 |
+
start_frame = int(start_time * fps)
|
16 |
+
end_frame = int(end_time * fps)
|
17 |
+
|
18 |
+
vidcap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
|
19 |
+
success, frame = vidcap.read()
|
20 |
+
current_frame = start_frame
|
21 |
+
frames = []
|
22 |
+
|
23 |
+
while success and current_frame <= end_frame:
|
24 |
+
frames.append(frame)
|
25 |
+
current_frame += 1
|
26 |
+
success, frame = vidcap.read()
|
27 |
+
|
28 |
+
if speed_multiplier != 1.0:
|
29 |
+
frames = frames[::int(speed_multiplier)]
|
30 |
+
|
31 |
+
# Save frames as GIF
|
32 |
+
rgb_frames = [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in frames]
|
33 |
+
imageio.mimsave(output_path, rgb_frames, fps=fps/speed_multiplier)
|
34 |
+
|
35 |
+
|
36 |
+
# Streamlit app
|
37 |
+
st.title("Automatic GIF Creator from Videos")
|
38 |
+
|
39 |
+
uploaded_file = st.file_uploader("Choose a video file...", type=["mp4", "avi", "mov", "mkv"])
|
40 |
+
|
41 |
+
if uploaded_file is not None:
|
42 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_video:
|
43 |
+
temp_video.write(uploaded_file.read())
|
44 |
+
temp_video_path = temp_video.name
|
45 |
+
|
46 |
+
# Preview the uploaded video
|
47 |
+
st.video(temp_video_path)
|
48 |
+
|
49 |
+
start_time = st.number_input("Start Time (seconds)", min_value=0.0, value=0.0, step=0.1)
|
50 |
+
end_time = st.number_input("End Time (seconds)", min_value=0.1, value=5.0, step=0.1)
|
51 |
+
speed_multiplier = st.number_input("Speed Multiplier", min_value=0.1, max_value=10.0, value=1.0)
|
52 |
+
|
53 |
+
if st.button("Create GIF"):
|
54 |
+
output_gif_path = temp_video_path + ".gif"
|
55 |
+
|
56 |
+
st.write("Creating GIF...")
|
57 |
+
extract_frames(temp_video_path, output_gif_path, start_time, end_time, speed_multiplier)
|
58 |
+
|
59 |
+
st.write("GIF created successfully!")
|
60 |
+
|
61 |
+
gif = Image.open(output_gif_path)
|
62 |
+
st.image(gif, caption="Generated GIF")
|
63 |
+
|
64 |
+
# Read the GIF file as bytes
|
65 |
+
with open(output_gif_path, "rb") as file:
|
66 |
+
gif_bytes = file.read()
|
67 |
+
|
68 |
+
# Provide a download button for the GIF
|
69 |
+
st.download_button(
|
70 |
+
label="Download GIF",
|
71 |
+
data=gif_bytes,
|
72 |
+
file_name="output.gif",
|
73 |
+
mime="image/gif"
|
74 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
imageio
|
3 |
+
opencv-python-headless
|