aianyu commited on
Commit
c4b2b9d
·
verified ·
1 Parent(s): 7f5e3d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+
4
+ # Set page config
5
+ st.set_page_config(page_title="M3U8 Player", page_icon="📺", layout="centered")
6
+
7
+ st.title("在线 M3U8 播放器")
8
+ st.write("输入 M3U8 地址并点击播放按钮。")
9
+
10
+ # Input for M3U8 URL
11
+ m3u8_url = st.text_input("M3U8 URL:", placeholder="例如: https://example.com/stream.m3u8")
12
+
13
+ # Play button
14
+ play_button = st.button("播放")
15
+
16
+ if play_button and m3u8_url:
17
+ # HTML code to embed hls.js player
18
+ # hls.js library is loaded from a CDN
19
+ html_code = f"""
20
+ <!DOCTYPE html>
21
+ <html>
22
+ <head>
23
+ <title>HLS Player</title>
24
+ <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
25
+ <style>
26
+ video {{
27
+ width: 100%;
28
+ max-width: 800px; /* Limit max width */
29
+ height: auto;
30
+ display: block;
31
+ margin: 10px auto; /* Center the video */
32
+ }}
33
+ </style>
34
+ </head>
35
+ <body>
36
+ <video id="video" controls></video>
37
+ <script>
38
+ var video = document.getElementById('video');
39
+ var m3u8_url = "{m3u8_url}"; // Get URL from Python
40
+ if(Hls.isSupported()) {{
41
+ var hls = new Hls();
42
+ hls.loadSource(m3u8_url);
43
+ hls.attachMedia(video);
44
+ hls.on(Hls.Events.MANIFEST_PARSED,function() {{
45
+ // video.play(); // Autoplay after manifest is parsed - Removed for better control
46
+ }});
47
+ // Handle errors
48
+ hls.on(Hls.Events.ERROR, function (event, data) {{
49
+ console.error("HLS.js Error:", data);
50
+ if (data.fatal) {{
51
+ switch(data.type) {{
52
+ case Hls.ErrorTypes.NETWORK_ERROR:
53
+ console.error("fatal network error encountered, trying to recover");
54
+ // hls.startLoad(); // Avoid automatic restart loops
55
+ break;
56
+ case Hls.ErrorTypes.MEDIA_ERROR:
57
+ console.error("fatal media error encountered, trying to recover");
58
+ // hls.recoverMediaError(); // Avoid automatic recovery loops
59
+ break;
60
+ default:
61
+ // cannot recover
62
+ console.error("unrecoverable error encountered: ", data);
63
+ hls.destroy();
64
+ break;
65
+ }}
66
+ }}
67
+ }});
68
+ }} else if (video.canPlayType('application/vnd.apple.mpegurl')) {{
69
+ // Native HLS support (e.g., Safari)
70
+ video.src = m3u8_url;
71
+ video.addEventListener('loadedmetadata',function() {{
72
+ // video.play(); // Removed for better control
73
+ }});
74
+ }} else {{
75
+ alert("您的浏览器不支持 HLS 播放。");
76
+ }}
77
+ </script>
78
+ </body>
79
+ </html>
80
+ """
81
+ # Use Streamlit components to render the HTML
82
+ # Adjust height based on typical video aspect ratio (e.g., 16:9)
83
+ components.html(html_code, height=450, scrolling=False) # Approximate height for 16:9 video
84
+ elif play_button and not m3u8_url:
85
+ st.warning("请输入 M3U8 地址。")
86
+