-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
47 lines (37 loc) · 1.53 KB
/
utils.py
File metadata and controls
47 lines (37 loc) · 1.53 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
import subprocess
import cv2
def get_video_rotation(image_path): # 회전 정보 확인 함수
try:
command = ['ffmpeg', '-i', image_path]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.stderr:
for line in result.stderr.splitlines():
if "displaymatrix: rotation" in line:
rotation = line.split('rotation of')[-1].strip()
print(f"회전 정보 있음: {float(rotation.split()[0])} degrees")
return float(rotation.split()[0])
return None
except Exception as e:
print(f"메타데이터 추출 중 오류 발생: {str(e)}")
return None
def rotate_video(image_path, rotation):
cap = cv2.VideoCapture(image_path)
if not cap.isOpened():
print("비디오 파일을 열 수 없습니다.")
return None
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
output_path = f"{image_path.split('.')[0]}_rotated.mp4"
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 30, (height, width))
while True:
ret, frame = cap.read()
if not ret:
break
if rotation == -90:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
elif rotation == 90:
frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
out.write(frame)
cap.release()
out.release()
return output_path