commit 716419e5b62a9159acefcb1b070849a3e782643b
parent 62ddb61a4a0492147ee0e7c257f3be83e904b185
Author: Oliver Lowe <o@olowe.co>
Date: Wed, 26 Jun 2024 09:42:55 +1000
m3u8: more real-world decode example
This is a clearer example of the kind of thing you can do by decoding
a playlist instead of just printing a struct.
Diffstat:
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/m3u8/parse_test.go b/m3u8/parse_test.go
@@ -37,22 +37,26 @@ func ExampleEncode() {
}
func ExampleDecode() {
- m3u8Data := `#EXTM3U
+ s := `
+#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=1280000,RESOLUTION=640x360
url_0/low.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2560000,RESOLUTION=1280x720
url_0/mid.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=7680000,RESOLUTION=1920x1080
url_0/high.m3u8`
- rd := strings.NewReader(m3u8Data)
- p, _ := Decode(rd)
- fmt.Printf("%+v", p)
-
- // Output: &{Version:0 Segments:[] IndependentSegments:false Start:<nil> TargetDuration:0s Sequence:0 DiscontinuitySequence:0 End:false Type:invalid IFramesOnly:false Media:[] Variants:[#EXT-X-STREAM-INF:BANDWIDTH=1280000,RESOLUTION=640x360
- // url_0/low.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=2560000,RESOLUTION=1280x720
- // url_0/mid.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=7680000,RESOLUTION=1920x1080
- // url_0/high.m3u8] SessionData:[] SessionKey:<nil>}
+ p, err := Decode(strings.NewReader(s))
+ if err != nil {
+ // handle error
+ }
+ for _, v := range p.Variants {
+ fmt.Printf("%s %dp@%dkbps\n", v.URI, v.Resolution[1], v.Bandwidth/1e3)
+ }
+ // Output:
+ // url_0/low.m3u8 360p@1280kbps
+ // url_0/mid.m3u8 720p@2560kbps
+ // url_0/high.m3u8 1080p@7680kbps
}
func TestDecode(t *testing.T) {