save a screen of video FileField to ImageField durring create the object

16 Views Asked by At

im trying to save a screen image from SongForms video. I guess i should rewrite Songs save method. but i have no idea ,how can i take a screen from uplouded video and save it as imagefiled or thumbnail durring creation

models.py

class Song:
    video= models.FileField(
        upload_to=`videos/`
    image=models.ImageField(
        upload_to`images/`,
        default=`1.jpg`

forms.py

class SongForm(forms.ModelForm):
    class Meta:
        model=Song
    fields=[`video`]

im not sure if cv2 can help me. because i dont know how i should enter VideoCapture adress and how i should save it as imagefield durring create the object

# Importing all necessary libraries 
import cv2 
import os 
  
# Read the video from specified path 
cam = cv2.VideoCapture("C:\\Users\\Admin\\PycharmProjects\\project_1\\openCV.mp4") 
  
try: 
      
    # creating a folder named data 
    if not os.path.exists('data'): 
        os.makedirs('data') 
  
# if not created then raise error 
except OSError: 
    print ('Error: Creating directory of data') 
  
# frame 
currentframe = 0
  
while(True): 
      
    # reading from frame 
    ret,frame = cam.read() 
  
    if ret: 
        # if video is still left continue creating images 
        name = './data/frame' + str(currentframe) + '.jpg'
        print ('Creating...' + name) 
  
        # writing the extracted images 
        cv2.imwrite(name, frame) 
  
        # increasing counter so that it will 
        # show how many frames are created 
        currentframe += 1
    else: 
        break
  
# Release all space and windows once done 
cam.release() 
cv2.destroyAllWindows() 
0

There are 0 best solutions below