zmbfeng commited on
Commit
8b0fea4
·
1 Parent(s): 6b88a7b

got audio to work at different url master.m3u8

Browse files
Files changed (2) hide show
  1. app.py +36 -2
  2. hls/master.m3u8 +9 -0
app.py CHANGED
@@ -22,6 +22,7 @@ to_add_next_video=False
22
  async def websocket_endpoint(websocket: WebSocket):
23
  await websocket.accept()
24
  global to_add_next_video
 
25
  try:
26
  while True:
27
  data = await websocket.receive_text()
@@ -29,12 +30,15 @@ async def websocket_endpoint(websocket: WebSocket):
29
  event_data = json.loads(data)
30
  if event_data['event'] == "video-nearing-end":
31
  to_add_next_video=True
 
 
32
  except Exception as e:
33
  print("Error:", e)
34
  finally:
35
  await websocket.close()
36
 
37
  segment_counter = 0
 
38
  @app.get("/hls/output.m3u8", response_class=PlainTextResponse)
39
  def generate_playlist():
40
  global segment_counter
@@ -61,11 +65,41 @@ def generate_playlist():
61
 
62
  # Add the segment multiple times based on the counter
63
  if to_add_next_video:
64
- for _ in range(segment_counter + 1):
 
 
 
 
 
 
 
 
 
 
 
65
  playlist_content.append(f"#EXTINF:{segment_duration},")
66
  playlist_content.append("output0.ts")
67
  segment_counter += 1 # Increase the counter each time the playlist is requested
68
  to_add_next_video=False
69
 
70
  return "\n".join(playlist_content)
71
- app.mount("/hls", StaticFiles(directory="hls"), name="hls")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  async def websocket_endpoint(websocket: WebSocket):
23
  await websocket.accept()
24
  global to_add_next_video
25
+ global use_video_with_audio
26
  try:
27
  while True:
28
  data = await websocket.receive_text()
 
30
  event_data = json.loads(data)
31
  if event_data['event'] == "video-nearing-end":
32
  to_add_next_video=True
33
+ if event_data['event'] == "Button was clicked":
34
+ use_video_with_audio=True
35
  except Exception as e:
36
  print("Error:", e)
37
  finally:
38
  await websocket.close()
39
 
40
  segment_counter = 0
41
+ use_video_with_audio = False
42
  @app.get("/hls/output.m3u8", response_class=PlainTextResponse)
43
  def generate_playlist():
44
  global segment_counter
 
65
 
66
  # Add the segment multiple times based on the counter
67
  if to_add_next_video:
68
+ # for _ in range(segment_counter + 1):
69
+ for _ in range(segment_counter):
70
+ playlist_content.append(f"#EXTINF:{segment_duration},")
71
+ playlist_content.append("output0.ts")
72
+
73
+ if to_add_next_video:
74
+ playlist_content.append(f"#EXTINF:{segment_duration},")
75
+ playlist_content.append("output_with_audio0.ts")
76
+ to_add_next_video=False
77
+ playlist_content.append(f"#EXTINF:{segment_duration},")
78
+ playlist_content.append("output0.ts")
79
+ else:
80
  playlist_content.append(f"#EXTINF:{segment_duration},")
81
  playlist_content.append("output0.ts")
82
  segment_counter += 1 # Increase the counter each time the playlist is requested
83
  to_add_next_video=False
84
 
85
  return "\n".join(playlist_content)
86
+ app.mount("/hls", StaticFiles(directory="hls"), name="hls")
87
+
88
+
89
+ # command = [
90
+ # "ffmpeg",
91
+ # "-i", input_file,
92
+ # "-profile:v", "baseline",
93
+ # "-level", "3.0",
94
+ # "-start_number", "0",
95
+ # "-hls_time", "4",
96
+ # "-hls_list_size", "0",
97
+ # "-f", "hls",
98
+ # output_playlist
99
+ # ]
100
+ # ffmpeg -i <input_file> -profile:v baseline -level 3.0 -start_number 0 -hls_time 4 -hls_list_size 0 -f hls <output_playlist>
101
+ #
102
+ # ffmpeg -i Liv_with_audio.mp4 -profile:v baseline -level 3.0 -c:v libx264 -b:v 1500k -maxrate 1500k -bufsize 3000k -vf "scale=-2:720" -g 48 -hls_time 4 -hls_playlist_type vod -hls_segment_filename "video%03d.ts" video.m3u8
103
+ #
104
+ # # Encode audio
105
+ # ffmpeg -i Liv_with_audio.mp4 -c:a aac -b:a 192k -hls_time 4 -hls_playlist_type vod -hls_segment_filename "audio%03d.ts" audio.m3u8
hls/master.m3u8 CHANGED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ #EXTM3U
2
+ #EXT-X-VERSION:3
3
+
4
+ # Audio
5
+ #EXT-X-MEDIA:TYPE=AUDIO, GROUP-ID="audio", NAME="English", DEFAULT=YES, URI="audio.m3u8"
6
+
7
+ # Video
8
+ #EXT-X-STREAM-INF:BANDWIDTH=1500000, RESOLUTION=1280x720, AUDIO="audio"
9
+ video.m3u8