Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions ExtractThumbnailFromVideo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Thumbnail Extractor

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.

## Table of Contents
- [Function Description](#function-description)
- [Usage](#usage)
- [Required Packages](#required-packages)

## Function Description

The `extract_thumbnail` function takes two parameters:

- `video_path` (str): The path to the input video file.
- `frame_size` (tuple): A tuple containing the desired dimensions (width, height) for the thumbnail frame.

The function will raise an `Exception` if it fails to extract a frame from the video.

### Function Logic

1. The function opens the specified video file using OpenCV.
2. It seeks to the middle frame by calculating the middle frame index.
3. The frame is resized to the specified dimensions.
4. The resized frame is saved as an image file with a filename derived from the video's base name.

## Usage

Here's an example of how to use the function:

```python
from thumbnail_extractor import extract_thumbnail

# Extract a thumbnail from 'my_video.mp4' with dimensions (320, 240)
extract_thumbnail('my_video.mp4', (320, 240))
# Replace 'my_video.mp4' with the path to your own video file and (320, 240) with your desired thumbnail dimensions.

## Required Packages
```
To use this function, you need the following package:

- **OpenCV (cv2)**: You can install it using `pip`:

```shell
pip install opencv-python
```

This function is useful for generating thumbnail images from videos. It simplifies the process of creating video thumbnails for various applications.


39 changes: 39 additions & 0 deletions ExtractThumbnailFromVideo/extract_thumbnail_from_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import cv2
import os

def extract_thumbnail(video_path, frame_size):
"""
Extracts a thumbnail frame from a video and saves it as an image file.

Args:
video_path (str): The path to the input video file.
frame_size (tuple): A tuple containing the desired dimensions (width, height) for the thumbnail frame.

Raises:
Exception: If the function fails to extract a frame from the video.

The function opens the specified video file, seeks to the middle frame,
resizes the frame to the specified dimensions, and saves it as an image
file with a filename derived from the video's base name.

Example:
extract_thumbnail('my_video.mp4', (320, 240))

Required Packages:
cv2 (pip install cv2)

This function is useful for generating thumbnail images from videos.
"""
video_capture = cv2.VideoCapture(video_path) # Open the video file for reading
total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) # Get the total number of frames in the video
middle_frame_index = total_frames // 2 # Calculate the index of the middle frame
video_capture.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_index) # Seek to the middle frame
success, frame = video_capture.read() # Read the middle frame
video_capture.release() # Release the video capture object

if success:
frame = cv2.resize(frame, frame_size) # Resize the frame to the specified dimensions
thumbnail_filename = f"{os.path.basename(video_path)}_thumbnail.jpg" # Create a filename for the thumbnail
cv2.imwrite(thumbnail_filename, frame) # Save the thumbnail frame as an image
else:
raise Exception("Could not extract frame") # Raise an exception if frame extraction fails
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Feel free to explore the scripts and use them for your learning and automation n
38. [Images Downloader](https://git.io/JvnJh) - Download images from webpages on Unix-based systems.
39. [space_invader.py.py](https://github.com/meezan-mallick/space_invader_game) - Classical 2D space invader game to recall your childhood memories.
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.

41. [Extract Thumbnail From Video](https://github.com/geekcomputers/Python/tree/ExtractThumbnailFromVideo) - Extract Thumbnail from video files
<hr>

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