Neon-AI commited on
Commit
7814055
·
verified ·
1 Parent(s): e28678b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import tempfile
3
+ import subprocess
4
+ import os
5
+ from fastapi import FastAPI, UploadFile, File
6
+ from fastapi.responses import FileResponse
7
+
8
+ BLUR_W = 291
9
+ BLUR_H = 84
10
+ BLUR_KERNEL = (15, 15)
11
+ TEXT = "nt-animes"
12
+ FONT = cv2.FONT_HERSHEY_SIMPLEX
13
+ FONT_SCALE = 0.4
14
+ FONT_COLOR = (255, 255, 255)
15
+ FONT_THICKNESS = 1
16
+ TEXT_X = 8
17
+ TEXT_Y = 10
18
+
19
+ app = FastAPI(title="Video Blur + Text API")
20
+
21
+ def blur_video(input_path):
22
+ tmp_video = tempfile.mktemp(suffix="_video.mp4")
23
+ final_video = tempfile.mktemp(suffix="_final.mp4")
24
+
25
+ cap = cv2.VideoCapture(input_path)
26
+ if not cap.isOpened():
27
+ raise RuntimeError("Cannot open video")
28
+
29
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
30
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
31
+ fps = cap.get(cv2.CAP_PROP_FPS)
32
+
33
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
34
+ out = cv2.VideoWriter(tmp_video, fourcc, fps, (width, height))
35
+
36
+ while True:
37
+ ret, frame = cap.read()
38
+ if not ret:
39
+ break
40
+
41
+ roi = frame[0:BLUR_H, 0:BLUR_W]
42
+ roi = cv2.GaussianBlur(roi, BLUR_KERNEL, 0)
43
+ frame[0:BLUR_H, 0:BLUR_W] = roi
44
+ cv2.putText(frame, TEXT, (TEXT_X, TEXT_Y), FONT, FONT_SCALE, FONT_COLOR, FONT_THICKNESS, cv2.LINE_AA)
45
+
46
+ out.write(frame)
47
+
48
+ cap.release()
49
+ out.release()
50
+
51
+ # Merge audio
52
+ subprocess.run([
53
+ "ffmpeg", "-y",
54
+ "-i", tmp_video,
55
+ "-i", input_path,
56
+ "-c:v", "copy",
57
+ "-c:a", "copy",
58
+ "-map", "0:v:0",
59
+ "-map", "1:a:0",
60
+ final_video
61
+ ], check=True)
62
+
63
+ return final_video
64
+
65
+ @app.post("/blur")
66
+ async def blur_endpoint(file: UploadFile = File(...)):
67
+ # Save uploaded file temporarily
68
+ tmp_input = tempfile.mktemp(suffix=".mp4")
69
+ with open(tmp_input, "wb") as f:
70
+ f.write(await file.read())
71
+
72
+ # Process video
73
+ output_path = blur_video(tmp_input)
74
+
75
+ return FileResponse(output_path, filename="blurred_video.mp4", media_type="video/mp4")