Spaces:
Runtime error
Runtime error
arnabbumba077
commited on
Commit
•
ec2b971
1
Parent(s):
887cd57
Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import imageio
|
3 |
+
import cv2
|
4 |
+
import os
|
5 |
+
|
6 |
+
def create_gif(video_path, start_time, end_time, gif_path, speed_multiplier):
|
7 |
+
# Open the video file
|
8 |
+
video = cv2.VideoCapture(video_path)
|
9 |
+
|
10 |
+
# Get the FPS of the original video
|
11 |
+
fps = video.get(cv2.CAP_PROP_FPS)
|
12 |
+
|
13 |
+
# Calculate the frame numbers for start and end times
|
14 |
+
start_frame = int(start_time * fps)
|
15 |
+
end_frame = int(end_time * fps)
|
16 |
+
|
17 |
+
# Set the video to start at the start_frame
|
18 |
+
video.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
|
19 |
+
|
20 |
+
frames = []
|
21 |
+
success, frame = video.read()
|
22 |
+
current_frame = start_frame
|
23 |
+
|
24 |
+
while success and current_frame <= end_frame:
|
25 |
+
frames.append(frame)
|
26 |
+
current_frame += 1
|
27 |
+
success, frame = video.read()
|
28 |
+
|
29 |
+
video.release()
|
30 |
+
|
31 |
+
# Adjust the speed of the GIF
|
32 |
+
if speed_multiplier != 1.0:
|
33 |
+
frames = frames[::int(speed_multiplier)]
|
34 |
+
|
35 |
+
# Convert frames to RGB and save as GIF
|
36 |
+
rgb_frames = [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in frames]
|
37 |
+
imageio.mimsave(gif_path, rgb_frames, fps=fps/speed_multiplier)
|
38 |
+
|
39 |
+
def main():
|
40 |
+
st.title("Automatic GIF Creator from Videos")
|
41 |
+
|
42 |
+
uploaded_file = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov", "mkv"])
|
43 |
+
|
44 |
+
if uploaded_file is not None:
|
45 |
+
with open("temp_video.mp4", "wb") as f:
|
46 |
+
f.write(uploaded_file.getbuffer())
|
47 |
+
|
48 |
+
st.video("temp_video.mp4")
|
49 |
+
|
50 |
+
start_time = st.number_input("Start Time (seconds)", min_value=0.0, value=0.0)
|
51 |
+
end_time = st.number_input("End Time (seconds)", min_value=0.0, value=5.0)
|
52 |
+
speed_multiplier = st.number_input("Speed Multiplier", min_value=0.1, max_value=10.0, value=1.0)
|
53 |
+
|
54 |
+
if st.button("Create GIF"):
|
55 |
+
if end_time > start_time:
|
56 |
+
with st.spinner("Creating GIF..."):
|
57 |
+
gif_path = "output.gif"
|
58 |
+
create_gif("temp_video.mp4", start_time, end_time, gif_path, speed_multiplier)
|
59 |
+
st.success("GIF created successfully!")
|
60 |
+
st.image(gif_path)
|
61 |
+
|
62 |
+
# Create a download button for the GIF
|
63 |
+
with open(gif_path, "rb") as file:
|
64 |
+
st.download_button(
|
65 |
+
label="Download GIF",
|
66 |
+
data=file,
|
67 |
+
file_name="output.gif",
|
68 |
+
mime="image/gif"
|
69 |
+
)
|
70 |
+
else:
|
71 |
+
st.error("End time must be greater than start time")
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
moviepy
|
3 |
+
imageio[ffmpeg]
|
4 |
+
opencv-python
|