Created
July 12, 2021 18:58
-
-
Save ZackAkil/ded5d97ae644b35d75e4e4f8364c0a2a to your computer and use it in GitHub Desktop.
Using CV2 generate folders of thumbnails given a folder of videos.
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
# given a folder of videos, generate thumbnails for each video and store them in a new folder structure | |
# !pip install opencv-python | |
# !sudo apt-get install ffmpeg libsm6 libxext6 -y | |
import cv2 | |
import os | |
from PIL import Image | |
def generate_thumbnails(video_file_name, thumbnail_directory): | |
print(video_file_name, thumbnail_directory) | |
skip_frames = 30 | |
video = cv2.VideoCapture(video_file_name) | |
good_frame, frame = video.read() | |
frame_count = 0 | |
thumb_count = 0 | |
while good_frame: | |
frame_count += 1 | |
if (frame_count % skip_frames) == 0: | |
thumb_count += 1 | |
print('saving frame ', thumb_count) | |
rbg_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
Image.fromarray(rbg_img).save(thumbnail_directory + '/{}.png'.format(thumb_count)) | |
good_frame, frame = video.read() | |
# create folders to store video thumbnails | |
os.mkdir('thumbnails') | |
for filename in os.listdir('videos'): | |
print(filename) | |
folder_name = filename.split('.')[0] | |
os.mkdir('thumbnails/'+ folder_name) | |
for filename in os.listdir('videos'): | |
print(filename) | |
folder_name = filename.split('.')[0] | |
video_name = 'videos/' + filename | |
folder_name = 'thumbnails/' + folder_name | |
generate_thumbnails(video_name, folder_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment