Python's m3u8 module parses the ts fragment address according to the given url

Python's m3u8 module is a module for parsing m3u8 files, which can easily obtain various information in m3u8 files, such as playlists, media file addresses, etc. The m3u8 file is a text-based media playlist format, often used in streaming media transmission protocols (such as HLS).

The m3u8 module provides two methods, load() and loads(), to load m3u8 files, corresponding to loading m3u8 files from files and strings, respectively. After loading, various information in the m3u8 file can be obtained through the properties and methods of the m3u8 object. Commonly used properties and methods include:

m3u8_obj.playlists: Get the list of playlist objects.
m3u8_obj.segments: Get a list of media file segment objects.
m3u8_obj.is_variant: Determine whether it is a variant stream.
m3u8_obj.is_endlist: Determine whether it is the last media file.
m3u8_obj.target_duration: Get the target duration.
m3u8_obj.media_sequence: Get the sequence number of the media file.

import m3u8
from urllib.parse import urljoin

index_url = 'http://xx/index.m3u8'
# 使用m3u8.load()方法加载m3u8文件
playlist = m3u8.load(index_url)
# 获取m3u8文件中的所有ts地址
for uri in playlist.segments.uri:
    # ts分片绝对地址
    url = urljoin(index_url, uri)
    print(url)

Through the m3u8 module, various information in the m3u8 file can be easily parsed, and the automatic processing and playback of the m3u8 file can be realized.

Guess you like

Origin blog.csdn.net/lilongsy/article/details/131120269