-2

I want to change the framerate of an mp4 file from 30 to 5.I tried set(cv2.CAP_PROP_FPS, 10).But it did not work. I can't find a way to do this with ffmpeg. Please help me to do this.Thank you.

import cv2

cap = cv2.VideoCapture('./data/video/7e2.mp4')
fps = int(cap.get(cv2.CAP_PROP_FPS))
print('fps :'+str(fps))

if (cap.isOpened()== False):
  print("Error opening video stream or file")

while(cap.isOpened()):
  ret, frame = cap.read()
  if ret == True:
    cv2.imshow('Frame',frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
      break
  else:
    break
cap.release()
cv2.destroyAllWindows()

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Roshani
  • 41
  • 6

1 Answers1

1

If you are reading a file then file will not be modified and FPS of the original file will be unchanged. cv2.VideoCapture( ) opens file in read mode. To save a video file you need to use VideoWriter and then open the output file using

video.open(out_filename,VideoWriter::fourcc('M','P','4','V'),FPS, Size(FRAME_WIDTH,FRAME_HEIGHT),true) 

And then finally use write function to enter each frame.

So your code will look like this:

cv::VideoWriter video;
video.open(out_filename,VideoWriter::fourcc('M','P','4','V'),FPS, Size(FRAME_WIDTH,FRAME_HEIGHT),true)
In loop {
... Read Each Frame
 video.write(frame);

(I have given C++ code, Python code will be similar)

I am providing you the complete code. It should work.

import cv2

cap = cv2.VideoCapture('./data/video/7e2.mp4')
fps = int(cap.get(cv2.CAP_PROP_FPS))
print('fps :'+str(fps))
# Change the FPS whatever you want
FPS=5;
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
writer = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), FPS, (frame_width,frame_height) )   
   
if (cap.isOpened()== False):
  print("Error opening video stream or file")

while(cap.isOpened()):
  ret, frame = cap.read()
  if ret == True:
    cv2.imshow('Frame',frame)
    writer.write( frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
      break
  else:
    break
writer.release()
cap.release()
cv2.destroyAllWindows()
                        
Prabir Ghosh
  • 430
  • 3
  • 7