Skip to content

Commit

Permalink
Merge pull request #119 from sunshineplan/fix-parse-key
Browse files Browse the repository at this point in the history
Fix parse key
  • Loading branch information
sunshineplan authored Jun 2, 2024
2 parents 22e6b59 + 7e29dcd commit 5af6f4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.x'
go-version: stable

- name: Test Code
run: |
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.x'
go-version: stable

- name: Build
run: |
go build -ldflags "-s -w" -o hlsdl ./cmd
GOOS=windows go build -ldflags "-s -w" -o hlsdl.exe ./cmd
go build -ldflags "-s -w" -o hlsdl-linux-amd64 ./cmd
GOARCH=arm64 GOOS=darwin go build -ldflags "-s -w" -o hlsdl-darwin-arm64 ./cmd
GOOS=windows go build -ldflags "-s -w" -o hlsdl-windows-amd64.exe ./cmd
echo "date=$(TZ=PRC date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
- name: Upload Release Asset
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.x'
go-version: stable

- name: Test Code
run: |
Expand Down
49 changes: 25 additions & 24 deletions m3u8.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net/url"
"os"
"slices"
"sort"
"time"

Expand All @@ -19,46 +20,46 @@ var c = cache.New(false)
func parse(url *url.URL, playlist m3u8.Playlist) (*url.URL, *m3u8.MediaPlaylist, error) {
switch playlist := playlist.(type) {
case *m3u8.MediaPlaylist:
var segments []*m3u8.MediaSegment
for _, s := range playlist.Segments {
if s == nil {
if playlist.Key != nil && playlist.Key.URI != "" {
if u, err := url.Parse(playlist.Key.URI); err != nil {
return nil, nil, err
} else {
playlist.Key.URI = u.String()
}
}
playlist.Segments = slices.DeleteFunc(playlist.Segments, func(i *m3u8.MediaSegment) bool { return i == nil })
for _, i := range playlist.Segments {
if i == nil {
continue
}

if s.Key != nil && s.Key.URI != "" {
u, err := url.Parse(s.Key.URI)
if err != nil {
if i.Key != nil && i.Key.URI != "" {
if u, err := url.Parse(i.Key.URI); err != nil {
return nil, nil, err
} else {
i.Key.URI = u.String()
}
s.Key.URI = u.String()
}

if s.Discontinuity {
playlist.Key = s.Key
if i.Discontinuity {
playlist.Key = i.Key
} else {
if s.Key == nil && playlist.Key != nil {
s.Key = playlist.Key
if i.Key == nil && playlist.Key != nil {
i.Key = playlist.Key
}
}

u, err := url.Parse(s.URI)
if err != nil {
if u, err := url.Parse(i.URI); err != nil {
return nil, nil, err
} else {
i.URI = u.String()
}
s.URI = u.String()

segments = append(segments, s)
}
playlist.Segments = segments

return url, playlist, nil
case *m3u8.MasterPlaylist:
for _, i := range playlist.Variants {
u, err := url.Parse(i.URI)
if err != nil {
if u, err := url.Parse(i.URI); err != nil {
continue
} else {
i.URI = u.String()
}
i.URI = u.String()
}
sort.SliceStable(playlist.Variants, func(i, j int) bool {
return playlist.Variants[i].Bandwidth > playlist.Variants[j].Bandwidth
Expand Down

0 comments on commit 5af6f4a

Please sign in to comment.