@@ -423,6 +423,119 @@ Such named modules will always then be available under the
423
423
Please see the documentation (linked above) about restrictions and gotchas
424
424
when configuring how JavaScript modules are made available to PyScript.
425
425
426
+ ### ` pyscript.media `
427
+
428
+ The ` pyscript.media ` namespace provides classes and functions for interacting
429
+ with media devices and streams in a web browser. This module enables you to work
430
+ with cameras, microphones, and other media input/output devices directly from
431
+ Python code.
432
+
433
+ #### ` pyscript.media.Device `
434
+
435
+ A class that represents a media input or output device, such as a microphone,
436
+ camera, or headset.
437
+
438
+ ``` python title="Creating a Device object"
439
+ from pyscript.media import Device, list_devices
440
+
441
+ # List all available media devices
442
+ devices = await list_devices()
443
+ # Get the first available device
444
+ my_device = devices[0 ]
445
+ ```
446
+
447
+ The ` Device ` class has the following properties:
448
+
449
+ * ` id ` - a unique string identifier for the represented device.
450
+ * ` group ` - a string group identifier for devices belonging to the same physical device.
451
+ * ` kind ` - an enumerated value: "videoinput", "audioinput", or "audiooutput".
452
+ * ` label ` - a string describing the device (e.g., "External USB Webcam").
453
+
454
+ The ` Device ` class also provides the following methods:
455
+
456
+ ##### ` Device.load(audio=False, video=True) `
457
+
458
+ A class method that loads a media stream with the specified options.
459
+
460
+ ``` python title="Loading a media stream"
461
+ # Load a video stream (default)
462
+ stream = await Device.load()
463
+
464
+ # Load an audio stream only
465
+ stream = await Device.load(audio = True , video = False )
466
+
467
+ # Load with specific video constraints
468
+ stream = await Device.load(video = {" width" : 1280 , " height" : 720 })
469
+ ```
470
+
471
+ Parameters:
472
+ * ` audio ` (bool, default: False) - Whether to include audio in the stream.
473
+ * ` video ` (bool or dict, default: True) - Whether to include video in the
474
+ stream. Can also be a dictionary of video constraints.
475
+
476
+ Returns:
477
+ * A media stream object that can be used with HTML media elements.
478
+
479
+ ##### ` get_stream() `
480
+
481
+ An instance method that gets a media stream from this specific device.
482
+
483
+ ``` python title="Getting a stream from a specific device"
484
+ # Find a video input device
485
+ video_devices = [d for d in devices if d.kind == " videoinput" ]
486
+ if video_devices:
487
+ # Get a stream from the first video device
488
+ stream = await video_devices[0 ].get_stream()
489
+ ```
490
+
491
+ Returns:
492
+ * A media stream object from the specific device.
493
+
494
+ #### ` pyscript.media.list_devices() `
495
+
496
+ An async function that returns a list of all currently available media input and
497
+ output devices.
498
+
499
+ ``` python title="Listing all media devices"
500
+ from pyscript.media import list_devices
501
+
502
+ devices = await list_devices()
503
+ for device in devices:
504
+ print (f " Device: { device.label} , Kind: { device.kind} " )
505
+ ```
506
+
507
+ Returns:
508
+ * A list of ` Device ` objects representing the available media devices.
509
+
510
+ !!! Note
511
+
512
+ The returned list will omit any devices that are blocked by the document
513
+ Permission Policy or for which the user has not granted permission.
514
+
515
+ ### Simple Example
516
+
517
+ ``` python title="Basic camera access"
518
+ from pyscript import document
519
+ from pyscript.media import Device
520
+
521
+ async def init_camera ():
522
+ # Get a video stream
523
+ stream = await Device.load(video = True )
524
+
525
+ # Set the stream as the source for a video element
526
+ video_el = document.getElementById(" camera" )
527
+ video_el.srcObject = stream
528
+
529
+ # Initialize the camera
530
+ init_camera()
531
+ ```
532
+
533
+ !!! warning
534
+
535
+ Using media devices requires appropriate permissions from the user.
536
+ Browsers will typically show a permission dialog when `list_devices()` or
537
+ `Device.load()` is called.
538
+
426
539
### ` pyscript.storage `
427
540
428
541
The ` pyscript.storage ` API wraps the browser's built-in
0 commit comments