231 lines
6.7 KiB
Go
231 lines
6.7 KiB
Go
package media
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/grafov/m3u8"
|
|
"log"
|
|
"m3u8-downloader/pkg/constants"
|
|
"m3u8-downloader/pkg/health"
|
|
"m3u8-downloader/pkg/httpClient"
|
|
"m3u8-downloader/pkg/logging"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type StreamVariant struct {
|
|
URL string
|
|
Bandwidth uint32
|
|
BaseURL *url.URL
|
|
ID int
|
|
Resolution string
|
|
OutputDir string
|
|
}
|
|
|
|
// ResolutionHeight converts a label produced by ExtractResolution back into
|
|
// the pixel height it stands for, so variants can be ranked by quality.
|
|
//
|
|
// The height is parsed rather than looked up in a table: labels come from the
|
|
// playlist's own RESOLUTION attribute, so a stream can legitimately offer
|
|
// 1440p or 2160p that no fixed table would anticipate. Anything not in
|
|
// <height>p form — "unknown", from a playlist with no variants — returns 0 and
|
|
// therefore ranks below every recognised resolution.
|
|
//
|
|
// Comparing the labels as strings instead is a trap worth naming: "720p" sorts
|
|
// after "1080p", so lexicographic comparison silently prefers lower quality.
|
|
func ResolutionHeight(resolution string) int {
|
|
trimmed := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(resolution)), "p")
|
|
|
|
height, err := strconv.Atoi(trimmed)
|
|
if err != nil || height < 0 {
|
|
return 0
|
|
}
|
|
return height
|
|
}
|
|
|
|
// ExtractResolution maps a variant to the label used for its output directory,
|
|
// falling back to a bandwidth heuristic when the playlist omits RESOLUTION.
|
|
func ExtractResolution(variant *m3u8.Variant) string {
|
|
if variant.Resolution != "" {
|
|
parts := strings.Split(variant.Resolution, "x")
|
|
if len(parts) == 2 {
|
|
return parts[1] + "p"
|
|
}
|
|
}
|
|
switch {
|
|
case variant.Bandwidth >= 5000000:
|
|
return "1080p"
|
|
case variant.Bandwidth >= 3000000:
|
|
return "720p"
|
|
case variant.Bandwidth >= 1500000:
|
|
return "480p"
|
|
case variant.Bandwidth >= 800000:
|
|
return "360p"
|
|
default:
|
|
return "240p"
|
|
}
|
|
}
|
|
|
|
// GetAllVariants fetches the master playlist and returns one entry per stream
|
|
// variant. Segments are recorded into the manifest by VariantDownloader, which
|
|
// receives it directly, so no writer is threaded through here.
|
|
func GetAllVariants(masterURL string, outputDir string) ([]*StreamVariant, error) {
|
|
client := &http.Client{}
|
|
req, _ := http.NewRequest("GET", masterURL, nil)
|
|
req.Header.Set("User-Agent", constants.HTTPUserAgent)
|
|
req.Header.Set("Referer", constants.REFERRER)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
logging.MustGetFactory().Download.Error("failed to fetch master playlist", logging.KeyError, err.Error())
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
playlist, listType, err := m3u8.DecodeFrom(resp.Body, true)
|
|
if err != nil {
|
|
logging.MustGetFactory().Download.Error("failed to decode master playlist", logging.KeyError, err.Error())
|
|
return nil, err
|
|
}
|
|
|
|
base, _ := url.Parse(masterURL)
|
|
|
|
if listType == m3u8.MEDIA {
|
|
return []*StreamVariant{{
|
|
URL: masterURL,
|
|
Bandwidth: 0,
|
|
BaseURL: base,
|
|
ID: 0,
|
|
Resolution: "unknown",
|
|
OutputDir: path.Join(outputDir, "unknown"),
|
|
}}, nil
|
|
}
|
|
|
|
master := playlist.(*m3u8.MasterPlaylist)
|
|
if len(master.Variants) == 0 {
|
|
return nil, fmt.Errorf("no variants found in master playlist")
|
|
}
|
|
|
|
variants := make([]*StreamVariant, 0, len(master.Variants))
|
|
for i, v := range master.Variants {
|
|
vURL, _ := url.Parse(v.URI)
|
|
fullURL := base.ResolveReference(vURL).String()
|
|
resolution := ExtractResolution(v)
|
|
outputDir := path.Join(outputDir, resolution)
|
|
variants = append(variants, &StreamVariant{
|
|
URL: fullURL,
|
|
Bandwidth: v.Bandwidth,
|
|
BaseURL: base.ResolveReference(vURL),
|
|
ID: i,
|
|
Resolution: resolution,
|
|
OutputDir: outputDir,
|
|
})
|
|
}
|
|
logging.MustGetFactory().Download.Info("discovered variants", "count", len(variants))
|
|
return variants, nil
|
|
}
|
|
|
|
func VariantDownloader(ctx context.Context, variant *StreamVariant, sem chan struct{}, manifest *ManifestWriter) {
|
|
log.Printf("Starting %s variant downloader (bandwidth: %d)", variant.Resolution, variant.Bandwidth)
|
|
dlLog := logging.MustGetFactory().Download.With(logging.KeyResolution, variant.Resolution)
|
|
registry := health.MustGetRegistry()
|
|
dlLog.Info("variant downloader started", logging.KeyBandwidth, variant.Bandwidth)
|
|
registry.RecordVariantStarted(variant.Resolution, variant.Bandwidth)
|
|
|
|
ticker := time.NewTicker(constants.RefreshDelay)
|
|
defer ticker.Stop()
|
|
client := &http.Client{}
|
|
seen := make(map[string]bool)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
registry.RecordVariantStopped(variant.Resolution)
|
|
return
|
|
default:
|
|
}
|
|
|
|
var seq uint64
|
|
playlist, err := LoadMediaPlaylist(variant.URL)
|
|
if err != nil {
|
|
log.Printf("%s: Error loading playlist playlist: %v", variant.Resolution, err)
|
|
dlLog.Warn("failed to load media playlist", logging.KeyError, err.Error())
|
|
registry.RecordPlaylistError(variant.Resolution, err)
|
|
goto waitTick
|
|
}
|
|
seq = playlist.SeqNo
|
|
|
|
for _, seg := range playlist.Segments {
|
|
if seg == nil {
|
|
continue
|
|
}
|
|
job := SegmentJob{
|
|
URI: seg.URI,
|
|
Seq: seq,
|
|
VariantID: variant.ID,
|
|
Variant: variant,
|
|
}
|
|
segmentKey := job.Key()
|
|
if seen[segmentKey] {
|
|
seq++
|
|
continue
|
|
}
|
|
seen[segmentKey] = true
|
|
|
|
sem <- struct{}{} // Acquire
|
|
go func(j SegmentJob) {
|
|
defer func() { <-sem }() // Release
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
err := DownloadSegment(ctx, client, j.AbsoluteURL(), j.Variant.OutputDir)
|
|
name := strings.TrimSuffix(path.Base(j.Key()), path.Ext(path.Base(j.Key())))
|
|
|
|
if err == nil {
|
|
log.Printf("✓ %s downloaded segment %s", j.Variant.Resolution, name)
|
|
dlLog.Debug("segment downloaded", logging.KeySegment, name)
|
|
registry.RecordSegmentDownloaded(variant.Resolution)
|
|
if manifest != nil {
|
|
manifest.AddOrUpdateSegment(strconv.FormatUint(j.Seq, 10), variant.Resolution)
|
|
}
|
|
return
|
|
}
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
// Suppress log: shutdown in progress
|
|
return
|
|
}
|
|
|
|
if httpClient.IsHTTPStatus(err, 403) {
|
|
log.Printf("✗ %s failed to download segment %s (403)", j.Variant.Resolution, name)
|
|
dlLog.Warn("segment download failed (403)", logging.KeySegment, name)
|
|
} else {
|
|
log.Printf("✗ %s failed to download segment %s: %v", j.Variant.Resolution, name, err)
|
|
dlLog.Error("segment download failed", logging.KeySegment, name, logging.KeyError, err.Error())
|
|
}
|
|
registry.RecordSegmentFailed(variant.Resolution, err)
|
|
}(job)
|
|
seq++
|
|
}
|
|
|
|
if playlist.Closed {
|
|
log.Printf("%s: Playlist closed (#EXT-X-ENDLIST)", variant.Resolution)
|
|
dlLog.Info("playlist closed")
|
|
registry.RecordVariantStopped(variant.Resolution)
|
|
return
|
|
}
|
|
|
|
waitTick:
|
|
select {
|
|
case <-ctx.Done():
|
|
registry.RecordVariantStopped(variant.Resolution)
|
|
return
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|