patx/micropie
update streaming examples for videos
Commit 1fa1c84 · patx · 2025-01-22T21:23:39-05:00
Comments
No comments yet.
Diff
diff --git a/examples/streaming/video.py b/examples/streaming/video1.py
similarity index 92%
rename from examples/streaming/video.py
rename to examples/streaming/video1.py
index 3713590..9ac67eb 100644
--- a/examples/streaming/video.py
+++ b/examples/streaming/video1.py
@@ -10,7 +10,6 @@ class VideoStreamer(Server):
<html>
<body>
<center>
- <h1>Star Wars: Revenge of the Sith (III) Stream</h1>
<video width="640" height="360" controls>
<source src="/stream" type="video/mp4">
Your browser does not support the video tag. Use Chrome for best results.
@@ -83,9 +82,3 @@ app = VideoStreamer()
# The WSGI entry point Gunicorn will look for
wsgi_app = app.wsgi_app
-
-if __name__ == "__main__":
- # Optional: run a simple server (no partial-content).
- # For partial-content, run via: gunicorn app:wsgi_app
- app.run(port=8080)
-
diff --git a/examples/streaming/video2.py b/examples/streaming/video2.py
new file mode 100644
index 0000000..a9b1248
--- /dev/null
+++ b/examples/streaming/video2.py
@@ -0,0 +1,36 @@
+import os
+from MicroPie import Server
+
+VIDEO_PATH = "path/to/your/video.mp4"
+
+class VideoStreamer(Server):
+ def index(self):
+ """Serve the HTML page with a video player."""
+ return '''
+ <html>
+ <body>
+ <center>
+ <video width="640" height="360" controls>
+ <source src="/stream" type="video/mp4">
+ Your browser does not support the video tag.
+ </video>
+ </center>
+ </body>
+ </html>
+ '''
+
+ def stream(self):
+ """Stream the video file in chunks."""
+ def generator():
+ chunk_size = 1024 * 1024 # 1MB chunks
+ try:
+ with open(VIDEO_PATH, 'rb') as video:
+ while chunk := video.read(chunk_size):
+ yield chunk
+ except FileNotFoundError:
+ yield b"Video file not found."
+
+ return generator()
+
+app = VideoStreamer()
+wsgi_app = app.wsgi_app