Python New n frame video is heavier than the input video

189 Views Asked by At

I managed to write a code to decimate my video and take only 1 frame out of 10, in order to make my neural network more efficient in the future for character recognition. The new video exit_video is well decimated because it's way faster than the previous one.

1: When I print the fps of the new video, I have 30 again despite the decimation

2: Why is my new video heavier ? 50.000 ko and it was 42.000 ko for the firts one

Thanks for your help

import cv2
#import os
import sys


video = cv2.VideoCapture("./video/inputvideo.mp4")
frameWidth = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
frameFourcc = int(video.get(cv2.CAP_PROP_FOURCC))
success,image = video.read()

if not success:
    print('impossible de prendre une frame')
    sys.exit()
    
fps = video.get(cv2.CAP_PROP_FPS)
print("fps de base " + str(fps))
print(frameFourcc)
count = 0
exit_file = 'decimated_v1.mp4'
exit_video = cv2.VideoWriter(exit_file, frameFourcc, fps, (frameWidth, frameHeight))
while True:
    if ((count % 10 ) ==0):
        exit_video.write(image)
    success,image = video.read()
    if not success:
        break
        
    count +=1
    
exit_video.release()
exit_video_info = cv2.VideoCapture("decimated_v1.mp4")
fps_sortie = exit_video_info.get(cv2.CAP_PROP_FPS)
print("fps de sortie " + str(fps_sortie))    
1

There are 1 best solutions below

6
On BEST ANSWER

Decimating a video file that's not all Intra frames will require re-encoding. Unless your input file is e.g. ProRes or MJPEG, that's likely going to be the case.

Since you're not setting encoding parameters, OpenCV likely end up using some defaults that end up with a higher bitrate than your input file.

You'll probably have a better time using the FFmpeg tool than OpenCV, and its select filter.

ffmpeg -i ./video/inputvideo.mp4 -vf select='not(mod(n\,10))' ./decimated_v1.mp4

would be the basic syntax to use every tenth frame from the input; you can then add your desired encoding parameters such as -crf to adjust the H.264 rate factor – or, of course, you can change to a different codec altogether.