Crazyka51 commited on
Commit
a8aca4e
·
verified ·
1 Parent(s): 70f8ba9

Create video_processing.py

Browse files
Files changed (1) hide show
  1. video_processing.py +199 -0
video_processing.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def execute_python_script(script_code, context=None):
2
+ """
3
+ Safely execute Python script with limited context
4
+ Returns script output or error message
5
+ """
6
+ import io
7
+ import sys
8
+ from contextlib import redirect_stdout, redirect_stderr
9
+
10
+ if context is None:
11
+ context = {
12
+ 'video_path': None,
13
+ 'frame_data': None,
14
+ 'metadata': None
15
+ }
16
+
17
+ # Capture output
18
+ output = io.StringIO()
19
+ error = io.StringIO()
20
+
21
+ try:
22
+ with redirect_stdout(output), redirect_stderr(error):
23
+ # Create restricted execution environment
24
+ exec_globals = {
25
+ '__builtins__': {
26
+ 'print': print,
27
+ 'str': str,
28
+ 'int': int,
29
+ 'float': float,
30
+ 'list': list,
31
+ 'dict': dict,
32
+ 'tuple': tuple,
33
+ 'range': range,
34
+ 'len': len,
35
+ 'enumerate': enumerate,
36
+ 'zip': zip,
37
+ 'min': min,
38
+ 'max': max,
39
+ 'sum': sum,
40
+ 'abs': abs,
41
+ 'round': round
42
+ },
43
+ 'context': context
44
+ }
45
+
46
+ exec(script_code, exec_globals)
47
+
48
+ if error.getvalue():
49
+ return f"Error: {error.getvalue()}"
50
+ else:
51
+ return output.getvalue()
52
+
53
+ except Exception as e:
54
+ return f"Error executing script: {str(e)}"
55
+
56
+ def validate_python_script(script_code):
57
+ """
58
+ Validate Python script syntax and restricted functions
59
+ Returns (is_valid, error_message)
60
+ """
61
+ import ast
62
+
63
+ try:
64
+ # Parse script into AST
65
+ tree = ast.parse(script_code)
66
+
67
+ # Check for disallowed nodes
68
+ for node in ast.walk(tree):
69
+ if isinstance(node, ast.Import):
70
+ return (False, "Import statements are not allowed")
71
+ if isinstance(node, ast.ImportFrom):
72
+ return (False, "Import statements are not allowed")
73
+ if isinstance(node, ast.Call):
74
+ if isinstance(node.func, ast.Name):
75
+ if node.func.id in ['eval', 'exec', 'open', 'execfile']:
76
+ return (False, f"Function {node.func.id}() is not allowed")
77
+
78
+ return (True, "Script is valid")
79
+ except SyntaxError as e:
80
+ return (False, f"Syntax error: {str(e)}")
81
+
82
+
83
+ def extract_video_metadata(video_path):
84
+ """
85
+ Extract technical metadata from video file
86
+ Returns dictionary of metadata
87
+ """
88
+ import cv2
89
+ from datetime import datetime
90
+ import os
91
+
92
+ cap = cv2.VideoCapture(video_path)
93
+
94
+ if not cap.isOpened():
95
+ return None
96
+
97
+ # Get basic video properties
98
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
99
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
100
+ fps = cap.get(cv2.CAP_PROP_FPS)
101
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
102
+ duration = frame_count / fps
103
+
104
+ # Get file info
105
+ file_stats = os.stat(video_path)
106
+ created = datetime.fromtimestamp(file_stats.st_ctime)
107
+ modified = datetime.fromtimestamp(file_stats.st_mtime)
108
+
109
+ cap.release()
110
+
111
+ return {
112
+ 'filename': os.path.basename(video_path),
113
+ 'path': video_path,
114
+ 'resolution': f"{width}x{height}",
115
+ 'fps': fps,
116
+ 'duration': duration,
117
+ 'frame_count': frame_count,
118
+ 'size': file_stats.st_size,
119
+ 'created': created,
120
+ 'modified': modified
121
+ }
122
+
123
+ def extract_audio_metadata(audio_path):
124
+ """
125
+ Extract technical metadata from audio file
126
+ Returns dictionary of metadata
127
+ """
128
+ import wave
129
+ from datetime import datetime
130
+ import os
131
+
132
+ try:
133
+ with wave.open(audio_path, 'rb') as audio_file:
134
+ channels = audio_file.getnchannels()
135
+ sample_width = audio_file.getsampwidth()
136
+ framerate = audio_file.getframerate()
137
+ frames = audio_file.getnframes()
138
+ duration = frames / float(framerate)
139
+
140
+ file_stats = os.stat(audio_path)
141
+ created = datetime.fromtimestamp(file_stats.st_ctime)
142
+ modified = datetime.fromtimestamp(file_stats.st_mtime)
143
+
144
+ return {
145
+ 'filename': os.path.basename(audio_path),
146
+ 'path': audio_path,
147
+ 'channels': channels,
148
+ 'sample_width': sample_width,
149
+ 'sample_rate': framerate,
150
+ 'duration': duration,
151
+ 'size': file_stats.st_size,
152
+ 'created': created,
153
+ 'modified': modified
154
+ }
155
+ except:
156
+ return None
157
+
158
+ def extract_exif_data(image_path):
159
+ """
160
+ Extract EXIF metadata from image file
161
+ Returns dictionary of EXIF data
162
+ """
163
+ from PIL import Image, ExifTags
164
+ from datetime import datetime
165
+ import os
166
+
167
+ try:
168
+ img = Image.open(image_path)
169
+ exif_data = img._getexif()
170
+
171
+ if not exif_data:
172
+ return None
173
+
174
+ exif = {}
175
+ for tag, value in exif_data.items():
176
+ decoded = ExifTags.TAGS.get(tag, tag)
177
+ exif[decoded] = value
178
+
179
+ # Get file info
180
+ file_stats = os.stat(image_path)
181
+ created = datetime.fromtimestamp(file_stats.st_ctime)
182
+ modified = datetime.fromtimestamp(file_stats.st_mtime)
183
+
184
+ exif['filename'] = os.path.basename(image_path)
185
+ exif['path'] = image_path
186
+ exif['size'] = file_stats.st_size
187
+ exif['created'] = created
188
+ exif['modified'] = modified
189
+
190
+ return exif
191
+ except:
192
+ return None
193
+
194
+
195
+ def detect_faces(frame, min_confidence=0.7):
196
+ """
197
+ Detect faces in a frame using OpenCV DNN
198
+ Returns list of face bounding boxes and confidence scores
199
+ """