0

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 Answers1

0

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.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Hi, my entire code is using openCV so I would like to stick with it, I am not familiar with ffmpeg at all. You said it is likely to have used defauts encoding parameters, yet, in VideoWriter(), the 2nd parameter specify the encoding of the input, which is AVC1. Thanks for your reply –  Jun 09 '21 at 08:19
  • By my quick googling, it looks like VideoWriter doesn't allow properly setting the quality. https://github.com/opencv/opencv/issues/8961 – AKX Jun 09 '21 at 08:22
  • This related SO question ends up piping to ffmpeg... :) https://stackoverflow.com/q/38686359/51685 – AKX Jun 09 '21 at 08:23
  • Hi, when I try it, I have File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified –  Jun 14 '21 at 08:17
  • Do you have ffmpeg installed? Have you tried substituting the real path to your ffmpeg binary as the first argument? – AKX Jun 14 '21 at 08:26
  • Yes I have put the real path for the first argument, but I see that I can only use ffmpeg in python, I think I did not install it correctly or put in in the PATH –  Jun 14 '21 at 09:04