hls.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package hls
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. const (
  7. HLS_KEY_METHOD_AES_128 = "AES-128"
  8. )
  9. // https://datatracker.ietf.org/doc/draft-pantos-http-live-streaming/
  10. // 以”#EXT“开头的表示一个”tag“,否则表示注释,直接忽略
  11. type Playlist struct {
  12. io.Writer
  13. ExtM3U string // indicates that the file is an Extended M3U [M3U] Playlist file. (4.3.3.1) -- 每个M3U文件第一行必须是这个tag.
  14. Version int // indicates the compatibility version of the Playlist file. (4.3.1.2) -- 协议版本号.
  15. Sequence int // indicates the Media Sequence Number of the first Media Segment that appears in a Playlist file. (4.3.3.2) -- 第一个媒体段的序列号.
  16. Targetduration int // specifies the maximum Media Segment duration. (4.3.3.1) -- 每个视频分段最大的时长(单位秒).
  17. PlaylistType int // rovides mutability information about the Media Playlist file. (4.3.3.5) -- 提供关于PlayList的可变性的信息.
  18. Discontinuity int // indicates a discontinuity between theMedia Segment that follows it and the one that preceded it. (4.3.2.3) -- 该标签后边的媒体文件和之前的媒体文件之间的编码不连贯(即发生改变)(场景用于插播广告等等).
  19. Key PlaylistKey // specifies how to decrypt them. (4.3.2.4) -- 解密媒体文件的必要信息(表示怎么对media segments进行解码).
  20. EndList string // indicates that no more Media Segments will be added to the Media Playlist file. (4.3.3.4) -- 标示没有更多媒体文件将会加入到播放列表中,它可能会出现在播放列表文件的任何地方,但是不能出现两次或以上.
  21. Inf PlaylistInf // specifies the duration of a Media Segment. (4.3.2.1) -- 指定每个媒体段(ts)的持续时间.
  22. tsCount int
  23. }
  24. // Discontinuity :
  25. // file format
  26. // number, type and identifiers of tracks
  27. // timestamp sequence
  28. // encoding parameters
  29. // encoding sequence
  30. type PlaylistKey struct {
  31. Method string // specifies the encryption method. (4.3.2.4)
  32. Uri string // key url. (4.3.2.4)
  33. IV string // key iv. (4.3.2.4)
  34. }
  35. type PlaylistInf struct {
  36. Duration float64
  37. Title string
  38. FilePath string
  39. }
  40. func (pl *Playlist) Init() (err error) {
  41. // ss := fmt.Sprintf("#EXTM3U\n"+
  42. // "#EXT-X-VERSION:%d\n"+
  43. // "#EXT-X-MEDIA-SEQUENCE:%d\n"+
  44. // "#EXT-X-TARGETDURATION:%d\n"+
  45. // "#EXT-X-PLAYLIST-TYPE:%d\n"+
  46. // "#EXT-X-DISCONTINUITY:%d\n"+
  47. // "#EXT-X-KEY:METHOD=%s,URI=%s,IV=%s\n"+
  48. // "#EXT-X-ENDLIST", hls.Version, hls.Sequence, hls.Targetduration, hls.PlaylistType, hls.Discontinuity, hls.Key.Method, hls.Key.Uri, hls.Key.IV)
  49. _, err = fmt.Fprintf(pl, "#EXTM3U\n"+
  50. "#EXT-X-VERSION:%d\n"+
  51. "#EXT-X-MEDIA-SEQUENCE:%d\n"+
  52. "#EXT-X-TARGETDURATION:%d\n", pl.Version, pl.Sequence, pl.Targetduration)
  53. pl.Sequence++
  54. return
  55. }
  56. func (pl *Playlist) WriteInf(inf PlaylistInf) (err error) {
  57. _, err = fmt.Fprintf(pl, "#EXTINF:%.3f,\n"+
  58. "%s\n", inf.Duration, inf.Title)
  59. pl.tsCount++
  60. return
  61. }