Skip to content

Commit 1fa1c84

Browse files
author
patx
committed
update streaming examples for videos
1 parent 9dba3e7 commit 1fa1c84

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

examples/streaming/video.py renamed to examples/streaming/video1.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def index(self):
1010
<html>
1111
<body>
1212
<center>
13-
<h1>Star Wars: Revenge of the Sith (III) Stream</h1>
1413
<video width="640" height="360" controls>
1514
<source src="/stream" type="video/mp4">
1615
Your browser does not support the video tag. Use Chrome for best results.
@@ -83,9 +82,3 @@ def generator(start=0, end=None):
8382

8483
# The WSGI entry point Gunicorn will look for
8584
wsgi_app = app.wsgi_app
86-
87-
if __name__ == "__main__":
88-
# Optional: run a simple server (no partial-content).
89-
# For partial-content, run via: gunicorn app:wsgi_app
90-
app.run(port=8080)
91-

examples/streaming/video2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from MicroPie import Server
3+
4+
VIDEO_PATH = "path/to/your/video.mp4"
5+
6+
class VideoStreamer(Server):
7+
def index(self):
8+
"""Serve the HTML page with a video player."""
9+
return '''
10+
<html>
11+
<body>
12+
<center>
13+
<video width="640" height="360" controls>
14+
<source src="/stream" type="video/mp4">
15+
Your browser does not support the video tag.
16+
</video>
17+
</center>
18+
</body>
19+
</html>
20+
'''
21+
22+
def stream(self):
23+
"""Stream the video file in chunks."""
24+
def generator():
25+
chunk_size = 1024 * 1024 # 1MB chunks
26+
try:
27+
with open(VIDEO_PATH, 'rb') as video:
28+
while chunk := video.read(chunk_size):
29+
yield chunk
30+
except FileNotFoundError:
31+
yield b"Video file not found."
32+
33+
return generator()
34+
35+
app = VideoStreamer()
36+
wsgi_app = app.wsgi_app

0 commit comments

Comments
 (0)