Pepguy commited on
Commit
f07a195
·
verified ·
1 Parent(s): 1012f9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -1,19 +1,23 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
 
3
  import subprocess, base64, os, uuid, shutil, whisper
4
 
5
  app = FastAPI()
6
  whisper_model = whisper.load_model("tiny")
7
 
 
8
  class VideoJsonRequest(BaseModel):
9
  video_base64: str
 
 
10
 
11
  def to_b64(path):
12
  with open(path, "rb") as f: return base64.b64encode(f.read()).decode('utf-8')
13
 
14
  @app.get("/")
15
  async def index():
16
- return {"success": True}
17
 
18
  @app.post("/process-video")
19
  async def process(req: VideoJsonRequest):
@@ -24,34 +28,46 @@ async def process(req: VideoJsonRequest):
24
  a_p = f"{tmp}/a.wav"
25
  try:
26
  with open(v_p, "wb") as f: f.write(base64.b64decode(req.video_base64))
 
 
27
  probe = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", v_p], capture_output=True, text=True).stdout
28
  dur = float(probe.strip() or 0)
 
 
 
 
29
  print("-------------------")
30
- print(f"fps={5/max(dur,1)}")
31
- print(f"fps={1/max(dur,1)}")
32
  print("-------------------")
33
- # Extract 15 frames
 
34
  subprocess.run(["ffmpeg", "-y",
35
  "-loglevel", "error",
36
- "-i", v_p, "-vf", f"fps={3/max(dur,1)}", "-vframes", "15", "-q:v", "5", f"{tmp}/f_%03d.jpg"])
37
- # Transcribe
38
- subprocess.run(["ffmpeg", "-y",
39
- "-loglevel", "error",
40
- "-i", v_p, "-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1", a_p])
41
- #txt = whisper_model.transcribe(a_p)["text"].strip()
42
- result = whisper_model.transcribe(a_p)
43
-
44
- lines = [
45
- f"[{s['start']:.2f}] {s['text'].strip()}"
46
- for s in result["segments"]
47
- ]
 
 
 
48
 
49
- txt = "\n".join(lines)
50
  f_names = sorted([f"{tmp}/{f}" for f in os.listdir(tmp) if f.startswith("f_")])
51
  imgs = [to_b64(f) for f in f_names]
 
52
  print("-------------------")
53
- print(f"length={len(imgs)}")
54
  print("-------------------")
 
55
  return {"success": True, "transcript": txt, "frames": imgs, "thumbnail": imgs[0] if imgs else None}
56
  except Exception as e: return {"success": False, "error": str(e)}
57
  finally: shutil.rmtree(tmp, ignore_errors=True)
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+ from typing import Optional
4
  import subprocess, base64, os, uuid, shutil, whisper
5
 
6
  app = FastAPI()
7
  whisper_model = whisper.load_model("tiny")
8
 
9
+ # 🚨 DYNAMIC SCHEMA
10
  class VideoJsonRequest(BaseModel):
11
  video_base64: str
12
+ num_frames: Optional[int] = 15 # Default to 15
13
+ get_transcript: Optional[bool] = True # Default to True
14
 
15
  def to_b64(path):
16
  with open(path, "rb") as f: return base64.b64encode(f.read()).decode('utf-8')
17
 
18
  @app.get("/")
19
  async def index():
20
+ return {"success": True, "engine": "Dynamic Viral Cat Media Server"}
21
 
22
  @app.post("/process-video")
23
  async def process(req: VideoJsonRequest):
 
28
  a_p = f"{tmp}/a.wav"
29
  try:
30
  with open(v_p, "wb") as f: f.write(base64.b64decode(req.video_base64))
31
+
32
+ # Get Duration
33
  probe = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", v_p], capture_output=True, text=True).stdout
34
  dur = float(probe.strip() or 0)
35
+
36
+ # 🚨 DYNAMIC FRAME MATH: Spreads requested frames evenly across duration
37
+ calc_fps = req.num_frames / max(dur, 1)
38
+
39
  print("-------------------")
40
+ print(f"Requested Frames: {req.num_frames} | Duration: {dur:.2f}s | Calculated FPS: {calc_fps:.2f}")
41
+ print(f"Transcript requested: {req.get_transcript}")
42
  print("-------------------")
43
+
44
+ # Extract X frames
45
  subprocess.run(["ffmpeg", "-y",
46
  "-loglevel", "error",
47
+ "-i", v_p, "-vf", f"fps={calc_fps}",
48
+ "-vframes", str(req.num_frames),
49
+ "-q:v", "5", f"{tmp}/f_%03d.jpg"])
50
+
51
+ # 🚨 CONDITIONAL TRANSCRIPT
52
+ txt = ""
53
+ if req.get_transcript:
54
+ subprocess.run(["ffmpeg", "-y",
55
+ "-loglevel", "error",
56
+ "-i", v_p, "-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1", a_p])
57
+
58
+ if os.path.exists(a_p):
59
+ result = whisper_model.transcribe(a_p)
60
+ lines = [f"[{s['start']:.2f}] {s['text'].strip()}" for s in result["segments"]]
61
+ txt = "\n".join(lines)
62
 
63
+ # Gather frames
64
  f_names = sorted([f"{tmp}/{f}" for f in os.listdir(tmp) if f.startswith("f_")])
65
  imgs = [to_b64(f) for f in f_names]
66
+
67
  print("-------------------")
68
+ print(f"Successfully extracted {len(imgs)} images.")
69
  print("-------------------")
70
+
71
  return {"success": True, "transcript": txt, "frames": imgs, "thumbnail": imgs[0] if imgs else None}
72
  except Exception as e: return {"success": False, "error": str(e)}
73
  finally: shutil.rmtree(tmp, ignore_errors=True)