알짜게시판

[파이썬] 동영상 특정 영역 모자이크(흐리게) 하기

준비물: python, ffmpeg

동영상 파일을 input.mp4 로 복사 또는 수정한다.
아래 파이썬 코드를 이용하여 흐리게 할 영역 및 시간을 구한다.
[code]
import cv2

video_path = "input.mp4"
cap = cv2.VideoCapture(video_path)

frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)

cv2.namedWindow("Video Viewer")

roi_start = None
roi_end = None
dragging = False
current_frame = None

def draw_rectangle(event, x, y, flags, param):
global roi_start, roi_end, dragging
if event == cv2.EVENT_LBUTTONDOWN:
roi_start = (x, y)
dragging = True
elif event == cv2.EVENT_MOUSEMOVE and dragging:
roi_end = (x, y)
redraw_frame()
elif event == cv2.EVENT_LBUTTONUP:
roi_end = (x, y)
dragging = False
x0, y0 = roi_start
x1, y1 = roi_end
x, y = min(x0, x1), min(y0, y1)
w, h = abs(x1 - x0), abs(y1 - y0)

if w > 0 and h > 0:
print(f"{w}:{h}:{x}:{y}")

redraw_frame()

def redraw_frame():
if current_frame is None:
return
frame = current_frame.copy()
if roi_start and roi_end:
x0, y0 = roi_start
x1, y1 = roi_end
w = abs(x1 - x0)
h = abs(y1 - y0)
if w > 0 and h > 0:
cv2.rectangle(frame, roi_start, roi_end, (255, 0, 0), 2)
frame_number = cv2.getTrackbarPos("Frame", "Video Viewer")
time_text = f"Time: {frame_number / fps:.2f}s"
cv2.putText(frame, time_text, (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("Video Viewer", frame)

def on_trackbar(val):
global current_frame
cap.set(cv2.CAP_PROP_POS_FRAMES, val)
ret, frame = cap.read()
if ret:
current_frame = frame.copy()
redraw_frame()

cv2.setMouseCallback("Video Viewer", draw_rectangle)
cv2.createTrackbar("Frame", "Video Viewer", 0, frame_count - 1, on_trackbar)

# 초기 프레임 표시
on_trackbar(0)

while True:
if cv2.getWindowProperty("Video Viewer", cv2.WND_PROP_VISIBLE) < 1:
break
key = cv2.waitKey(100)
if key == 27:
break

cap.release()
cv2.destroyAllWindows()
[/code]

그리고 ffmpeg.exe 을 이용하여 아래와 같이 사용한다.
영역값은 드래그 했을때 출력된 값 (예: 100:100:40:80)
시작시간,종료시간은 초단위 (예: 3.5)
[code]
ffmpeg -y -i input.mp4 -filter_complex "[0:v]crop={영역값},boxblur=3:1[blur]; [0:v][blur]overlay=43:65:enable='between(t,{시작시간},{종료시간})'" -c:a copy output.mp4
[/code]

로그인 후 댓글내용을 입력해주세요

번호 제목 글쓴이 조회 날짜
열람 [파이썬] 동영상 특정 영역 모자이크(흐리게) 하기 7,743 04-30
65 [리눅스] 구글 크롬 콘솔 'Attestation check for Topics on' 에러 원인 & 해결방… 22,975 04-11
64 [크롬] 개발자 도구(DevTools)에서 붙여넣기 안될때 47,482 03-10
63 [그누보드] SmartEditor2Skin.html 다운로드 되는 현상 95,294 01-08
62 [PHP] 검색 로봇 접속시 종료 55,901 10-07
61 [리눅스] PHP-FPM 튜닝을 위한 메모리 계산 40,904 08-29
60 [윈도우] 아이폰 벨소리 쉽게 추가하기 93,752 06-14
59 [윈도우] C:\Windows\Installer 폴더 다른 드라이브로 옮기기 104,367 05-29
58 [PHP] REQUEST 값 받기(application/json 포함) 98,833 04-20
57 [Linux] 데몬(systemd service) 작성 및 실행하기 65,766 03-14
56 [MySQL] 업데이트 이후 에러 발생([ERROR] Incorrect definition of table … 91,486 12-08
55 구글 크롬 탭검색 비활성화 72,339 12-04
54 [MySQL] MySQL, MariaDB 업데이트 73,595 11-24
53 [아이폰] HTTPS 감청 우회하기 266,741 11-01
52 Windows에 Rust 설치하기 57,499 10-05
51 [ReactNative] android directory was detected in the project. 65,472 07-20
50 [리눅스] 메일(Mail) 삭제/초기화 58,331 07-18
49 [Delphi] 윈도우 알림센터에 메시지 출력하기 55,555 06-20
48 텔레그램 봇 (Telegram Bot) 만들기 38,194 05-03
47 [부트스트랩] nav 에서 하위 항목 활성화 시 부모 항목 활성화 34,986 04-30
46 [리눅스] PHP 버전 추가하기 34,691 04-29
45 [PHP] Curl 접속 속도가 늦은 경우 36,699 04-10
44 [윈도우] 파일 내용 검색 45,807 02-09
43 [MySQL] unblock with 'mysqladmin flush-hosts' 에러 53,805 01-01
42 [ReactNative] 캐시 초기화 하는 방법 57,427 12-27
41 [윈도우] node.js 와 npm 업데이트(업그레이드) 57,505 12-01
40 [윈도우] 윈도우11 시작 메뉴 위치 변경(가운데, 왼쪽) 60,533 10-10
39 [Delphi] 중복 실행 방지 59,000 09-21
38 [리눅스] yum 에서 Thread died in Berkeley DB library 오류 59,099 09-18
37 [리눅스] VNSTAT 설치하기 56,153 09-17
36 [리눅스] 로그 삭제 및 비우기(?) 58,196 09-09
목록