Created
May 15, 2025 05:44
-
-
Save AalbatrossGuy/193d2db84066e2f1018c84d298937067 to your computer and use it in GitHub Desktop.
How to create thumbnail of a video in python using OpenCV & moviepy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dotenv import load_dotenv | |
| from moviepy import VideoFileClip | |
| import cv2 | |
| def create_thumbnail(file) -> None: | |
| video = VideoFileClip(f"{os.getenv('path/to/video.mp4')}") # Get the video from storage. | |
| filenoext = file.rsplit('.', 1)[0] # Get the filename without the extension ['mp4', 'mov', 'mkv', etc.] | |
| get_frame = video.get_frame(3) # Get the 3rd frame from the video. You can choose the frame you need. Getting a single frame from the video helps in memory efficiency as the video doesn't need to be loaded in memory completely. | |
| cv2.imwrite(f"{os.getenv('path/to/save/image.jpg')}", get_frame) # Save the frame to the desired path. | |
| # Don't load the whole video in memory unless it's a lightweight video. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment