Multi-DJ livestream toolkit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.4 KiB

import sys
import os
import tempfile
import shutil
import base64
import trio
from trio_cdp import page, emulation, dom
from ..chromium import Chromium
from ..filters import FFmpegChain, FFmpegFilter
from .base import OBSSource
async def do_screencast(url, outfile, dims):
c = Chromium()
async with await c.connect() as conn, await as sess, sess.page_enable():
# Set viewport size and transparency.
await emulation.set_device_metrics_override(
width=dims[0], height=dims[1],
device_scale_factor=1.0, mobile=False
await emulation.set_default_background_color_override(dom.RGBA(0, 0, 0, 0))
# Navigate to the website.
async with sess.wait_for(page.LoadEventFired):
await page.navigate(url)
# Start capture.
await page.start_screencast(format_='jpeg', quality=60)
async for frame in sess.listen(page.ScreencastFrame):
await page.screencast_frame_ack(frame.session_id)
def capture_page(url, outfile, dims=(1280, 720)):, url, outfile, dims, restrict_keyboard_interrupt_to_checkpoints=True)
class OBSBrowserSource(OBSSource):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.local = False
self.url = None
self.width = 1280
self.height = 720 = False
self.tempdir = tempfile.mkdtemp()
def __del__(self):
shutil.rmtree(self.tempdir, ignore_errors=True)
def load(self, data): = data['name']
self.local = data['settings']['is_local_file']
if self.local:
self.url = data['settings']['local_file']
self.url = data['settings']['url']
self.width = data['settings']['width']
self.height = data['settings']['height'] = data['settings']['reroute_audio']
def to_ffmpeg(self, scene):
fifo_path = os.path.join(self.tempdir, 'frame.bin')
fifo = os.mkfifo(fifo_path)
runner = functools.partial(capture_page, url=self.url, outfile=fifo_path, dims=(self.width, self.height))
return [FFmpegChain(
inputs=[FFmpegInput(fifo_path, f='image2pipe', framerate=runner.rate, follow=1, probesize=8192)],