Gufu / Propagation YouTuber [Takashi Tachibana, which increases with "NHK" and doubles with "break"](https://www. synced to youtube.com/watch?v=NcR2d8_iM9o): rolling_eyes: With just the right video material, it seems that opencv can do something such as area division, face recognition, erasing telop, etc. This video has 18 minutes, but I want to use only the first minute when the candidate appears. So, I will try to extract only a part by time with python and OpenCV only.
[](http://www.youtube. com / watch? v = iRi4od_Thus)
--Extract fps (frames per second) from the original video
--Calculate the number of frames with start time * fps
or end time * fps
I tried to measure with time.time ()
, but it was more successful to control it based on the number of frames.
Also, because it is the first minute when the candidate appears
--Start time = 30 seconds --End time = 90 seconds
It was made.
import cv2
if __name__ == '__main__':
cap = cv2.VideoCapture('drop_out_nhk.mp4')
cap_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
cap_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
writer = cv2.VideoWriter('one_minutes.mp4',fourcc, fps, (cap_width, cap_height))
#Start or end time you want to extract
begin = 30
end = 90
for i in range(end * fps):
ret, frame = cap.read()
if ret:
if begin * fps < i:
writer.write(frame)
writer.release()
cap.release()
I think this is just one minute.
I thought I would use ʻasyncio`, but I was able to implement it fairly easily. If you change the image size, the codec will make a mistake for some reason.
-Read video from OpenCV-Python file --OpenCV --Load / write video with VideoCapture / VideoWriter.
Recommended Posts