-1

Does anyone know any function for saving a video in openCV.

Preferably a function to be able to work with it.

I've tried to implement but I couldn't

AMC
  • 2,642
  • 7
  • 13
  • 35
  • What is the issue, exactly? Have you done any research? – AMC Mar 28 '20 at 22:38
  • i already resolve that. thanks, if you want to see that question hihi https://stackoverflow.com/questions/60907884/how-increment-filename-in-pythonopencv – FRango SStYle Mar 28 '20 at 22:41

1 Answers1

0

OpenCV has VideoWriter API that you can use. See the below example code:

##define the video writer
out = cv2.VideoWriter(<filename_with_extention>,<compression_format>, <frames_per_second>, (frame_width,frame_height)) 

## write a frame in the loop
out.write(<your_frame>)

## Release the video writer outside of the scope of the loop 
out.release()
sam
  • 2,263
  • 22
  • 34
  • yes, but needed a function to implement with another code – FRango SStYle Mar 28 '20 at 18:37
  • Sorry, I might not be able to understand clearly here. What do you mean by "with another code". – sam Mar 28 '20 at 18:43
  • I have a program that selects points in a video and tracks them. And I wanted a function that recorded this video, already with the tracking points – FRango SStYle Mar 28 '20 at 18:48
  • like that: https://stackoverflow.com/questions/60903066/how-to-save-a-video-opencv-with-selected-points-and-track-on-them it's the problem i want to resolve :x – FRango SStYle Mar 28 '20 at 18:50
  • Ok, while reading and parsing through the input video, you can write the frames which have your tracked points in the VideoWriter object separately. When the input video parsing is finished, you can release VideoWriter object. – sam Mar 28 '20 at 18:52
  • Exactly, I tested it but I couldn't: x, did you see the code on the link I sent? – FRango SStYle Mar 28 '20 at 18:53
  • if you could help with the code: x I've been trying this for two days and nothing xd – FRango SStYle Mar 28 '20 at 18:54
  • I had it on the def save video but I deleted it – FRango SStYle Mar 28 '20 at 19:03
  • but is something like that: ``` def save_video(outPath, fps, mirror=False): out = cv2.VideoWriter(outPath, fourcc, fps, (int(width), int(height))) while(cap.isOpened()): if(check==True): out.write(frame) ``` – FRango SStYle Mar 28 '20 at 19:04
  • I couldn't see your frame writing part of the code. Before the line cv2.imshow("Frame", frame) you can write out.write(frame). Also, why are there 2 while True loops? You should have one while True: loop. I would suggest editing in that question with latest code. – sam Mar 28 '20 at 19:05
  • first while is to stop the video and choose the points, when i press 'p' the video continue with the trackng – FRango SStYle Mar 28 '20 at 19:06
  • Before any loop starts, write out = cv2.VideoWriter(outPath, fourcc, fps, (int(width), int(height))) Inside the loop write, out.write Lastly, release is at correct position. No need to call separate save_video function – sam Mar 28 '20 at 19:07
  • write out.write inside that loop? – FRango SStYle Mar 28 '20 at 19:10
  • and the argument ?? – FRango SStYle Mar 28 '20 at 19:11
  • Yes out.write(frame) should be inside the loop. Just before your cv2.imshow(frame). Adding the updated code in the other question. – sam Mar 28 '20 at 19:14
  • hmm, outPath is not defined – FRango SStYle Mar 28 '20 at 19:16
  • yes, it should be the name of the file.. say "my_video.avi" – sam Mar 28 '20 at 19:17
  • man! you save my life <3 – FRango SStYle Mar 28 '20 at 19:20
  • Please upvote :) Also I would suggest avoiding 2 while loops somehow. Ideally, there should not be two while True loops. – sam Mar 28 '20 at 19:21
  • theoretically the first while ends after pressing a key, starting the other, but yes I will see about it, thank you again! given upvote :) – FRango SStYle Mar 28 '20 at 19:24
  • My pleasure, and yes you are correct from the logical point of implementation flow. – sam Mar 28 '20 at 19:25
  • yeah nice, thanks again! – FRango SStYle Mar 28 '20 at 19:27
  • sry for another question xd, are we able to save several videos, or is it replaced every time we play the saved video? – FRango SStYle Mar 28 '20 at 20:01