Skip to content

Instantly share code, notes, and snippets.

@t2psyto
Created January 1, 2018 07:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save t2psyto/f9189acb15619a1e4d3b700a63d0ccf1 to your computer and use it in GitHub Desktop.
Save t2psyto/f9189acb15619a1e4d3b700a63d0ccf1 to your computer and use it in GitHub Desktop.
Brython webcam example
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Camera with mediaDevice</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.4.0/brython.min.js"></script>
</head>
<body onload="brython()">
<h1>Brython webcam example</h1>
<div id='main'><div>
<script type="text/python">
import browser
from browser import html, document, window
aa = document['main']
videotag = html.VIDEO(id='video', width='320', height='240')
aa.html = ""
aa <= videotag
from javascript import JSObject
createUrl = JSObject(window.URL.createObjectURL)
def OnSuccess(stream):
video = document['video']
video.src = createUrl(stream)
video.play()
browser.window.navigator.mediaDevices.getUserMedia(
{"video": True, "audio": False}
).then(OnSuccess)
</script>
</body>
</html>
@PierreQuentel
Copy link

Very nice !

You can simplify the Python code : JSObject is no longer needed (there is a deprecation message in the console), and you don't need to set an id to the video element.

from browser import html, document, window

video = html.VIDEO(width=320, height=240)
document['main'] <= video

createUrl = window.URL.createObjectURL

def success(stream):
  video.src = createUrl(stream)
  video.play()

window.navigator.mediaDevices.getUserMedia(
    {"video": True, "audio": False}
    ).then(success)

@t2psyto
Copy link
Author

t2psyto commented Mar 3, 2018

Pierre, thanks for advice!
and sorry for did not noticing your comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment