media: fixes default format filtering
This commit is contained in:
parent
ebba6a8835
commit
8d57b97b0f
2 changed files with 27 additions and 15 deletions
|
@ -68,25 +68,33 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) {
|
|||
media.NSFW = true // always nsfw
|
||||
|
||||
if gif.Urls.Sd != "" {
|
||||
media.AddFormat(&models.MediaFormat{
|
||||
format := &models.MediaFormat{
|
||||
FormatID: "sd",
|
||||
Type: enums.MediaTypeVideo,
|
||||
URL: []string{gif.Urls.Sd},
|
||||
VideoCodec: enums.MediaCodecAVC,
|
||||
Width: int64(gif.Width / 2),
|
||||
Height: int64(gif.Height / 2),
|
||||
})
|
||||
}
|
||||
if gif.HasAudio {
|
||||
format.AudioCodec = enums.MediaCodecAAC
|
||||
}
|
||||
media.AddFormat(format)
|
||||
}
|
||||
|
||||
if gif.Urls.Hd != "" {
|
||||
media.AddFormat(&models.MediaFormat{
|
||||
format := &models.MediaFormat{
|
||||
FormatID: "hd",
|
||||
Type: enums.MediaTypeVideo,
|
||||
URL: []string{gif.Urls.Hd},
|
||||
VideoCodec: enums.MediaCodecAVC,
|
||||
Width: int64(gif.Width),
|
||||
Height: int64(gif.Height),
|
||||
})
|
||||
}
|
||||
if gif.HasAudio {
|
||||
format.AudioCodec = enums.MediaCodecAAC
|
||||
}
|
||||
media.AddFormat(format)
|
||||
}
|
||||
|
||||
if gif.Urls.Poster != "" {
|
||||
|
@ -101,12 +109,6 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if gif.HasAudio {
|
||||
for _, format := range media.Formats {
|
||||
format.AudioCodec = enums.MediaCodecAAC
|
||||
}
|
||||
}
|
||||
|
||||
if len(media.Formats) > 0 {
|
||||
mediaList = append(mediaList, media)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -106,12 +107,21 @@ func (media *Media) GetDefaultVideoFormat() *MediaFormat {
|
|||
if len(filtered) == 0 {
|
||||
return nil
|
||||
}
|
||||
bestFormat := filtered[0]
|
||||
for _, format := range filtered {
|
||||
if format.Bitrate > bestFormat.Bitrate {
|
||||
bestFormat = format
|
||||
slices.SortFunc(filtered, func(a, b *MediaFormat) int {
|
||||
if a.Bitrate != b.Bitrate {
|
||||
if a.Bitrate > b.Bitrate {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
}
|
||||
if a.Height > b.Height {
|
||||
return -1
|
||||
} else if a.Height < b.Height {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
bestFormat := filtered[0]
|
||||
bestFormat.IsDefault = true
|
||||
return bestFormat
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue