SoFunction
Updated on 2024-12-13

python implement video split frame effect

In this article, the example for you to share the python realization of the video sub-frame specific code, for your reference, the specific content is as follows

import cv2 
vidcap = ('') 
success,image = () 
count = 0 
success = True 
while success: 
 success,image = () 
 ("frame%" % count, image)  # save frame as JPEG file 
 if (10) == 27:      
  break 
 count += 1 

python tools:Extract and save every frame of the video

# coding=utf-8 
 
import os 
import cv2 
 
videos_src_path = "/home/wgp/video/" 
video_formats = [".MP4", ".MOV"] 
frames_save_path = "/home/wgp/video/" 
width = 320 
height = 240 
time_interval = 50 
 
 
def video2frame(video_src_path, formats, frame_save_path, frame_width, frame_height, interval): 
 """
 Reads and writes the video to the image at fixed intervals
 :param video_src_path: the path where the video is stored
 :param formats: all included video formats
 :param frame_save_path: save path
 :param frame_width: save frame width
 :param frame_height: Save the frame height
 :param interval: save the frame interval
 :return: frame image
 """ 
 videos = (video_src_path) 
 
 def filter_format(x, all_formats): 
  if x[-4:] in all_formats: 
   return True 
  else: 
   return False 
 
 videos = filter(lambda x: filter_format(x, formats), videos) 
 
 for each_video in videos: 
  print "Video being read:", each_video 
 
  each_video_name = each_video[:-4] 
  (frame_save_path + each_video_name) 
  each_video_save_full_path = (frame_save_path, each_video_name) + "/" 
 
  each_video_full_path = (video_src_path, each_video) 
 
  cap = (each_video_full_path) 
  frame_index = 0 
  frame_count = 0 
  if (): 
   success = True 
  else: 
   success = False 
   print("Read failed!") 
 
  while(success): 
   success, frame = () 
   print "---> Reading frame %d:" % frame_index, success 
 
   if frame_index % interval == 0: 
    resize_frame = (frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA) 
    # (each_video_save_full_path + each_video_name + "_%" % frame_index, resize_frame) 
    (each_video_save_full_path + "%" % frame_count, resize_frame) 
    frame_count += 1 
 
   frame_index += 1 
 
 () 
 
 
if __name__ == '__main__': 
 video2frame(videos_src_path, video_formats, frames_save_path, width, height, time_interval) 

This is the whole content of this article.