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

준비물: python, ffmpeg

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

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()

그리고 ffmpeg.exe 을 이용하여 아래와 같이 사용한다.
영역값은 드래그 했을때 출력된 값 (예: 100:100:40:80)
시작시간,종료시간은 초단위 (예: 3.5)

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

评论

精华帖

Delphi TDirectory.Delete 대신 CMD 으로 폴더 삭제
开发技巧 애드센스 자동 광고 사용 시 빈 화면이 출력된다면?
WordPress xmlrpc.php 차단으로 보안 강화하기
Delphi 폼이 모니터 한 가운데 있는 경우 자연스럽게 왼쪽으로 …
Windows WSH script registration is not valid.
WordPress 제목 블록 기본값을 H5로 변경하는 방법
Python 동영상 특정 영역 모자이크(흐리게) 하기
Linux 구글 크롬 콘솔 'Attestation check for Topics on' 에…
其他 크롬 - 개발자 도구(DevTools)에서 붙여넣기 안될때
GnuBoard SmartEditor2Skin.html 다운로드 되는 현상
开发技巧 검색 로봇 접속시 종료
Linux PHP-FPM 튜닝을 위한 메모리 계산
Windows 아이폰 벨소리 쉽게 추가하기
Windows C:\Windows\Installer 폴더 다른 드라이브로 옮기기
开发技巧 REQUEST 값 받기(application/json 포함)
Linux 데몬(systemd service) 작성 및 실행하기
开发技巧 업데이트 이후 에러 발생([ERROR] Incorrect definition…
其他 구글 크롬 탭검색 비활성화
开发技巧 MySQL, MariaDB 업데이트
其他 아이폰 - 비공개 릴레이로 HTTPS 감청 막기
Windows Windows에 Rust 설치하기
开发技巧 [RN] android directory was detected in the project.
Linux 메일(Mail) 삭제/초기화
Delphi 윈도우 알림센터에 메시지 출력하기
其他 텔레그램 봇 (Telegram Bot) 만들기
开发技巧 [부트스트랩] nav 에서 하위 항목 활성화 시 부모 항목 …
Linux PHP 버전 추가하기
开发技巧 Curl 접속 속도가 늦은 경우
Windows 파일 내용 검색
开发技巧 unblock with 'mysqladmin flush-hosts' 에러