erfyhersr commited on
Commit
15bd3a5
·
verified ·
1 Parent(s): ee7005e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
app.py CHANGED
@@ -3,21 +3,19 @@ import uuid
3
  import threading
4
  from flask import Flask, send_from_directory, render_template_string
5
  import streamlit as st
 
6
 
7
- # Directory to save uploaded videos
8
  VIDEO_DIR = "videos"
9
  if not os.path.exists(VIDEO_DIR):
10
  os.makedirs(VIDEO_DIR)
11
 
12
- # Flask app to serve videos and the custom embed page
13
  app = Flask(__name__)
14
 
15
  @app.route('/video/<video_id>')
16
  def serve_video(video_id):
17
- """
18
- Serves the video file by matching the unique video_id.
19
- """
20
- # Look for a file that starts with the video_id
21
  for filename in os.listdir(VIDEO_DIR):
22
  if filename.startswith(video_id):
23
  return send_from_directory(VIDEO_DIR, filename)
@@ -25,9 +23,7 @@ def serve_video(video_id):
25
 
26
  @app.route('/embed/<video_id>')
27
  def embed_video(video_id):
28
- """
29
- Returns an HTML page with a custom video player that streams the video.
30
- """
31
  video_file = None
32
  for filename in os.listdir(VIDEO_DIR):
33
  if filename.startswith(video_id):
@@ -38,7 +34,6 @@ def embed_video(video_id):
38
 
39
  # The video source URL points to the /video/<video_id> route.
40
  video_url = f"/video/{video_id}"
41
- # Custom HTML for the video player
42
  html = f"""
43
  <!DOCTYPE html>
44
  <html>
@@ -72,24 +67,25 @@ def embed_video(video_id):
72
  return render_template_string(html)
73
 
74
  def run_flask():
75
- # Run the Flask app on port 8000.
76
- app.run(port=8000)
77
 
78
  # Start the Flask server in a background thread (only once).
79
  if "FLASK_STARTED" not in st.session_state:
80
  st.session_state.FLASK_STARTED = True
81
- thread = threading.Thread(target=run_flask, daemon=True)
82
- thread.start()
83
 
84
- # --- Streamlit Interface ---
 
 
 
85
 
86
  st.title("Cloud Video Hosting and Streaming")
87
 
88
  uploaded_file = st.file_uploader("Upload a video", type=["mp4", "mov", "avi", "mkv"])
89
  if uploaded_file is not None:
90
- # Generate a unique id for the video
91
  video_id = str(uuid.uuid4())
92
- # Preserve the original extension
93
  ext = uploaded_file.name.split('.')[-1]
94
  filename = f"{video_id}.{ext}"
95
  filepath = os.path.join(VIDEO_DIR, filename)
@@ -97,15 +93,17 @@ if uploaded_file is not None:
97
  f.write(uploaded_file.getbuffer())
98
  st.success("Video uploaded successfully!")
99
 
100
- # Provide the embed link to stream the video with the custom media player
101
- embed_link = f"http://localhost:8000/embed/{video_id}"
 
102
  st.write("Your embed link:")
103
  st.code(embed_link)
104
  st.markdown(f"[Open Video]({embed_link})")
105
 
106
- # Optional: List available videos
107
  st.subheader("Available Videos")
108
  for fname in os.listdir(VIDEO_DIR):
109
  vid_id = fname.split('.')[0]
110
- embed_link = f"http://localhost:8000/embed/{vid_id}"
 
111
  st.markdown(f"- [Video {vid_id}]({embed_link})")
 
3
  import threading
4
  from flask import Flask, send_from_directory, render_template_string
5
  import streamlit as st
6
+ from pyngrok import ngrok
7
 
8
+ # Directory to store videos
9
  VIDEO_DIR = "videos"
10
  if not os.path.exists(VIDEO_DIR):
11
  os.makedirs(VIDEO_DIR)
12
 
13
+ # Create a Flask app to serve videos and an embed page
14
  app = Flask(__name__)
15
 
16
  @app.route('/video/<video_id>')
17
  def serve_video(video_id):
18
+ """Serves the video file based on the unique video_id."""
 
 
 
19
  for filename in os.listdir(VIDEO_DIR):
20
  if filename.startswith(video_id):
21
  return send_from_directory(VIDEO_DIR, filename)
 
23
 
24
  @app.route('/embed/<video_id>')
25
  def embed_video(video_id):
26
+ """Returns a custom HTML page with a video player streaming the video."""
 
 
27
  video_file = None
28
  for filename in os.listdir(VIDEO_DIR):
29
  if filename.startswith(video_id):
 
34
 
35
  # The video source URL points to the /video/<video_id> route.
36
  video_url = f"/video/{video_id}"
 
37
  html = f"""
38
  <!DOCTYPE html>
39
  <html>
 
67
  return render_template_string(html)
68
 
69
  def run_flask():
70
+ # Bind to 0.0.0.0 to allow external connections and run on port 8000
71
+ app.run(host="0.0.0.0", port=8000)
72
 
73
  # Start the Flask server in a background thread (only once).
74
  if "FLASK_STARTED" not in st.session_state:
75
  st.session_state.FLASK_STARTED = True
76
+ threading.Thread(target=run_flask, daemon=True).start()
 
77
 
78
+ # Setup ngrok to expose port 8000 publicly (only once).
79
+ if "NGROK_URL" not in st.session_state:
80
+ public_url = ngrok.connect(8000, bind_tls=True)
81
+ st.session_state.NGROK_URL = public_url.public_url
82
 
83
  st.title("Cloud Video Hosting and Streaming")
84
 
85
  uploaded_file = st.file_uploader("Upload a video", type=["mp4", "mov", "avi", "mkv"])
86
  if uploaded_file is not None:
87
+ # Save the uploaded video using a unique id
88
  video_id = str(uuid.uuid4())
 
89
  ext = uploaded_file.name.split('.')[-1]
90
  filename = f"{video_id}.{ext}"
91
  filepath = os.path.join(VIDEO_DIR, filename)
 
93
  f.write(uploaded_file.getbuffer())
94
  st.success("Video uploaded successfully!")
95
 
96
+ # Generate a public embed link using the ngrok public URL
97
+ public_url = st.session_state.NGROK_URL
98
+ embed_link = f"{public_url}/embed/{video_id}"
99
  st.write("Your embed link:")
100
  st.code(embed_link)
101
  st.markdown(f"[Open Video]({embed_link})")
102
 
103
+ # Optional: List available videos with their public embed links
104
  st.subheader("Available Videos")
105
  for fname in os.listdir(VIDEO_DIR):
106
  vid_id = fname.split('.')[0]
107
+ public_url = st.session_state.NGROK_URL
108
+ embed_link = f"{public_url}/embed/{vid_id}"
109
  st.markdown(f"- [Video {vid_id}]({embed_link})")