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

@@ -14,6 +14,7 @@ import (
"github.com/debridmediamanager/zurg/internal/rar"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/utils"
mapset "github.com/deckarep/golang-set/v2"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/panjf2000/ants/v2"
@@ -107,10 +108,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, r
if torrent != nil {
accessKey := t.GetKey(torrent)
allTorrents.Set(accessKey, torrent)
t.assignDirectory(torrent, func(directory string) {
listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, torrent)
})
t.assignDirectory(torrent, false)
}
return false
})
@@ -230,13 +228,16 @@ func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) {
}
func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) {
changesApplied := false
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.MediaInfo != nil || file.State.Is("broken_file") {
isPlayable := utils.IsPlayable(file.Path) || t.IsPlayable(file.Path)
if file.MediaInfo != nil || file.State.Is("broken_file") || !isPlayable {
return
}
unrestrict := t.UnrestrictFileUntilOk(file, false)
unrestrict := t.UnrestrictFileUntilOk(file, true)
if unrestrict == nil {
file.State.Event(context.Background(), "break_file")
changesApplied = true
return
}
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
@@ -244,11 +245,15 @@ func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) {
data, err := ffprobe.ProbeURL(ctx, unrestrict.Download)
if err != nil {
t.log.Warnf("Cannot probe file %s: %v", file.Path, err)
file.State.Event(context.Background(), "break_file")
return
}
file.MediaInfo = data
changesApplied = true
})
if changesApplied {
t.assignDirectory(torrent, true)
t.writeTorrentToFile(torrent)
}
}
func (t *TorrentManager) readTorrentFromFile(filePath string) *Torrent {
@@ -436,7 +441,6 @@ func (t *TorrentManager) analyzeAllTorrents() {
idx := 0
allTorrents.IterCb(func(_ string, torrent *Torrent) {
t.applyMediaInfoDetails(torrent)
t.writeTorrentToFile(torrent)
idx++
t.log.Debugf("Applied media info details to torrent %s (%d/%d)", t.GetKey(torrent), idx, totalCount)
})