Someone happened to ask me how to remove some frames of the video , There's just time , Exactly , Just send it .
If you want to use it, you can take it for reference .
Here is my use of opencv The method of extracting the middle frames of video .
The main idea is to read frame When , By the way, write down the frame .
At the same time, if it is not necessary to extract the deleted frames , direct continue To the next cycle .
The sample code is as follows , Mainly according to MP4 Format for processing .
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn-pro
@author : Swordsman a Liang _ALiang
@file : test.py
@ide : PyCharm
@time : 2022-06-30 17:55:48
"""
import cv2
# Video frame extraction
def extract_frame(video_path: str, result_path: str, fps, weight, height, start, end):
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoWriter = cv2.VideoWriter(result_path, fourcc, fps, (weight, height))
vc = cv2.VideoCapture(video_path)
if vc.isOpened():
ret, frame = vc.read()
else:
ret = False
count = 0 # count the number of pictures
while ret:
ret, frame = vc.read()
if start <= count <= end:
count += 1
continue
else:
videoWriter.write(frame)
count += 1
print(count)
videoWriter.release()
vc.release()
if __name__ == '__main__':
extract_frame('C:\\Users\\xxx\\Desktop\\123.mp4', 'C:\\Users\\xxx\\Desktop\\114.mp4', 25, 640, 368, 119, 125)
Be careful
1、extract_frame The input parameters of methods are : Enter the video address 、 Output video address 、 video fps、 Wide video resolution 、 High video resolution 、 The starting frame of the video that needs to be extracted 、 The end frame of the video that needs to be removed .
We won't verify the effect , You can try it on your own .
Pay attention to the wrong address , Causes the output to be empty .
Recently, my mentality has changed , I used to feel where I was , The rules are so chaotic , People's inertia and avoidance are so visible to the naked eye in front of things . I hate it , Even some repel and want to destroy it . But suddenly I stood in front of the mirror , I found that I also became a part of it , A feeling of nausea is tender for the second time . Learn to reconcile with yourself first , Trying to change what can be changed , I think this is what I want to do .