Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import cv2 | |
| import tempfile | |
| import os | |
| from PIL import Image | |
| st.set_page_config(page_title="AI Thumbnail Generator", layout="centered") | |
| st.title("π― AI-Based Thumbnail Generator") | |
| st.write("Upload a video to extract thumbnail-worthy frames using AI.") | |
| uploaded_video = st.file_uploader("Upload Video File", type=["mp4", "mov", "avi", "mkv", "wmv"]) | |
| if uploaded_video is not None: | |
| st.video(uploaded_video) | |
| st.success("β Video uploaded successfully!") | |
| # Save the uploaded video to a temp file | |
| tfile = tempfile.NamedTemporaryFile(delete=False) | |
| tfile.write(uploaded_video.read()) | |
| # Extract frames using OpenCV | |
| vidcap = cv2.VideoCapture(tfile.name) | |
| success, image = vidcap.read() | |
| count = 0 | |
| thumbnail_frame = None | |
| while success: | |
| if count % 60 == 0: # extract every 2 seconds assuming 30 FPS | |
| frame_path = f"frame_{count}.jpg" | |
| cv2.imwrite(frame_path, image) | |
| thumbnail_frame = frame_path # keep last extracted frame as mock thumbnail | |
| success, image = vidcap.read() | |
| count += 1 | |
| if thumbnail_frame: | |
| st.subheader("Generated Thumbnail Frame") | |
| image = Image.open(thumbnail_frame) | |
| st.image(image, caption="Best Frame", use_column_width=True) | |
| with open(thumbnail_frame, "rb") as file: | |
| st.download_button("π₯ Download Thumbnail", file, file_name="thumbnail.jpg") | |