Spanicin commited on
Commit
6775f15
·
verified ·
1 Parent(s): d90e3f2

Update stream_server.py

Browse files
Files changed (1) hide show
  1. stream_server.py +16 -10
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: str):
75
  """
76
  Endpoint to add a video to the streaming queue.
77
 
@@ -98,16 +98,22 @@ async def add_video(video_name: str):
98
  segment_counter += num_new_segments
99
 
100
  video_names.append(video_name)
101
- video_file_name = os.path.basename(video_name)
102
- # Append .ts segments to the corresponding .m3u8 file
103
- m3u8_file_path = HLS_DIR / f'{video_name}.m3u8'
104
- # ts_files = sorted([f for f in os.listdir(HLS_DIR) if f.startswith('video_stream') and f.endswith('.ts')])
105
-
106
- # Open the M3U8 file in append mode and write the new segments
 
 
 
 
107
  with open(m3u8_file_path, 'a') as m3u8_file:
108
- # for ts_file in ts_files[-num_new_segments:]:
109
- m3u8_file.write(f"#EXTINF:3.000,\n") # Adjust duration as necessary
110
- m3u8_file.write(f"/live_stream/{video_file_name}\n")
 
 
111
 
112
  # Update concatenated playlist
113
  #await concatenate_playlists(video_names, HLS_DIR)
 
71
 
72
 
73
 
74
+ async def add_video(video_name: str, m3u8_file_path: Path):
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
+
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)