SoFunction
Updated on 2025-03-01

python convert video into time instance through video frames

I won’t say much nonsense, I’d better look at the code directly!

def frames_to_timecode(framerate,frames):
 """
  Video converted to time through video frames
  :param framerate: video framerate
  :param frames: current video frames
  :return:Time (00:00:01:01)
  """
 return '{0:02d}:{1:02d}:{2:02d}:{3:02d}'.format(int(frames / (3600 * framerate)),
             int(frames / (60 * framerate) % 60),
             int(frames / framerate % 60),
             int(frames % framerate))

print(frames_to_timecode(25,123))

00:00:04:23

Supplementary knowledge:python+opencv intercepts the video segment of the specified frame and converts the resolution at the same time

I watched it online for a long time and haven't solved the problem. Many of them are copying and pasting other people's things, which is delaying time. After many attempts and modifications, I finally succeeded. Without saying much nonsense, I just uploaded the code:

import cv2
videoCapture = ('K:/test/')
fps = 30 # Save the frame rate of the videosize = (350, 256) # Save the size of the video 
videoWriter = ('K:/test/0000_test.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps, size)
i = 0
 
while True:
 success, frame = ()
 if success:
  i += 1
  print('i = ', i)
  if (i >= 1000 and i <= 2000):
   frame=(frame,(350,256))
   (frame)
 
  if(i>2000):
   break
 else:
  print('end')
  break

The original video format should be converted to .mov (the editor will only succeed in this situation, and others can be tested by yourself). If you need to adjust the resolution, you must have:

frame=(frame,(350,256))

In this process, otherwise the video frame will not be written and the video output size will be 0.

I hope to point out the shortcomings, and I hope it can help you!

The above python example of converting videos into time through video frames is all the content I share with you. I hope you can give you a reference and I hope you can support me more.