[PYTHON] How to save only a part of a long video using OpenCV

Trigger

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.

Original material

-Destroy NHK![Political Broadcasting] Party to protect the people from NHK [Takashi Tachibana and 3 others] National Proportional District

[Destroy NHK![Political Broadcasting] Party to protect the people from NHK [Takashi Tachibana and 3 others] National Proportional District](http://www.youtube. com / watch? v = iRi4od_Thus)

Method

--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.

development of

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()

result

I think this is just one minute.

in conclusion

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.

Referenced link

-Read video from OpenCV-Python file --OpenCV --Load / write video with VideoCapture / VideoWriter.

Recommended Posts

How to save only a part of a long video using OpenCV
How to take a captured image from a video (OpenCV)
[Ruby] How to replace only a part of the string matched by the regular expression?
How to shuffle a part of a Python list (at random.shuffle)
How to transpose a 2D array using only python [Note]
How to split and save a DataFrame
How to get rid of long comprehensions
How to draw a graph using Matplotlib
How to crop the lower right part of the image with Python OpenCV
How to play a video while watching the number of frames (Mac)
A simple example of how to use ArgumentParser
How to code a drone using image recognition
How to upload to a shared drive using pydrive
How to uninstall a module installed using setup.py
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Save an array of numpy to a wav file using the wave module
A story of using rembg to make a white background + person video into a black background video
How to write a GUI using the maya command
A memo of how to use AIST supercomputer ABCI
How to set up a Python environment using pyenv
Cut a part of the string using a Python slice
How to make a shooting game with toio (Part 1)
How to hold a hands-on seminar using Jupyter using docker
How to loop and play gif video with openCV
How to save a table scraped by python to csv
Basics of PyTorch (2) -How to make a neural network-
Implementation of a convolutional neural network using only Numpy
How to execute a command using subprocess in Python
How to create an instance of a particular class from dict using __new__ () in python
How to get only the data you need from a structured data set using a versatile method
How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"
[Ubuntu] How to delete the entire contents of a directory
[Python] How to make a list of character strings character by character
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit Part 2-
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
How to display a list of installable versions with pyenv
How to register a package on PyPI (as of September 2017)
How to get a list of built-in exceptions in python
How to find the scaling factor of a biorthogonal wavelet
Survey log of how to optimize LightGBM hyperparameters using Optuna
How to get a list of links from a page from wikipedia
How to get a quadratic array of squares in a spiral!
[End of 2020] A memo to start using AWS CLI (Version 2)
How to connect the contents of a list into a string
How to hack a terminal
A memorandum of using eigen3
How to display a specified column of files in Linux (awk)
How to build a LAMP environment using Vagrant and VirtulBox Note
How to paste a CSV file into an Excel file using Pandas
Overview of how to create a server socket and how to establish a client socket
I tried to get a database of horse racing using Pandas
How to determine the existence of a selenium element in Python
[Python] How to force a method of a subclass to do something specific
I tried to make a regular expression of "amount" using Python
ABC170 E --How to solve without using multiset of Smart Infants
I tried to make a regular expression of "time" using Python
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to make a regular expression of "date" using Python
Here's a brief summary of how to get started with Django
How to check the memory size of a dictionary in Python