Hi there, its been a long time since i haven't updated anything on this blog, but i haven't come back with empty hands or empty mind, i have very great ideas to post on this blog, so stay tuned.
If you haven't check my other posts i request you to go through it once.
Let me start where i left, i have completed below topics.
1. Introduction to OpenCV & Installing OpenCV in your PC. (Link)
2. Writing your first OpenCV program. (Link)
3. Read,Save and display Images (Link)
4. Reading videos with OpenCV (Link)
4. Writing videos with OpenCV (This blog)
In this blog i want to show you how to save videos with OpenCV, before that we need to read video or video stream to our program so if you dont know how to do that i request you to go through this post to know how to read videos using OpenCV.
Ok lets get started,
In my previous post i showed you how to read videos using imutils package, but in this video i want to show you how to read video using inbuilt OpenCV function cv2.VideoCapture().
Create a new file name it as WriteVideo.py
Import cv2 library, and initialize video capture instance.
videofile variable should contain a path to your video file if you are using video.
if you are using webcam just replace the code as below,
Now lets see if our video or webcam is working or not,
In above code what we are doing is that, we are checking if the video capture stream is opened or not? by using isOpened() method. the reason why we are using this method is to end the infinite while loop when video frame is ended when we are using video files. while in webcam if the webcam is not found the while loop will not initiate.
Once we are in while loop, we are reading frames from videofile or the webcam, the read() method returns two values, first one is flag containing true or false for frame is received or not, and second is frame it self. frame is nothing but an image from the video source.
after that we are quickly checking if we have received frame or not, if not we are ending our program, if you are using video file the program will end as soon as the video is ended.
on line 13 we are displaying our frame by using cv2.imshow(), line 14 is to record user keyboard event when we want to exit in between by pressing q key from keyboard we can exit the while loop by breaking the loop.
once the loop is exited, we have to close the window opened by cv2.imshow() method or else it'll stay opened all the time even if we end our program, and also we have to release our capture instance as well as the the resource that we are using (i.e videofile or webcam so that other process or program can access it afterwards.
Before saving the video, you should know that each computer has something called codecs, which is used while saving,displaying(rendering) videos on our computer. my computer might have different codec and your computer might have different codec as well. so if i write code according to my computer's codec it might possible that it'll not work on your computer, if it is having different codec. So to solve this issue i have developed a module that can check what codecs are supported by your computer so that you can use them to save the video.
so to do that you need that module, you can download it from my github repo. Download it or just do git checkout of that file, place this file in same directory where WriteVideo.py present.
Now once you have this file import this file(module) in our WriteVideo.py file.
Make sure the file name is correct with case sensitive.
Once you import this file, you are ready to test the codecs installed in your system.
In above code i have commented out the webcam code because in this module we will be needing one video file to give as an input, so i have given path to the video file and i'm passing the videofile path to the CV2VideoWriter instance. you can take any file and give it as an input, make sure you enter correct path, best practice is to put video file in same directory and give videofile name in your program.
Note: Please take video which is short in length like 1 to 2 minutes, so that the procedure finishes faster. Also take long enough so that we can extract few frames and save the video.
Once you run this code, what actually this CV2VideoWriter does is that it reads the video given as an input and tries to save video with different available codecs, one which passes the saving procedure it counts as a working codec where it fails it count that codec as a not supported codec by system.
In output you can see the available codecs in your computer which you can use to save the videos from OpenCV.
If you are running code in cmd you can see some errors with output which you can ignore,but observe the line start with codec and showing its availability, where its stated as true means that codec is available to use.
But of you run this program on spyder IDE, you can see a clear output shown below. If you want to know more about spyder IDE leave your questions in comment section.
Now we have the codec names which we can use to save the videos using OpenCV.
Below i have attached the full code for saving video.
There are few changes that we will do while saving the video,
1. While initiating CV2VideoWriter Instance we will provide codec that is working in our your PC.
2. We will provide channel type i.e single channel that is black and white or 3 channel that is RGB. so to do this we will provide channel=0 for single channel and for RGB channel=1. (if you dont do this you might get an error)
3. We will start a session for video writer to save a frame.
4. We will read a frame from webcam and convert it into gray image using cv2.cvtcolor() method. if you are using video that is fine, if you have provided channel=0 that is single channel then you need to convert your frame into gray that is single channel or else it will throw an error.
5. After converting we will save the frame using writter.save_session_frame() method.
Once all the frame saving is done we need to end the session so that our writer knows to finally save the video and of course we need to release our capture instance so that other program can access out webcam or video file.
Kudos, now you know how to save video using OpenCV python.
Let me know in case you are facing any issue with above method.
tags: opencv,python,video,videowrite,how to write video using opencv,videowritter,ffmpeg, codec, VideoWriter_fourcc, opencv with python, video operations, convert to gray.
If you haven't check my other posts i request you to go through it once.
Let me start where i left, i have completed below topics.
1. Introduction to OpenCV & Installing OpenCV in your PC. (Link)
2. Writing your first OpenCV program. (Link)
3. Read,Save and display Images (Link)
4. Reading videos with OpenCV (Link)
4. Writing videos with OpenCV (This blog)
In this blog i want to show you how to save videos with OpenCV, before that we need to read video or video stream to our program so if you dont know how to do that i request you to go through this post to know how to read videos using OpenCV.
Ok lets get started,
1. Read video from either your webcam or from any other video file.
In my previous post i showed you how to read videos using imutils package, but in this video i want to show you how to read video using inbuilt OpenCV function cv2.VideoCapture().
Create a new file name it as WriteVideo.py
Import cv2 library, and initialize video capture instance.
videofile variable should contain a path to your video file if you are using video.
if you are using webcam just replace the code as below,
Now lets see if our video or webcam is working or not,
In above code what we are doing is that, we are checking if the video capture stream is opened or not? by using isOpened() method. the reason why we are using this method is to end the infinite while loop when video frame is ended when we are using video files. while in webcam if the webcam is not found the while loop will not initiate.
Once we are in while loop, we are reading frames from videofile or the webcam, the read() method returns two values, first one is flag containing true or false for frame is received or not, and second is frame it self. frame is nothing but an image from the video source.
after that we are quickly checking if we have received frame or not, if not we are ending our program, if you are using video file the program will end as soon as the video is ended.
on line 13 we are displaying our frame by using cv2.imshow(), line 14 is to record user keyboard event when we want to exit in between by pressing q key from keyboard we can exit the while loop by breaking the loop.
once the loop is exited, we have to close the window opened by cv2.imshow() method or else it'll stay opened all the time even if we end our program, and also we have to release our capture instance as well as the the resource that we are using (i.e videofile or webcam so that other process or program can access it afterwards.
2. Save video frames.
so to do that you need that module, you can download it from my github repo. Download it or just do git checkout of that file, place this file in same directory where WriteVideo.py present.
Now once you have this file import this file(module) in our WriteVideo.py file.
Make sure the file name is correct with case sensitive.
Once you import this file, you are ready to test the codecs installed in your system.
In above code i have commented out the webcam code because in this module we will be needing one video file to give as an input, so i have given path to the video file and i'm passing the videofile path to the CV2VideoWriter instance. you can take any file and give it as an input, make sure you enter correct path, best practice is to put video file in same directory and give videofile name in your program.
Note: Please take video which is short in length like 1 to 2 minutes, so that the procedure finishes faster. Also take long enough so that we can extract few frames and save the video.
Once you run this code, what actually this CV2VideoWriter does is that it reads the video given as an input and tries to save video with different available codecs, one which passes the saving procedure it counts as a working codec where it fails it count that codec as a not supported codec by system.
In output you can see the available codecs in your computer which you can use to save the videos from OpenCV.
If you are running code in cmd you can see some errors with output which you can ignore,but observe the line start with codec and showing its availability, where its stated as true means that codec is available to use.
But of you run this program on spyder IDE, you can see a clear output shown below. If you want to know more about spyder IDE leave your questions in comment section.
Now we have the codec names which we can use to save the videos using OpenCV.
Below i have attached the full code for saving video.
There are few changes that we will do while saving the video,
1. While initiating CV2VideoWriter Instance we will provide codec that is working in our your PC.
2. We will provide channel type i.e single channel that is black and white or 3 channel that is RGB. so to do this we will provide channel=0 for single channel and for RGB channel=1. (if you dont do this you might get an error)
3. We will start a session for video writer to save a frame.
4. We will read a frame from webcam and convert it into gray image using cv2.cvtcolor() method. if you are using video that is fine, if you have provided channel=0 that is single channel then you need to convert your frame into gray that is single channel or else it will throw an error.
5. After converting we will save the frame using writter.save_session_frame() method.
Once all the frame saving is done we need to end the session so that our writer knows to finally save the video and of course we need to release our capture instance so that other program can access out webcam or video file.
Kudos, now you know how to save video using OpenCV python.
Let me know in case you are facing any issue with above method.
tags: opencv,python,video,videowrite,how to write video using opencv,videowritter,ffmpeg, codec, VideoWriter_fourcc, opencv with python, video operations, convert to gray.
No comments:
Post a Comment