How to create thumbnails for videos with python and opencv





Sometimes, sorting through the rubble of large and small video files in a folder (folders), there is no time to look into the contents of each file. This is where the so-called thumbnails come to mind, which allow you to create an idea of ​​the content in the form of cutting fragments from a video.



Let's create a small program that will create thumbnails for each of the files in the current windows folder, and add a timeline to the cut files.



Standard import of modules at the beginning of a python program:



import numpy as np
import cv2
import os


We indicate in which folder to look for files and add a message for the user:



file=file
print('...')
path=r'E:\1'
os.chdir(path)


Here the program processes all files on the E drive in the folder 1.



Next, opencv enters the battle, cuts frames and the timeline to them:



vidcap = cv2.VideoCapture(path+'\\'+file)
    fps = vidcap.get(cv2.CAP_PROP_FPS)
    #print(fps)
    n=12
    total_frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
    time_line = total_frames / fps

    frames_step = total_frames//n
    time_line_step=time_line//n
    #print(int(time_line_step))
    a=[]
    b=[]


n - the number of files in the slicing, 12 pieces.



Since the timeline slicing is in seconds, so that it is correctly displayed on frames, let's

add a function that leads to the time format 00:00:00:



def sec_to_time(t):
        h=str(t//3600)
        m=(t//60)%60
        s=t%60
        if m<10:
            m='0'+str(m)
        else:
            m=str(m)
        if s<10:
            s='0'+str(s)
        else:
            s=str(s)    
        #print(h+':'+m+':'+s)
        t=h+':'+m+':'+s
        return t


Now we get the pictures, reduce their size by 50% and save them to disk as intermediate files:



for i in range(n):        
        vidcap.set(1,i*frames_step)
        success,image = vidcap.read()
        #  
        scale_percent = 50
        width = int(image.shape[1] * scale_percent / 100)
        height = int(image.shape[0] * scale_percent / 100)
        image=cv2.resize(image, (width, height))

        #     c time_line
        font = cv2.FONT_HERSHEY_COMPLEX    
        t=int(time_line_step)*i    
        image=cv2.putText(image, sec_to_time(t), (100, 30), font, 0.5, color=(0, 0, 255), thickness=0)   
        cv2.imwrite('image'+str(i)+'.jpg',image)
        a.append('image'+str(i)+'.jpg')
    vidcap.release()


We glue the resulting files, using opencv, horizontally with each other, observing the order:



def glue (img1,img2,img3,x):
        i1 = cv2.imread(img1)
        i2 = cv2.imread(img2)
        i3 = cv2.imread(img3)    
        vis = np.concatenate((i1, i2, i3), axis=1)
        cv2.imwrite('out'+str(x)+'.png', vis)
        b.append('out'+str(x)+'.png')
    x=0
    while x<len(a):    
        glue(a[x],a[x+1],a[x+2],x)
        x+=3


Glue the resulting "triplets" vertically:



 #   
    def glue2 (img1,img2,img3,img4):
        i1 = cv2.imread(img1)
        i2 = cv2.imread(img2)
        i3 = cv2.imread(img3)
        i4 = cv2.imread(img4) 
        vis = np.concatenate((i1, i2, i3,i4), axis=0)
        cv2.imwrite(file[:-4]+'.jpeg', vis)
    glue2(b[0],b[1],b[2],b[3])


We clean up the folder by deleting temporary files:



#
    c=['jpg', 'png']
    for root, dirs, files in os.walk(path):    
        for file in files:
            if file[-3:] in c:
                os.remove(file)


We carry out the above procedures for all video files in the folder:



video=['wmv', 'mp4', 'avi', 'mov', 'MP4', '.rm', 'mkv']
for root, dirs, files in os.walk(r'E:/1'):    
    for file in files:
        if file[-3:] in video:
            print(' -'+file)
            tumbnail(file)


The program code for those to whom I belong, first downloads the code, and then reads the article - download .



PS timeline is not without sin and is a little bit out of touch with the real timeline video.



This is especially noticeable on large video files.



All Articles