-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (63 loc) · 2.59 KB
/
main.py
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
import subprocess
def validate_image(image_path):
# Level 1: File extension and magic number check
allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp']
file_extension = image_path.lower().rsplit('.', 1)[-1]
if file_extension not in allowed_extensions:
print("Invalid file extension")
return False
try:
# to check if the file can be decoded/ incl. checking magic number internally
cmd = ['ffmpeg', '-v', 'error', '-i', image_path, '-f', 'null', '-']
subprocess.run(cmd, check=True, capture_output=True)
first_check = True
except subprocess.CalledProcessError as e:
print(f"Invalid image file: {e.stderr.decode().strip()}")
return False
# Level 2: hflip/crop check (example)
try:
cmd = ['ffmpeg', '-i', image_path, '-vf', 'hflip,crop=100:100', '-f', 'null', '-']
subprocess.run(cmd, check=True, capture_output=True)
second_check = True
except subprocess.CalledProcessError:
print("Image failed hflip/crop check")
return False
# Level 3: TBD
# If all checks pass, the image is considered valid
if first_check and second_check:
return True
def validate_video(video_path):
# Level 1: File extension and magic number check
allowed_extensions = ['mp4', '3gp', 'mov', 'wmv', 'avi', 'flv']
file_extension = video_path.lower().rsplit('.', 1)[-1]
if file_extension not in allowed_extensions:
print("Invalid file extension.")
return False
try:
# Check if the video can be decoded/incl. magic number check
cmd = ['ffprobe', '-v', 'error', '-show_format', '-show_streams', video_path]
subprocess.run(cmd, check=True, capture_output=True)
first_check = True
except subprocess.CalledProcessError as e:
print(f"Invalid video file: {e.stderr.decode().strip()}")
return False
# Level 2: Video manipulation check (example: flip/cut/pause)
try:
cmd = ['ffmpeg', '-i', video_path, '-vf', 'hflip', '-f', 'null', '-']
subprocess.run(cmd, check=True, capture_output=True)
second_check = True
except subprocess.CalledProcessError:
print("Video failed manipulation check")
return False
# Level 3: TBD
# If all checks pass, the video is considered valid
if first_check and second_check:
return True
# Video Check
video_path = 'test_samples/output.MOV'
if validate_video(video_path):
print("Video is valid")
# Image Check
image_path = 'test_samples/ezgif-2-4222e09cff-orijpg.gif'
if validate_image(image_path):
print("Image is valid")