Skip to content

Commit 68c5011

Browse files
Merge pull request #2027 from ibtsam3301/master
added function to extract thumbnail from videos
2 parents 96d0eb6 + 24a682c commit 68c5011

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

ExtractThumbnailFromVideo/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Thumbnail Extractor
2+
3+
This Python function extracts a thumbnail frame from a video and saves it as an image file. It utilizes the OpenCV library to perform these operations. This README provides an overview of the function, its usage, and the required packages.
4+
5+
## Table of Contents
6+
- [Function Description](#function-description)
7+
- [Usage](#usage)
8+
- [Required Packages](#required-packages)
9+
10+
## Function Description
11+
12+
The `extract_thumbnail` function takes two parameters:
13+
14+
- `video_path` (str): The path to the input video file.
15+
- `frame_size` (tuple): A tuple containing the desired dimensions (width, height) for the thumbnail frame.
16+
17+
The function will raise an `Exception` if it fails to extract a frame from the video.
18+
19+
### Function Logic
20+
21+
1. The function opens the specified video file using OpenCV.
22+
2. It seeks to the middle frame by calculating the middle frame index.
23+
3. The frame is resized to the specified dimensions.
24+
4. The resized frame is saved as an image file with a filename derived from the video's base name.
25+
26+
## Usage
27+
28+
Here's an example of how to use the function:
29+
30+
```python
31+
from thumbnail_extractor import extract_thumbnail
32+
33+
# Extract a thumbnail from 'my_video.mp4' with dimensions (320, 240)
34+
extract_thumbnail('my_video.mp4', (320, 240))
35+
# Replace 'my_video.mp4' with the path to your own video file and (320, 240) with your desired thumbnail dimensions.
36+
37+
## Required Packages
38+
```
39+
To use this function, you need the following package:
40+
41+
- **OpenCV (cv2)**: You can install it using `pip`:
42+
43+
```shell
44+
pip install opencv-python
45+
```
46+
47+
This function is useful for generating thumbnail images from videos. It simplifies the process of creating video thumbnails for various applications.
48+
49+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import cv2
2+
import os
3+
4+
def extract_thumbnail(video_path, frame_size):
5+
"""
6+
Extracts a thumbnail frame from a video and saves it as an image file.
7+
8+
Args:
9+
video_path (str): The path to the input video file.
10+
frame_size (tuple): A tuple containing the desired dimensions (width, height) for the thumbnail frame.
11+
12+
Raises:
13+
Exception: If the function fails to extract a frame from the video.
14+
15+
The function opens the specified video file, seeks to the middle frame,
16+
resizes the frame to the specified dimensions, and saves it as an image
17+
file with a filename derived from the video's base name.
18+
19+
Example:
20+
extract_thumbnail('my_video.mp4', (320, 240))
21+
22+
Required Packages:
23+
cv2 (pip install cv2)
24+
25+
This function is useful for generating thumbnail images from videos.
26+
"""
27+
video_capture = cv2.VideoCapture(video_path) # Open the video file for reading
28+
total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) # Get the total number of frames in the video
29+
middle_frame_index = total_frames // 2 # Calculate the index of the middle frame
30+
video_capture.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_index) # Seek to the middle frame
31+
success, frame = video_capture.read() # Read the middle frame
32+
video_capture.release() # Release the video capture object
33+
34+
if success:
35+
frame = cv2.resize(frame, frame_size) # Resize the frame to the specified dimensions
36+
thumbnail_filename = f"{os.path.basename(video_path)}_thumbnail.jpg" # Create a filename for the thumbnail
37+
cv2.imwrite(thumbnail_filename, frame) # Save the thumbnail frame as an image
38+
else:
39+
raise Exception("Could not extract frame") # Raise an exception if frame extraction fails

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Feel free to explore the scripts and use them for your learning and automation n
5252
38. [Images Downloader](https://git.io/JvnJh) - Download images from webpages on Unix-based systems.
5353
39. [space_invader.py.py](https://github.com/meezan-mallick/space_invader_game) - Classical 2D space invader game to recall your childhood memories.
5454
40. [Test Case Generator](https://github.com/Tanmay-901/test-case-generator/blob/master/test_case.py) - Generate different types of test cases with a clean and friendly UI, used in competitive programming and software testing.
55-
55+
41. [Extract Thumbnail From Video](https://github.com/geekcomputers/Python/tree/ExtractThumbnailFromVideo) - Extract Thumbnail from video files
5656
<hr>
5757

5858
_**Note**: The content in this repository belongs to the respective authors and creators. I'm just providing a formatted README.md for better presentation._

0 commit comments

Comments
 (0)