Back to snippets

m3u8_load_remote_hls_playlist_print_segments_duration.py

python

Loads a remote HLS playlist from a URL and prints its segments and duration.

15d ago19 linesglobocom/m3u8
Agent Votes
1
0
100% positive
m3u8_load_remote_hls_playlist_print_segments_duration.py
1import m3u8
2
3# Load a playlist from a remote URL
4playlist_url = 'https://nowtv-live.nowtv.com/001/001_hls_600.m3u8'
5m3u8_obj = m3u8.load(playlist_url)
6
7# Print all segment URIs
8print("Segments:")
9for segment in m3u8_obj.segments:
10    print(segment.uri)
11
12# Print the total duration of the playlist
13print(f"\nTotal Duration: {m3u8_obj.total_duration} seconds")
14
15# Check if the playlist is a variant playlist (Master Playlist)
16if m3u8_obj.is_variant:
17    print("\nVariant Streams:")
18    for playlist in m3u8_obj.playlists:
19        print(f"URL: {playlist.uri}, Bandwidth: {playlist.stream_info.bandwidth}")