-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update streaming examples for videos
- Loading branch information
patx
committed
Jan 23, 2025
1 parent
9dba3e7
commit 1fa1c84
Showing
2 changed files
with
36 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |