-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.py
More file actions
128 lines (100 loc) Β· 3.73 KB
/
Assignment.py
File metadata and controls
128 lines (100 loc) Β· 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
"Instructions"
-> Ask user if he wants to record a video or play a video or use webcam to see
himself.
-> According to that:
1. if record a video chosen:
-> record the user video giving him interface like play , pause , record button
and after that display the recorded video
-> give access user to download video also
2. if play a video chosen as for the source or upload the video and play it for user
3. if use webcam chosen then just open the webcam feed and let user
see himself
"""
import cv2
import tkinter as tk
from tkinter import filedialog
from threading import Thread
import datetime
import os
class VideoApp:
def __init__(self, root):
self.root = root
self.root.title("π₯ Video App")
self.root.geometry("300x300")
self.cap = None
self.recording = False
self.output = None
# Buttons
tk.Button(root, text="πΉ Record Video", command=self.record_video).pack(pady=10)
tk.Button(root, text="βΆοΈ Play Video", command=self.play_video).pack(pady=10)
tk.Button(root, text="πΈ Live Webcam", command=self.live_webcam).pack(pady=10)
tk.Button(root, text="β Quit", command=self.quit_app).pack(pady=30)
def record_video(self):
self.recording = True
filename = datetime.datetime.now().strftime("recorded_%Y%m%d_%H%M%S.avi")
fourcc = cv2.VideoWriter_fourcc(*"XVID")
self.cap = cv2.VideoCapture(0)
if not self.cap.isOpened():
print("β Could not open webcam")
return
width = int(self.cap.get(3))
height = int(self.cap.get(4))
self.output = cv2.VideoWriter(filename, fourcc, 20.0, (width, height))
def record():
while self.recording:
ret, frame = self.cap.read()
if not ret:
break
self.output.write(frame)
cv2.imshow("Recording... Press 'q' to stop", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
self.recording = False
break
self.cap.release()
self.output.release()
cv2.destroyAllWindows()
print(f"β
Saved recording to {filename}")
Thread(target=record).start()
def play_video(self):
filepath = filedialog.askopenfilename(
title="Select video file", filetypes=[("Video files", "*.mp4 *.avi *.mov")]
)
if not filepath:
return
cap = cv2.VideoCapture(filepath)
if not cap.isOpened():
print("β Could not open video")
return
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow("βΆοΈ Playing Video", frame)
if cv2.waitKey(30) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
def live_webcam(self):
self.cap = cv2.VideoCapture(0)
if not self.cap.isOpened():
print("β Could not open webcam")
return
while True:
ret, frame = self.cap.read()
if not ret:
break
cv2.imshow("πΈ Live Webcam Feed - Press 'q' to quit", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
self.cap.release()
cv2.destroyAllWindows()
def quit_app(self):
if self.cap:
self.cap.release()
cv2.destroyAllWindows()
self.root.quit()
if __name__ == "__main__":
root = tk.Tk()
app = VideoApp(root)
root.mainloop()