-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_subtitle.go
172 lines (144 loc) · 3.99 KB
/
app_subtitle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package app
import (
"errors"
"fmt"
"time"
"github.com/apex/log"
"github.com/fatih/set"
"github.com/tympanix/supper/list"
"github.com/tympanix/supper/logutil"
"github.com/tympanix/supper/types"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
)
// DownloadSubtitles downloads subtitles for a whole list of mediafiles for every
// langauge in the language set
func (a *Application) DownloadSubtitles(media types.LocalMediaList, lang set.Interface) ([]types.LocalSubtitle, error) {
var result []types.LocalSubtitle
if media == nil {
return nil, errors.New("no media supplied for subtitles")
}
if lang == nil {
return nil, errors.New("no languages supplied for subtitles")
}
video := media.FilterVideo()
if video.Len() == 0 {
return nil, errors.New("no video media found in path")
}
video, err := video.FilterMissingSubs(lang)
if err != nil {
return nil, err
}
// Iterate all media files in the list
for i, item := range video.List() {
ctx := log.WithFields(log.Fields{
"media": item,
"item": fmt.Sprintf("%v/%v", i+1, video.Len()),
})
cursubs, err := item.ExistingSubtitles()
if err != nil {
return nil, err
}
missingLangs := set.Difference(lang, cursubs.LanguageSet())
if missingLangs.Size() == 0 {
continue
}
var subs = list.Subtitles()
if !a.Config().Dry() {
var search []types.OnlineSubtitle
search, err = a.SearchSubtitles(item)
if err != nil {
ctx.WithError(err).Error("Subtitle failed")
if a.Config().Strict() {
return nil, err
}
continue
}
subs, err = list.NewSubtitlesFromInterface(search)
if err != nil {
ctx.WithError(err).Fatal("Subtitle error")
}
}
subs = subs.HearingImpaired(a.Config().Impaired())
// Download subtitle for each language
for _, v := range missingLangs.List() {
l, ok := v.(language.Tag)
if !ok {
return nil, logutil.Errorf("unknown language %v", v)
}
ctx = ctx.WithField("lang", display.English.Languages().Name(l))
if a.Config().Delay() > 0 {
time.Sleep(a.Config().Delay())
}
langsubs := subs.FilterLanguage(l)
if langsubs.Len() == 0 && !a.Config().Dry() {
ctx.Warn("No subtitle available")
continue
}
if !a.Config().Dry() {
sub, err := a.downloadBestSubtitle(ctx, item, langsubs)
if err != nil {
if a.Config().Strict() {
return nil, err
}
continue
}
result = append(result, sub)
} else {
ctx.WithField("reason", "dry-run").Info("Skip download")
}
}
}
return result, nil
}
func (a *Application) downloadBestSubtitle(ctx log.Interface, m types.Video, l types.SubtitleList) (types.LocalSubtitle, error) {
rated := l.RateByMedia(m, a.Config().Evaluator())
if rated.Len() == 0 {
m := "no subtitles satisfied media"
ctx.Warn(m)
return nil, errors.New(m)
}
sub := rated.Best()
if sub.Score() < (float32(a.Config().Score()) / 100.0) {
m := fmt.Sprintf("Score too low %.0f%%", sub.Score()*100.0)
ctx.Warnf(m)
return nil, errors.New(m)
}
onl, ok := sub.Subtitle().(types.OnlineSubtitle)
if !ok {
ctx.Fatal("Subtitle could not be cast to online subtitle")
}
srt, err := onl.Download()
if err != nil {
ctx.WithError(err).Error("Could not download subtitle")
return nil, err
}
defer srt.Close()
saved, err := m.SaveSubtitle(srt, onl.Language())
if err != nil {
ctx.WithError(err).Error("Subtitle error")
return nil, err
}
var strscore string
if sub.Score() == 0.0 {
strscore = "N/A"
} else {
strscore = fmt.Sprintf("%.0f%%", sub.Score()*100.0)
}
ctx.WithField("score", strscore).Info("Subtitle downloaded")
if err := a.execPluginsOnSubtitle(ctx, saved); err != nil {
return nil, err
}
return saved, nil
}
func (a *Application) execPluginsOnSubtitle(ctx log.Interface, s types.LocalSubtitle) error {
for _, plugin := range a.Config().Plugins() {
err := plugin.Run(s)
if err != nil {
ctx.WithField("plugin", plugin.Name()).Error("Plugin failed")
return err
}
ctx.WithField("plugin", plugin.Name()).Info("Plugin finished")
}
return nil
}