Spaces:
Sleeping
Sleeping
update
Browse files- app.py +40 -2
- packages.txt +1 -0
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -27,6 +27,31 @@ from PIL import Image
|
|
| 27 |
cached_latest_posts_df = None
|
| 28 |
last_fetched = None
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def get_latest_pots():
|
| 32 |
global cached_latest_posts_df
|
|
@@ -107,15 +132,28 @@ with gr.Blocks() as demo:
|
|
| 107 |
lines=1, placeholder="Post url or id here", label="URL or Post ID"
|
| 108 |
)
|
| 109 |
load_btn = gr.Button("Load")
|
| 110 |
-
video_player = gr.Video()
|
| 111 |
|
| 112 |
with gr.Column():
|
| 113 |
gr.Markdown("## Latest Posts")
|
| 114 |
latest_post_dataframe = gr.Dataframe()
|
| 115 |
get_latest_pots_btn = gr.Button("Refresh Latest Posts")
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
get_latest_pots_btn.click(get_latest_pots, outputs=[latest_post_dataframe])
|
| 120 |
demo.load(get_latest_pots, outputs=[latest_post_dataframe])
|
| 121 |
|
|
|
|
| 27 |
cached_latest_posts_df = None
|
| 28 |
last_fetched = None
|
| 29 |
|
| 30 |
+
from moviepy.editor import VideoFileClip
|
| 31 |
+
import numpy as np
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def extract_frames_moviepy(video_url, num_frames=10):
|
| 35 |
+
try:
|
| 36 |
+
print(f"Extracting {num_frames} frames from {video_url}")
|
| 37 |
+
# Load the video
|
| 38 |
+
clip = VideoFileClip(video_url)
|
| 39 |
+
|
| 40 |
+
# Calculate the times to extract frames
|
| 41 |
+
duration = clip.duration
|
| 42 |
+
frame_times = np.linspace(0, duration, num_frames, endpoint=False)
|
| 43 |
+
|
| 44 |
+
frameas = []
|
| 45 |
+
# Extract and save frames
|
| 46 |
+
for i, time in enumerate(frame_times):
|
| 47 |
+
frame = clip.get_frame(time)
|
| 48 |
+
frame_image = Image.fromarray(frame)
|
| 49 |
+
frameas.append(frame_image)
|
| 50 |
+
|
| 51 |
+
return frameas
|
| 52 |
+
except e as Exception:
|
| 53 |
+
raise gr.Error(f"Error extracting frames from video: {e}")
|
| 54 |
+
|
| 55 |
|
| 56 |
def get_latest_pots():
|
| 57 |
global cached_latest_posts_df
|
|
|
|
| 132 |
lines=1, placeholder="Post url or id here", label="URL or Post ID"
|
| 133 |
)
|
| 134 |
load_btn = gr.Button("Load")
|
| 135 |
+
video_player = gr.Video(interactive=False)
|
| 136 |
|
| 137 |
with gr.Column():
|
| 138 |
gr.Markdown("## Latest Posts")
|
| 139 |
latest_post_dataframe = gr.Dataframe()
|
| 140 |
get_latest_pots_btn = gr.Button("Refresh Latest Posts")
|
| 141 |
|
| 142 |
+
with gr.Column():
|
| 143 |
+
gr.Markdown("## Sampled Frames from Video")
|
| 144 |
+
with gr.Row():
|
| 145 |
+
num_frames = gr.Slider(minimum=1, maximum=20, step=1, value=10)
|
| 146 |
+
sample_btn = gr.Button("Sample")
|
| 147 |
+
|
| 148 |
+
sampled_frames = gr.Gallery()
|
| 149 |
|
| 150 |
+
sample_btn.click(
|
| 151 |
+
extract_frames_moviepy,
|
| 152 |
+
inputs=[video_player, num_frames],
|
| 153 |
+
outputs=[sampled_frames],
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
load_btn.click(load_video, inputs=[reddit_id], outputs=[video_player])
|
| 157 |
get_latest_pots_btn.click(get_latest_pots, outputs=[latest_post_dataframe])
|
| 158 |
demo.load(get_latest_pots, outputs=[latest_post_dataframe])
|
| 159 |
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ffmpy
|
| 2 |
+
moviepy
|