Spanicin commited on
Commit
581abfa
·
verified ·
1 Parent(s): 60d3a1b

Update stream_server.py

Browse files
Files changed (1) hide show
  1. 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, m3u8_file_path):
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 m3u8_file_path.exists():
104
- with open(m3u8_file_path, 'r') as m3u8_file:
105
  for line in m3u8_file:
106
  if line.startswith('/live_stream/video_stream'):
107
  existing_segments.add(line.strip())
108
 
109
- new_ts_files = sorted([f for f in os.listdir(HLS_DIR) if f.startswith('video_stream') and f.endswith('.ts')])
110
-
111
- with open(m3u8_file_path, 'a') as m3u8_file:
112
- for ts_file in new_ts_files[-num_new_segments:]: # Only process the new segments
113
- segment_path = f"/live_stream/{ts_file}"
114
- if segment_path not in existing_segments: # Check if already exists
115
- m3u8_file.write(f"#EXTINF:3.000,\n") # Adjust duration as necessary
116
- m3u8_file.write(f"{segment_path}\n")
117
-
118
- # Update concatenated playlist
119
- #await concatenate_playlists(video_names, HLS_DIR)
 
 
 
 
 
 
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