What I’m trying to do:
Allow the user to upload a video or an image using the File Loader component. Then, show a thumbnail of the video or image uploaded.
What I’ve tried and what’s not working:
It works as expected when it’s an image, but I don’t really know what to show when it’s a video.
import cv2
def save_video_frame(video_path: str, frame_path: str, frame_number: int) -> bool:
"""
Extract a specific frame from a video and save as an image
Args:
video_path: path to video file
frame_path: path to frame file
frame_number: frame number to extract from video
Returns:
If the save was successful
"""
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Cannot open video")
return False
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number - 1)
success, frame = cap.read()
if success:
cv2.imwrite(frame_path, frame)
else:
print(f"Failed to read frame {frame_number}")
cap.release()
return success
I just did a minimal alteration: to get a better thumbnail, I seeked 15s into the video using video_dom.currentTime = 15 and made the function to generate the thumbnail be inside the onseeked callback of video_dom.