erfyhersr commited on
Commit
aba142b
·
verified ·
1 Parent(s): 32f5a59

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ 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)
24
+ return "Video not found", 404
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):
34
+ video_file = filename
35
+ break
36
+ if not video_file:
37
+ return "Video not found", 404
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>
45
+ <head>
46
+ <title>Custom Video Player</title>
47
+ <style>
48
+ body {{
49
+ margin: 0;
50
+ background-color: #000;
51
+ display: flex;
52
+ align-items: center;
53
+ justify-content: center;
54
+ height: 100vh;
55
+ }}
56
+ video {{
57
+ width: 80%;
58
+ max-width: 800px;
59
+ height: auto;
60
+ outline: none;
61
+ }}
62
+ </style>
63
+ </head>
64
+ <body>
65
+ <video controls autoplay>
66
+ <source src="{video_url}" type="video/mp4">
67
+ Your browser does not support the video tag.
68
+ </video>
69
+ </body>
70
+ </html>
71
+ """
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)
96
+ with open(filepath, "wb") as f:
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})")