Skip to content

Commit e9787e3

Browse files
committed
Open-CV-Implementations
1 parent cd82833 commit e9787e3

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ This is a repository of code files which can be used to perform some basic tasks
5858

5959
Python Code to find & draw Contours present in an image using OpenCV.
6060

61+
* [Recording and Saving Videos](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/recording_video.ipynb):
62+
63+
Python Code to record and save videos with different types of extensions and video quality using OpenCV.
64+
6165
* [Adding Text to Videos](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/text_on_video.ipynb):
6266

6367
Python Code to draw text on a video using OpenCV.

recording_video.ipynb

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Recording Video in OpenCV"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np\n",
17+
"import cv2\n",
18+
"import os"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 20,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"filename = 'video.avi'\n",
28+
"frames_per_second = 24.0\n",
29+
"my_res = '720p'\n",
30+
"\n",
31+
"\n",
32+
"def change_res(cap, width, height):\n",
33+
" cap.set(3, width)\n",
34+
" cap.set(4, height)\n",
35+
"\n",
36+
"\n",
37+
"STD_DIMENSIONS = {\n",
38+
" \"480p\": (640, 480),\n",
39+
" \"720p\": (1280, 720),\n",
40+
" \"1080p\": (1920, 1080),\n",
41+
" \"4k\": (3840, 2160),\n",
42+
"}\n",
43+
"\n",
44+
"\n",
45+
"def get_dims(cap, res='1080p'):\n",
46+
" width, height = STD_DIMENSIONS[\"480p\"]\n",
47+
" if res in STD_DIMENSIONS:\n",
48+
" width,height = STD_DIMENSIONS[res]\n",
49+
" change_res(cap, width, height)\n",
50+
" return width, height\n",
51+
"\n",
52+
"\n",
53+
"VIDEO_TYPE = {\n",
54+
" 'avi': cv2.VideoWriter_fourcc(*'XVID'),\n",
55+
" 'mp4': cv2.VideoWriter_fourcc(*'XVID'),\n",
56+
"}\n",
57+
"\n",
58+
"def get_video_type(filename):\n",
59+
" filename, ext = os.path.splitext(filename)\n",
60+
" if ext in VIDEO_TYPE:\n",
61+
" return VIDEO_TYPE[ext]\n",
62+
" return VIDEO_TYPE['avi']\n",
63+
"\n",
64+
"\n",
65+
"\n",
66+
"cap = cv2.VideoCapture(0)\n",
67+
"out = cv2.VideoWriter(filename, get_video_type(filename), frames_per_second, get_dims(cap, my_res))\n",
68+
"\n",
69+
"while True:\n",
70+
" ret, frame = cap.read()\n",
71+
" out.write(frame)\n",
72+
" cv2.imshow('frame',frame)\n",
73+
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
74+
" break\n",
75+
"\n",
76+
"\n",
77+
"cap.release()\n",
78+
"out.release()\n",
79+
"cv2.destroyAllWindows()"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": []
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": []
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"metadata": {},
100+
"outputs": [],
101+
"source": []
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": []
109+
}
110+
],
111+
"metadata": {
112+
"kernelspec": {
113+
"display_name": "Python 3",
114+
"language": "python",
115+
"name": "python3"
116+
},
117+
"language_info": {
118+
"codemirror_mode": {
119+
"name": "ipython",
120+
"version": 3
121+
},
122+
"file_extension": ".py",
123+
"mimetype": "text/x-python",
124+
"name": "python",
125+
"nbconvert_exporter": "python",
126+
"pygments_lexer": "ipython3",
127+
"version": "3.5.2"
128+
}
129+
},
130+
"nbformat": 4,
131+
"nbformat_minor": 2
132+
}

0 commit comments

Comments
 (0)