Add media info filters

This commit is contained in:
Ben Adrian Sarmiento
2024-06-05 19:19:03 +02:00
parent fa26553933
commit c63ce8da0a
7 changed files with 265 additions and 58 deletions

View File

@@ -1,6 +1,10 @@
package config
import "os"
import (
"os"
"gopkg.in/vansante/go-ffprobe.v2"
)
type ConfigInterface interface {
GetConfig() ZurgConfig
@@ -16,7 +20,7 @@ type ConfigInterface interface {
GetPassword() string
GetProxy() string
GetDirectories() []string
MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64) bool
MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, mediaInfos []*ffprobe.ProbeData) bool
GetOnLibraryUpdate() string
GetNetworkBufferSize() int
EnableRetainFolderNameExtension() bool

View File

@@ -9,6 +9,7 @@ import (
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/utils"
"gopkg.in/vansante/go-ffprobe.v2"
"gopkg.in/yaml.v3"
)
@@ -96,7 +97,7 @@ func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
return result
}
func (z *ZurgConfigV1) MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64) bool {
func (z *ZurgConfigV1) MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, mediaInfos []*ffprobe.ProbeData) bool {
if directory == ALL_TORRENTS {
return true
}
@@ -104,14 +105,14 @@ func (z *ZurgConfigV1) MeetsConditions(directory, torrentName string, torrentSiz
return false
}
for _, filter := range z.Directories[directory].Filters {
if z.matchFilter(torrentName, torrentSize, torrentIDs, fileNames, fileSizes, filter) {
if z.matchFilter(torrentName, torrentSize, torrentIDs, fileNames, fileSizes, mediaInfos, filter) {
return true
}
}
return false
}
func (z *ZurgConfigV1) matchFilter(torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, filter *FilterConditionsV1) bool {
func (z *ZurgConfigV1) matchFilter(torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, mediaInfos []*ffprobe.ProbeData, filter *FilterConditionsV1) bool {
if filter.ID != "" {
for _, torrentID := range torrentIDs {
if torrentID == filter.ID {
@@ -160,7 +161,7 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentSize int64, torren
if len(filter.And) > 0 {
andResult := true
for _, andFilter := range filter.And {
andResult = andResult && z.matchFilter(torrentName, torrentSize, torrentIDs, fileNames, fileSizes, andFilter)
andResult = andResult && z.matchFilter(torrentName, torrentSize, torrentIDs, fileNames, fileSizes, mediaInfos, andFilter)
if !andResult {
return false
}
@@ -169,7 +170,7 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentSize int64, torren
}
if len(filter.Or) > 0 {
for _, orFilter := range filter.Or {
if z.matchFilter(torrentName, torrentSize, torrentIDs, fileNames, fileSizes, orFilter) {
if z.matchFilter(torrentName, torrentSize, torrentIDs, fileNames, fileSizes, mediaInfos, orFilter) {
return true
}
}
@@ -292,6 +293,195 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentSize int64, torren
}
}
}
// media info filters
if filter.MediaInfoResolution != "" {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "video" || stream.Level <= 0 {
continue
} else if (stream.Width >= 7680 || stream.Height >= 4320) && filter.MediaInfoResolution == "8k" {
return true
} else if (stream.Width >= 3840 || stream.Height >= 2160) && filter.MediaInfoResolution == "4k" {
return true
} else if (stream.Width >= 1920 || stream.Height >= 1080) && filter.MediaInfoResolution == "1080p" {
return true
} else if (stream.Width >= 1280 || stream.Height >= 720) && filter.MediaInfoResolution == "720p" {
return true
} else if (stream.Width >= 854 || stream.Height >= 480) && filter.MediaInfoResolution == "480p" {
return true
} else if (stream.Width >= 640 || stream.Height >= 360) && filter.MediaInfoResolution == "360p" {
return true
} else if (stream.Width >= 426 || stream.Height >= 240) && filter.MediaInfoResolution == "240p" {
return true
} else if (stream.Width >= 256 || stream.Height >= 144) && filter.MediaInfoResolution == "144p" {
return true
}
}
}
return false
}
if filter.MediaInfoBitRateGreaterThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
bitrate, err := strconv.ParseInt(mediaInfo.Format.BitRate, 10, 64)
if err != nil {
continue
}
if bitrate >= filter.MediaInfoBitRateGreaterThanOrEqual {
return true
}
}
return false
}
if filter.MediaInfoBitRateLessThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
bitrate, err := strconv.ParseInt(mediaInfo.Format.BitRate, 10, 64)
if err != nil {
continue
}
if bitrate <= filter.MediaInfoBitRateLessThanOrEqual {
return true
}
}
return false
}
if filter.MediaInfoVideoBitRateGreaterThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "video" || stream.Level <= 0 {
continue
}
bitrate, err := strconv.ParseInt(stream.BitRate, 10, 64)
if err != nil {
continue
}
if bitrate >= filter.MediaInfoVideoBitRateGreaterThanOrEqual {
return true
}
}
}
return false
}
if filter.MediaInfoVideoBitRateLessThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "video" || stream.Level <= 0 {
continue
}
bitrate, err := strconv.ParseInt(stream.BitRate, 10, 64)
if err != nil {
continue
}
if bitrate <= filter.MediaInfoVideoBitRateLessThanOrEqual {
return true
}
}
}
return false
}
if filter.MediaInfoAudioBitRateGreaterThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "audio" {
continue
}
bitrate, err := strconv.ParseInt(stream.BitRate, 10, 64)
if err != nil {
continue
}
if bitrate >= filter.MediaInfoAudioBitRateGreaterThanOrEqual {
return true
}
}
}
return false
}
if filter.MediaInfoAudioBitRateLessThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "audio" {
continue
}
bitrate, err := strconv.ParseInt(stream.BitRate, 10, 64)
if err != nil {
continue
}
if bitrate <= filter.MediaInfoAudioBitRateLessThanOrEqual {
return true
}
}
}
return false
}
if filter.MediaInfoDurationGreaterThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
if int64(mediaInfo.Format.DurationSeconds) >= filter.MediaInfoDurationGreaterThanOrEqual {
return true
}
}
return false
}
if filter.MediaInfoDurationLessThanOrEqual > 0 {
for _, mediaInfo := range mediaInfos {
if int64(mediaInfo.Format.DurationSeconds) <= filter.MediaInfoDurationLessThanOrEqual {
return true
}
}
return false
}
if filter.MediaInfoWithAudioLanguage != "" {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "audio" {
continue
}
if strings.EqualFold(stream.Tags.Language, filter.MediaInfoWithAudioLanguage) {
return true
}
}
}
return false
}
if filter.MediaInfoWithoutAudioLanguage != "" {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "audio" {
continue
}
if strings.EqualFold(stream.Tags.Language, filter.MediaInfoWithoutAudioLanguage) {
return false
}
}
}
return true
}
if filter.MediaInfoWithSubtitleLanguage != "" {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "subtitle" {
continue
}
if strings.EqualFold(stream.Tags.Language, filter.MediaInfoWithSubtitleLanguage) {
return true
}
}
}
return false
}
if filter.MediaInfoWithoutSubtitleLanguage != "" {
for _, mediaInfo := range mediaInfos {
for _, stream := range mediaInfo.Streams {
if stream.CodecType != "subtitle" {
continue
}
if strings.EqualFold(stream.Tags.Language, filter.MediaInfoWithoutSubtitleLanguage) {
return false
}
}
}
return true
}
return false
}

View File

@@ -42,4 +42,18 @@ type FilterConditionsV1 struct {
HasEpisodes bool `yaml:"has_episodes"`
IsMusic bool `yaml:"is_music"`
MediaInfoResolution string `yaml:"media_info_resolution"` // possible values: 8k 4k 1080p 720p 480p 360p 240p 144p
MediaInfoBitRateGreaterThanOrEqual int64 `yaml:"media_info_bit_rate_gte"` // bytes per second
MediaInfoBitRateLessThanOrEqual int64 `yaml:"media_info_bit_rate_lte"` // bytes per second
MediaInfoVideoBitRateGreaterThanOrEqual int64 `yaml:"media_info_video_bit_rate_gte"` // bytes per second
MediaInfoVideoBitRateLessThanOrEqual int64 `yaml:"media_info_video_bit_rate_lte"` // bytes per second
MediaInfoAudioBitRateGreaterThanOrEqual int64 `yaml:"media_info_audio_bit_rate_gte"` // bytes per second
MediaInfoAudioBitRateLessThanOrEqual int64 `yaml:"media_info_audio_bit_rate_lte"` // bytes per second
MediaInfoDurationGreaterThanOrEqual int64 `yaml:"media_info_duration_gte"` // seconds
MediaInfoDurationLessThanOrEqual int64 `yaml:"media_info_duration_lte"` // seconds
MediaInfoWithAudioLanguage string `yaml:"media_info_with_audio_language"` // 3 char language code
MediaInfoWithoutAudioLanguage string `yaml:"media_info_without_audio_language"` // 3 char language code
MediaInfoWithSubtitleLanguage string `yaml:"media_info_with_subtitle_language"` // 3 char language code
MediaInfoWithoutSubtitleLanguage string `yaml:"media_info_without_subtitle_language"` // 3 char language code
}