m3u8

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2024 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MaxPlaylistNum 在内存中最多维护的 m3u8 列表个数
	// 超出则淘汰最久没有读取的一个
	MaxPlaylistNum = 10

	// PreChanSize 预处理通道大小, 塞满时从头部开始淘汰
	PreChanSize = 1000
)

Variables

View Source
var GetPlaylist func(alistPath, templateId string, proxy, main bool) (string, bool)

GetPlaylist 获取 m3u 播放列表, 返回 m3u 文本

View Source
var GetSubtitleLink func(alistPath, templateId, subName string) (string, bool)

GetSubtitleLink 获取字幕链接

View Source
var GetTsLink func(alistPath, templateId string, idx int) (string, bool)

GetTsLink 获取 m3u 播放列表中的某个 ts 链接

View Source
var ParentHeadComments = map[string]struct{}{
	"#EXTM3U": {}, "#EXT-X-VERSION": {}, "#EXT-X-MEDIA-SEQUENCE": {},
	"#EXT-X-TARGETDURATION": {}, "#EXT-X-MEDIA": {}, "#EXT-X-INDEPENDENT-SEGMENTS": {},
	"#EXT-X-STREAM-INF": {},
}

ParentHeadComments 记录文件头注释

View Source
var ParentTailComments = map[string]struct{}{
	"#EXT-X-ENDLIST": {},
}

ParentTailComments 记录文件尾注释

View Source
var ValidM3U8Contents = map[string]struct{}{
	"application/vnd.apple.mpegurl": {},
	"application/x-mpegurl":         {},
	"audio/x-mpegurl":               {},
	"application/octet-stream":      {},
}

响应头中,有效的 m3u8 Content-Type 属性

Functions

func ProxyPlaylist

func ProxyPlaylist(c *gin.Context)

ProxyPlaylist 代理 m3u8 转码地址

func ProxySubtitle

func ProxySubtitle(c *gin.Context)

ProxySubtitle 代理字幕请求

func ProxyTsLink(c *gin.Context)

ProxyTsLink 代理 ts 直链地址

func PushPlaylistAsync

func PushPlaylistAsync(info Info)

PushPlaylistAsync 将一个 alist 转码资源异步缓存到内存中

Types

type Info

type Info struct {
	AlistPath     string               // 资源在 alist 中的绝对路径
	TemplateId    string               // 转码资源模板 id
	Subtitles     []alist.SubtitleInfo // 字幕信息, 如果一个资源是含有字幕的, 会返回变体 m3u8
	RemoteBase    string               // 远程 m3u8 地址前缀
	HeadComments  []string             // 头注释信息
	TailComments  []string             // 尾注释信息
	RemoteTsInfos []*TsInfo            // 远程的 ts URL 列表, 用于重定向

	// LastRead 客户端最后读取的时间戳 (毫秒)
	//
	// 超过 30 分钟未读取, 程序停止更新;
	// 超过 12 小时未读取, m3u info 被移除
	LastRead int64

	// LastUpdate 程序最后的更新时间戳 (毫秒)
	//
	// 客户端来读取时, 如果 m3u info 已经超过 10 分钟没有更新了
	// 触发更新机制之后, 再返回最新的地址
	LastUpdate int64
}

Info 记录一个 m3u8 相关信息

func NewByContent

func NewByContent(baseUrl, content string) (*Info, error)

NewByContent 根据 m3u8 文本初始化一个 info 对象

如果文本中的 ts 地址是相对地址, 可通过 baseUrl 指定请求前缀

func NewByRemote

func NewByRemote(url string, header http.Header) (*Info, error)

NewByRemote 从一个远程的 m3u8 地址中初始化 info 对象

func (*Info) Content

func (i *Info) Content() string

Content 将 i 转换为 m3u8 文本

func (*Info) ContentFunc

func (i *Info) ContentFunc(tsMapper func(int, string) string) string

ContentFunc 将 i 对象转换成 m3u8 文本

tsMapper 函数可以将当前 info 中的 ts 地址映射为自定义地址 两个参数分别是 ts 的索引和地址值

func (i *Info) GetTsLink(idx int) (string, bool)

GetTsLink 获取 ts 流的直链地址

func (*Info) MasterFunc deprecated

func (i *Info) MasterFunc(cntMapper func() string) string

Deprecated: MasterFunc 获取变体 m3u8

当 info 包含有字幕时, 需要调用这个方法返回

func (*Info) ProxyContent

func (i *Info) ProxyContent(main bool) string

ProxyContent 将 i 转换为 m3u8 本地代理文本

func (*Info) UpdateContent

func (i *Info) UpdateContent() error

UpdateContent 从 alist 获取最新的 m3u8 并更新对象

通过 AlistPath 和 TemplateId 定位到唯一一个转码资源地址

type ProxyParams

type ProxyParams struct {
	AlistPath  string `form:"alist_path"`
	TemplateId string `form:"template_id"`
	Remote     string `form:"remote"`
	Type       string `form:"type"`
	ApiKey     string `form:"api_key"`
	IdxStr     string `form:"idx"`
}

ProxyParams 代理请求接收参数

type TsInfo

type TsInfo struct {
	Comments []string // 注释信息
	Url      string   // 远程流请求地址
}

TsInfo 记录一个 ts 相关信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL