vicentemovil commited on
Commit
f8c0c02
·
1 Parent(s): 2c394d6

Add image capture endpoint (still PNG via Page.captureScreenshot)

Browse files
Files changed (1) hide show
  1. app.py +164 -53
app.py CHANGED
@@ -344,23 +344,10 @@ def _cp(path: str | None) -> str | None:
344
  return dst
345
 
346
 
347
- async def _record_async(
348
- url: str, w: int, h: int, duration: int,
349
- output_path: str,
350
- audio_path: str | None,
351
- pre_rec_path: str | None,
352
- post_rec_path: str | None,
353
- postroll_path: str | None,
354
- watermark_path: str | None,
355
- ):
356
- t0 = time.monotonic()
357
- def log(msg):
358
- print(f"[wr {time.monotonic()-t0:.1f}s] {msg}", flush=True)
359
-
360
  port = get_free_port()
361
  chromium = get_chromium_path()
362
- log(f"launching chromium on port {port}, url={url}, size={w}x{h}, duration={duration}")
363
-
364
  proc = subprocess.Popen(
365
  [
366
  chromium,
@@ -378,22 +365,75 @@ async def _record_async(
378
  stdout=subprocess.DEVNULL,
379
  stderr=subprocess.DEVNULL,
380
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
 
382
  try:
383
- ws_url = None
384
- async with aiohttp.ClientSession() as session:
385
- for attempt in range(20):
386
- await asyncio.sleep(0.5)
387
- try:
388
- async with session.get(f"http://localhost:{port}/json/list") as resp:
389
- pages = await resp.json()
390
- if pages:
391
- ws_url = pages[0]["webSocketDebuggerUrl"]
392
- log(f"chrome devtools ready after {attempt+1} attempts")
393
- break
394
- except Exception as e:
395
- log(f"devtools attempt {attempt+1} failed: {e}")
396
- continue
397
 
398
  if not ws_url:
399
  raise gr.Error("Could not connect to Chrome DevTools.")
@@ -416,30 +456,7 @@ async def _record_async(
416
  "width": w, "height": h, "deviceScaleFactor": 1, "mobile": False,
417
  })
418
 
419
- log(f"navigating to {url}")
420
- nav_id = await send("Page.navigate", {"url": url})
421
- deadline = time.monotonic() + 10
422
- while time.monotonic() < deadline:
423
- try:
424
- raw = await asyncio.wait_for(ws.recv(), timeout=1.0)
425
- if json.loads(raw).get("id") == nav_id:
426
- log("Page.navigate acknowledged")
427
- break
428
- except asyncio.TimeoutError:
429
- continue
430
-
431
- log("waiting for Page.loadEventFired (max 15s)")
432
- deadline = time.monotonic() + 15
433
- while time.monotonic() < deadline:
434
- try:
435
- raw = await asyncio.wait_for(ws.recv(), timeout=1.0)
436
- if json.loads(raw).get("method") == "Page.loadEventFired":
437
- log("Page.loadEventFired received")
438
- break
439
- except asyncio.TimeoutError:
440
- continue
441
- else:
442
- log("load timeout — starting screencast anyway")
443
 
444
  await send("Runtime.enable")
445
  await send("Runtime.evaluate", {
@@ -509,6 +526,91 @@ async def _record_async(
509
  return output_path
510
 
511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
512
  async def record(
513
  url: str,
514
  width: float,
@@ -601,5 +703,14 @@ with gr.Blocks(title="Website Renderer") as demo:
601
  outputs=video_output,
602
  )
603
 
 
 
 
 
 
 
 
 
 
604
  if __name__ == "__main__":
605
  demo.launch()
 
344
  return dst
345
 
346
 
347
+ def _launch_chromium(w: int, h: int):
348
+ """Launch headless Chromium with CDP enabled. Returns (proc, port)."""
 
 
 
 
 
 
 
 
 
 
 
349
  port = get_free_port()
350
  chromium = get_chromium_path()
 
 
351
  proc = subprocess.Popen(
352
  [
353
  chromium,
 
365
  stdout=subprocess.DEVNULL,
366
  stderr=subprocess.DEVNULL,
367
  )
368
+ return proc, port
369
+
370
+
371
+ async def _connect_cdp(port: int, log):
372
+ """Wait for the Chrome DevTools websocket URL and return it."""
373
+ async with aiohttp.ClientSession() as session:
374
+ for attempt in range(20):
375
+ await asyncio.sleep(0.5)
376
+ try:
377
+ async with session.get(f"http://localhost:{port}/json/list") as resp:
378
+ pages = await resp.json()
379
+ if pages:
380
+ log(f"chrome devtools ready after {attempt+1} attempts")
381
+ return pages[0]["webSocketDebuggerUrl"]
382
+ except Exception as e:
383
+ log(f"devtools attempt {attempt+1} failed: {e}")
384
+ continue
385
+ return None
386
+
387
+
388
+ async def _navigate_and_wait(ws, send, url, log):
389
+ """Navigate to url and wait for Page.loadEventFired (max 15s). Returns False on timeout."""
390
+ log(f"navigating to {url}")
391
+ nav_id = await send("Page.navigate", {"url": url})
392
+ deadline = time.monotonic() + 10
393
+ while time.monotonic() < deadline:
394
+ try:
395
+ raw = await asyncio.wait_for(ws.recv(), timeout=1.0)
396
+ if json.loads(raw).get("id") == nav_id:
397
+ log("Page.navigate acknowledged")
398
+ break
399
+ except asyncio.TimeoutError:
400
+ continue
401
+
402
+ log("waiting for Page.loadEventFired (max 15s)")
403
+ deadline = time.monotonic() + 15
404
+ while time.monotonic() < deadline:
405
+ try:
406
+ raw = await asyncio.wait_for(ws.recv(), timeout=1.0)
407
+ if json.loads(raw).get("method") == "Page.loadEventFired":
408
+ log("Page.loadEventFired received")
409
+ return True
410
+ except asyncio.TimeoutError:
411
+ continue
412
+ log("load timeout — proceeding anyway")
413
+ return False
414
+
415
+
416
+ async def _record_async(
417
+ url: str, w: int, h: int, duration: int,
418
+ output_path: str,
419
+ audio_path: str | None,
420
+ pre_rec_path: str | None,
421
+ post_rec_path: str | None,
422
+ postroll_path: str | None,
423
+ watermark_path: str | None,
424
+ ):
425
+ t0 = time.monotonic()
426
+ def log(msg):
427
+ print(f"[wr {time.monotonic()-t0:.1f}s] {msg}", flush=True)
428
+
429
+ port = get_free_port()
430
+ chromium = get_chromium_path()
431
+ log(f"launching chromium on port {port}, url={url}, size={w}x{h}, duration={duration}")
432
+
433
+ proc, port = _launch_chromium(w, h)
434
 
435
  try:
436
+ ws_url = await _connect_cdp(port, log)
 
 
 
 
 
 
 
 
 
 
 
 
 
437
 
438
  if not ws_url:
439
  raise gr.Error("Could not connect to Chrome DevTools.")
 
456
  "width": w, "height": h, "deviceScaleFactor": 1, "mobile": False,
457
  })
458
 
459
+ _navigate_and_wait(ws, send, url, log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
 
461
  await send("Runtime.enable")
462
  await send("Runtime.evaluate", {
 
526
  return output_path
527
 
528
 
529
+ async def _capture_async(
530
+ url: str, w: int, h: int,
531
+ output_path: str,
532
+ ):
533
+ t0 = time.monotonic()
534
+ def log(msg):
535
+ print(f"[wr {time.monotonic()-t0:.1f}s] {msg}", flush=True)
536
+
537
+ log(f"launching chromium, url={url}, size={w}x{h}")
538
+ proc, port = _launch_chromium(w, h)
539
+
540
+ try:
541
+ ws_url = await _connect_cdp(port, log)
542
+ if not ws_url:
543
+ raise gr.Error("Could not connect to Chrome DevTools.")
544
+
545
+ async with websockets.connect(ws_url, max_size=50_000_000) as ws:
546
+ msg_id = 1
547
+
548
+ async def send(method, params=None):
549
+ nonlocal msg_id
550
+ current_id = msg_id
551
+ await ws.send(json.dumps({"id": current_id, "method": method, "params": params or {}}))
552
+ msg_id += 1
553
+ return current_id
554
+
555
+ await send("Page.enable")
556
+ await send("Emulation.setDeviceMetricsOverride", {
557
+ "width": w, "height": h, "deviceScaleFactor": 1, "mobile": False,
558
+ })
559
+
560
+ _navigate_and_wait(ws, send, url, log)
561
+
562
+ log("settling 1s for paint/fonts...")
563
+ await asyncio.sleep(1.0)
564
+
565
+ log("capturing screenshot")
566
+ shot_id = await send("Page.captureScreenshot", {
567
+ "format": "png",
568
+ "fromSurface": True,
569
+ "captureBeyondViewport": False,
570
+ })
571
+ png_b64 = None
572
+ deadline = time.monotonic() + 10
573
+ while time.monotonic() < deadline:
574
+ try:
575
+ raw = await asyncio.wait_for(ws.recv(), timeout=1.0)
576
+ obj = json.loads(raw)
577
+ if obj.get("id") == shot_id:
578
+ png_b64 = obj["result"]["data"]
579
+ break
580
+ except asyncio.TimeoutError:
581
+ continue
582
+ if not png_b64:
583
+ raise gr.Error("Screenshot capture failed.")
584
+
585
+ with open(output_path, "wb") as f:
586
+ f.write(base64.b64decode(png_b64))
587
+ log(f"screenshot saved: {output_path}")
588
+
589
+ finally:
590
+ proc.terminate()
591
+ proc.wait()
592
+ log("chromium terminated")
593
+
594
+ return output_path
595
+
596
+
597
+ async def capture(
598
+ url: str,
599
+ width: float,
600
+ height: float,
601
+ ) -> str:
602
+ if not url.strip():
603
+ raise gr.Error("Please enter a URL.")
604
+ if not url.startswith(("http://", "https://")):
605
+ url = "https://" + url
606
+
607
+ w = max(2, (int(width) // 2) * 2)
608
+ h = max(2, (int(height) // 2) * 2)
609
+
610
+ output_path = f"/tmp/capture_{os.getpid()}_{int(time.time())}.png"
611
+ return await _capture_async(url, w, h, output_path)
612
+
613
+
614
  async def record(
615
  url: str,
616
  width: float,
 
703
  outputs=video_output,
704
  )
705
 
706
+ capture_btn = gr.Button("Capture image", variant="secondary")
707
+ image_output = gr.Image(label="Captured Image", type="filepath")
708
+
709
+ capture_btn.click(
710
+ fn=capture,
711
+ inputs=[url_input, width_input, height_input],
712
+ outputs=image_output,
713
+ )
714
+
715
  if __name__ == "__main__":
716
  demo.launch()