Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenCV capture video and write to drive
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# capture a video and write it to drive\n",
"\n",
"import cv2\n",
"\n",
"cap = cv2.VideoCapture(0)\n",
"window_name='camera'\n",
"\n",
"cv2.namedWindow(window_name,cv2.WND_PROP_FULLSCREEN)\n",
"cv2.setWindowProperty(window_name,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)\n",
"\n",
"fourcc = cv2.VideoWriter_fourcc(*'XVID')\n",
"out = cv2.VideoWriter('output.avi',fourcc, 30.0, (640,480)) # capture 30 frame per second\n",
" # video resolution 640x480\n",
"while (True):\n",
" ret, frame = cap.read()\n",
" \n",
" cv2.imshow(window_name,frame)\n",
" out.write(frame)\n",
" if cv2.waitKey(1) & 0xFF == ord('q'): # btw, you need to click the screen first. And then \n",
" # press q to quit\n",
" break\n",
" \n",
"cap.release()\n",
"out.release()\n",
"cv2.destroyAllWindows()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.