Ask Your Question
0

Can I change the fps of a video dynamically?

asked 2018-02-05 14:11:30 -0600

aniket03 gravatar image

updated 2020-07-23 04:07:18 -0600

I am using VideoWriter in python with opencv3.4. I wanted to change the fps of a video dynamically. Presently I am using out = cv2.VideoWriter('output.avi',fourcc,60.0, (848,480)) where 60.0 is the fps. Is it possible that at some part of my code , I can change it to 30fps and then back to 60fps or something else at some other part of the code?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2018-02-06 04:01:09 -0600

kbarni gravatar image

You can simulate the other framerates. For example, if you want 30fps stream on a 60fps video, just add each frame twice.

fps=30
while not endOfMyVideo:
    #get the frame to write...
    if fps==60:
        out.write(frame)
    elif fps==30:
        out.write(frame)
        out.write(frame)

For other frame rates you can duplicate only certain frames:

outputframe=0
inputframe=0
factor=videofps/inputfps
while not endOfMyVideo:
    #get the frame to write...
   out.write(frame)
   if outputframe-inputframe>1:
        out.write(frame) //we need to duplicate this frame
        inputframe=inputframe+1
    inputframe=inputframe+1 #get to the next frame
    outputframe=outputframe+factor

This is a simple solution, for better results you might interpolate the subsequent frames.

On the other hand, this will be a "pseudo" framerate, it won't really change the real framerate of the AVI file. So you can't change the framerate of a 30fps avi to 60fps just to add some high quality video (but you can downsample the 60fps video to 30fps by taking every second frame. The code above can be adapted to this situation too.)

edit flag offensive delete link more
0

answered 2020-07-14 08:00:48 -0600

urvi gravatar image

You can go through my code where I have done it using seconds of current time as a dynamic parameter.

Also, keep in mind that fps can't increase beyond its max(for a particular video it's fps is fixed when played in 1x speed).

Hope this helps. <3

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-02-05 14:11:30 -0600

Seen: 8,781 times

Last updated: Jul 14 '20