Skip to content

Commit

Permalink
update streaming examples for videos
Browse files Browse the repository at this point in the history
  • Loading branch information
patx committed Jan 23, 2025
1 parent 9dba3e7 commit 1fa1c84
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
7 changes: 0 additions & 7 deletions examples/streaming/video.py → examples/streaming/video1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def index(self):
<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.
Expand Down Expand Up @@ -83,9 +82,3 @@ def generator(start=0, end=None):

# 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)

36 changes: 36 additions & 0 deletions examples/streaming/video2.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1fa1c84

Please sign in to comment.