Spaces:
Paused
Paused
Update stream_server.py
Browse files- stream_server.py +23 -15
stream_server.py
CHANGED
@@ -71,7 +71,7 @@ def convert_to_hls(input_path, output_dir, video_name, start_number):
|
|
71 |
|
72 |
|
73 |
|
74 |
-
async def add_video(video_name,
|
75 |
"""
|
76 |
Endpoint to add a video to the streaming queue.
|
77 |
|
@@ -98,25 +98,33 @@ async def add_video(video_name, m3u8_file_path):
|
|
98 |
segment_counter += num_new_segments
|
99 |
|
100 |
video_names.append(video_name)
|
101 |
-
|
|
|
|
|
102 |
existing_segments = set()
|
103 |
-
if
|
104 |
-
with open(
|
105 |
for line in m3u8_file:
|
106 |
if line.startswith('/live_stream/video_stream'):
|
107 |
existing_segments.add(line.strip())
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
return {"message": f'"{video_name}" added to the streaming queue.'}
|
122 |
|
|
|
71 |
|
72 |
|
73 |
|
74 |
+
async def add_video(video_name, output_path, audio_duration):
|
75 |
"""
|
76 |
Endpoint to add a video to the streaming queue.
|
77 |
|
|
|
98 |
segment_counter += num_new_segments
|
99 |
|
100 |
video_names.append(video_name)
|
101 |
+
segment_duration = 3
|
102 |
+
full_segments = int(audio_duration // segment_duration)
|
103 |
+
remaining_duration = audio_duration % segment_duration
|
104 |
existing_segments = set()
|
105 |
+
if output_path:
|
106 |
+
with open(output_path, 'r') as m3u8_file:
|
107 |
for line in m3u8_file:
|
108 |
if line.startswith('/live_stream/video_stream'):
|
109 |
existing_segments.add(line.strip())
|
110 |
|
111 |
+
new_segments = []
|
112 |
+
# Generate new segment paths
|
113 |
+
for i in range(1, full_segments + 1):
|
114 |
+
new_segment_path = f"/live_stream/video_stream{i}.ts"
|
115 |
+
if new_segment_path not in existing_segments:
|
116 |
+
new_segments.append(new_segment_path)
|
117 |
+
|
118 |
+
with open(output_path, 'a') as m3u8_file:
|
119 |
+
for segment in new_segments:
|
120 |
+
m3u8_file.write(f"#EXTINF:{segment_duration:.3f},\n")
|
121 |
+
m3u8_file.write(f"{segment}\n")
|
122 |
+
|
123 |
+
if remaining_duration > 0:
|
124 |
+
remaining_segment_path = f"/live_stream/video_stream{full_segments + 1}.ts"
|
125 |
+
if remaining_segment_path not in existing_segments:
|
126 |
+
m3u8_file.write(f"#EXTINF:{remaining_duration:.3f},\n")
|
127 |
+
m3u8_file.write(f"{remaining_segment_path}\n")
|
128 |
|
129 |
return {"message": f'"{video_name}" added to the streaming queue.'}
|
130 |
|