How to save a video using OpenCV VideoWriter to a specific directory — python
Question:
I am trying to save a video in a specific folder.
but after running the code no output is saved.
Could anyone help? Thanks.
cap = cv2.VideoCapture(file_paths[0])
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
name = "C:jupyter_projectsTest FolderIntention datasetbackground_subtractedout.mp4"
out = cv2.VideoWriter(name,fourcc, 20,(320,180),False)
while(1):
ret, frame = cap.read()
if (ret == True):
resized_frame = cv2.resize(frame,(320,180),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
fgmask = fgbg.apply(resized_frame)
cv2.imshow('Frame',fgmask)
out.write(fgmask)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.waitKey(5)
cv2.destroyAllWindows()
PS: when I use default directory for saving videos output will be saved.
out = cv2.VideoWriter("out.mp4",fourcc, 20,(320,180),False)
Answers:
Try
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
instead of
fourcc = cv2.VideoWriter_fourcc(*'XVID')
Try using single quote and using double slash for your file directory.
When you call the function
cv2.VideoWriter("/your/path/filename.mp4",fourcc, 20,(320,180))
I hope it helps 🙂
Using createBackgroundSubtractorMOG2()
,
import cv2
cap = cv2.VideoCapture(0)
# fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
fgbg = cv2.createBackgroundSubtractorMOG2()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
name = "C://path//of//your_dir//out.mp4"
out = cv2.VideoWriter(name,fourcc, 20,(320,180),False)
while(1):
ret, frame = cap.read()
if (ret == True):
resized_frame = cv2.resize(frame,(320,180),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
fgmask = fgbg.apply(resized_frame)
cv2.imshow('Frame',fgmask)
out.write(fgmask)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.waitKey(5)
cv2.destroyAllWindows()
This will save your out.mp4 given location. You can also use single forward slash instead of double forward slash, when specifying location of your file.
If you are on windows, use a raw string in the format of: r"C:bluh bluh directoryabc"
I am trying to save a video in a specific folder.
but after running the code no output is saved.
Could anyone help? Thanks.
cap = cv2.VideoCapture(file_paths[0])
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
name = "C:jupyter_projectsTest FolderIntention datasetbackground_subtractedout.mp4"
out = cv2.VideoWriter(name,fourcc, 20,(320,180),False)
while(1):
ret, frame = cap.read()
if (ret == True):
resized_frame = cv2.resize(frame,(320,180),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
fgmask = fgbg.apply(resized_frame)
cv2.imshow('Frame',fgmask)
out.write(fgmask)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.waitKey(5)
cv2.destroyAllWindows()
PS: when I use default directory for saving videos output will be saved.
out = cv2.VideoWriter("out.mp4",fourcc, 20,(320,180),False)
Try
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
instead of
fourcc = cv2.VideoWriter_fourcc(*'XVID')
Try using single quote and using double slash for your file directory.
When you call the function
cv2.VideoWriter("/your/path/filename.mp4",fourcc, 20,(320,180))
I hope it helps 🙂
Using createBackgroundSubtractorMOG2()
,
import cv2
cap = cv2.VideoCapture(0)
# fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
fgbg = cv2.createBackgroundSubtractorMOG2()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
name = "C://path//of//your_dir//out.mp4"
out = cv2.VideoWriter(name,fourcc, 20,(320,180),False)
while(1):
ret, frame = cap.read()
if (ret == True):
resized_frame = cv2.resize(frame,(320,180),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
fgmask = fgbg.apply(resized_frame)
cv2.imshow('Frame',fgmask)
out.write(fgmask)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.waitKey(5)
cv2.destroyAllWindows()
This will save your out.mp4 given location. You can also use single forward slash instead of double forward slash, when specifying location of your file.
If you are on windows, use a raw string in the format of: r"C:bluh bluh directoryabc"