Ask Your Question
1

How do I save a video multiple times in python

asked 2020-07-23 02:43:28 -0600

tester gravatar image

I am trying to save the video many times If I use first writer then it save video only one time in this code And use this code then it doesn't save the video

import cv2
import time

cap = cv2.VideoCapture("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov")

width = int(cap.get(3))
height = int(cap.get(4))
fcc = cv2.VideoWriter_fourcc(*'XVID')
writer = cv2.VideoWriter('test.avi', fcc, 60.0, (width, height))
recording = False

while(1):
    ret, frame = cap.read()
    hms = time.strftime('%H:%M:%S', time.localtime())

    cv2.putText(frame, str(hms), (0, 15), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,255))

    cv2.imshow('frame', frame)

    k = cv2.waitKey(30) & 0xff
    if k == ord('r'):
        path = 'test_' + str(hms) + '.avi'
        writer = cv2.VideoWriter(path, fcc, 60.0, (width, height))            

    if recording:
        writer.write(frame)

    if k == ord('e'):
        print('record end')
        writer.release()

cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

recording is always False ?

berak gravatar imageberak ( 2020-07-23 03:24:33 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2020-07-23 03:16:36 -0600

updated 2020-07-23 03:52:44 -0600

you can test my modifications on your code

import cv2
import time

cap = cv2.VideoCapture(0)

width = int(cap.get(3))
height = int(cap.get(4))
fcc = cv2.VideoWriter_fourcc(*'XVID')
#writer = cv2.VideoWriter('e:/test.avi', fcc, 60.0, (width, height))
recording = False
videono = 0

while(1):
    ret, frame = cap.read()
    hms = time.strftime('%H:%M:%S', time.localtime())

    cv2.putText(frame, str(hms), (0, 15), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255))

    cv2.imshow('frame', frame)

    k = cv2.waitKey(1) & 0xff
    if k == ord('r') and recording is False:
        path = 'e:/test_' + str(videono) + '.avi'
        videono += 1
        print(path+' recording')
        writer = cv2.VideoWriter(path, fcc, 30.0, (width, height))
        recording = True

    if recording:
        writer.write(frame)

    if k == ord('e'):
        print('recording finished')
        recording = False
        writer.release()

cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

I solved the problem with this code. Thanks!

tester gravatar imagetester ( 2020-07-23 03:46:20 -0600 )edit

see also my last updates on your code. i don't know how to record video on realistic timing. recorded video is speedy here.

sturkmen gravatar imagesturkmen ( 2020-07-23 03:55:18 -0600 )edit
2

answered 2020-07-23 03:28:38 -0600

berak gravatar image

updated 2020-07-23 03:33:57 -0600

you should NEVER put : into a filename, it is reserved for various purposes (seperator on linux, disk specifier on win, etc)

try with a timestamp like:

 hms = time.strftime('%H_%M_%S', time.localtime())

and please check writer.isOpened() before trying to write, also the return value from writer.write()

edit flag offensive delete link more

Comments

It was my stupid mistake. Since I was only concerned about the file not being created, I didn't see it easy. Thanks

tester gravatar imagetester ( 2020-07-23 03:44:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-23 02:43:28 -0600

Seen: 2,992 times

Last updated: Jul 23 '20