hathibelagal commited on
Commit
7633e59
·
verified ·
1 Parent(s): 6ff3b50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -53
app.py CHANGED
@@ -1,59 +1,55 @@
1
  import gradio as gr
 
 
2
  from PIL import Image
3
- import subprocess
4
- import tempfile
5
- import os
6
 
7
- def extract_last_frame_fast(video_path):
 
8
  if not video_path:
9
- return None
10
-
11
- # Create a temporary file for the output frame
12
- with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
13
- output_frame = tmp.name
14
-
15
- # This ffmpeg command is magic: seeks near the end instantly without decoding everything
16
- cmd = [
17
- "ffmpeg",
18
- "-i", video_path,
19
- "-sseof", "-3", # start 3 seconds before the end
20
- "-frames:v", "1", # extract exactly 1 frame
21
- "-update", "1",
22
- "-q:v", "3", # good quality
23
- output_frame,
24
- "-y"
25
- ]
26
-
27
- # Run silently and fast
28
- result = subprocess.run(
29
- cmd,
30
- stdout=subprocess.PIPE,
31
- stderr=subprocess.PIPE,
32
- )
33
-
34
- if result.returncode != 0 or not os.path.exists(output_frame):
35
- # Fallback: try one more time at -1 second
36
- cmd[4] = "-1"
37
- subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38
-
39
- if os.path.exists(output_frame) and os.path.getsize(output_frame) > 0:
40
- img = Image.open(output_frame)
41
- os.unlink(output_frame) # cleanup
42
- return img
43
- else:
44
- os.unlink(output_frame)
45
- return None # Gradio will show broken image icon
46
-
47
- # Ultra-fast interface
48
- demo = gr.Interface(
49
- fn=extract_last_frame_fast,
50
- inputs=gr.Video(label="Upload Video (max 6 MB)"),
51
- ),
52
- outputs=gr.Image(label="Last Frame"),
53
- title="Instant Last Frame Extractor",
54
- description="Works instantly even on 6 MB videos!",
55
- allow_flagging="never",
56
- cache_examples=False
57
  )
58
 
59
- demo.launch(max_file_size="6MB") # your limit stays exactly 6 MB
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
  from PIL import Image
 
 
 
5
 
6
+ def extract_last_frame(video_path):
7
+ # Check if video_path is None or invalid
8
  if not video_path:
9
+ return "Error: No video file uploaded."
10
+
11
+ try:
12
+ # Open the video file
13
+ cap = cv2.VideoCapture(video_path)
14
+
15
+ # Check if video opened successfully
16
+ if not cap.isOpened():
17
+ return "Error: Could not open video file."
18
+
19
+ # Get the total number of frames
20
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
21
+
22
+ # Set the frame position to the last frame
23
+ cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
24
+
25
+ # Read the last frame
26
+ ret, frame = cap.read()
27
+
28
+ # Release the video capture object
29
+ cap.release()
30
+
31
+ if not ret:
32
+ return "Error: Could not read the last frame."
33
+
34
+ # Convert the frame from BGR (OpenCV format) to RGB
35
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
36
+
37
+ # Convert to PIL Image for Gradio compatibility
38
+ pil_image = Image.fromarray(frame_rgb)
39
+
40
+ return pil_image
41
+
42
+ except Exception as e:
43
+ return f"Error: {str(e)}"
44
+
45
+ # Create the Gradio interface
46
+ interface = gr.Interface(
47
+ fn=extract_last_frame, # Function to call
48
+ inputs=gr.Video(label="Upload Video"), # Input component for video upload
49
+ outputs=gr.Image(label="Last Frame"), # Output component for displaying the image
50
+ title="Extract Last Frame from Video",
51
+ description="Upload a video file to extract and display its last frame."
 
 
 
 
 
52
  )
53
 
54
+ # Launch the Gradio app
55
+ interface.launch(max_file_size="6MB")