-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswf2ass.go
282 lines (232 loc) · 6.65 KB
/
swf2ass.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"git.gammaspectra.live/WeebDataHoarder/swf-go"
swftag "git.gammaspectra.live/WeebDataHoarder/swf-go/tag"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/ass"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/ass/processing"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/settings"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/math"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/shapes"
"io"
math2 "math"
"os"
"path"
)
type KnownSignatures map[string]KnownSignature
type KnownSignature struct {
Name string `json:"name"`
Description string `json:"description"`
Remove []RemovalEntry `json:"remove"`
}
func (s KnownSignature) Filter(object *types.RenderedObject) bool {
for i := range s.Remove {
if s.Remove[i].Equals(object) {
return true
}
}
return false
}
type RemovalEntry struct {
ObjectId *uint16 `json:"objectId"`
ObjectIdComment *uint16 `json:"_objectId"`
Depth types.Depth `json:"depth"`
}
func (e RemovalEntry) Equals(object *types.RenderedObject) bool {
return (e.ObjectId == nil || *e.ObjectId == object.ObjectId) && (len(e.Depth) == 0 || (len(object.Depth) >= len(e.Depth)) && object.Depth[:len(e.Depth)].Equals(e.Depth))
}
func main() {
inputFile := flag.String("input", "", "Input SWF")
outputFile := flag.String("output", "", "Output ASS")
outputAudio := flag.String("audio", "", "Output Audio")
removalSignatures := flag.String("signatures", "signatures.json", "JSON file containing parameters for signature removal")
fromFrame := flag.Int64("from", 0, "Frame to start at")
toFrame := flag.Int64("to", math2.MaxInt64, "Frame to end at")
flag.Parse()
file, err := os.Open(*inputFile)
if err != nil {
panic(err)
}
defer file.Close()
swfReader, err := swf.NewReader(file)
if err != nil {
panic(err)
}
var knownSignatures KnownSignatures
removalSignaturesData, err := os.ReadFile(*removalSignatures)
if err == nil {
_ = json.Unmarshal(removalSignaturesData, &knownSignatures)
}
var tags []swftag.Tag
for {
readTag, err := swfReader.Tag()
if err != nil {
if errors.Is(err, swftag.ErrUnknownTag) {
continue
}
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if readTag == nil {
//not decoded
continue
}
tags = append(tags, readTag)
if readTag.Code() == swftag.RecordEnd {
break
}
switch t := readTag.(type) {
default:
_ = t
}
}
var frameOffset int64
processor := types.NewSWFProcessor(tags, shapes.RectangleFromSWF(swfReader.Header().FrameSize), swfReader.Header().FrameRate.Float64(), int64(swfReader.Header().FrameCount), swfReader.Header().Version)
assRenderer := ass.NewRenderer(path.Base(*inputFile), processor.FrameRate, processor.ViewPort)
var ks KnownSignature
_, err = file.Seek(0, io.SeekStart)
if err == nil {
hasher := sha256.New()
_, err = io.Copy(hasher, file)
if err == nil {
ks = knownSignatures[hex.EncodeToString(hasher.Sum(nil))]
}
}
if ks.Name == "" || ks.Description == "" {
for _, s := range knownSignatures {
if s.Name == path.Base(*inputFile) {
ks = s
break
}
}
}
temporaryOutput, err := os.Create(*outputFile + ".tmp")
if err != nil {
panic(err)
}
defer temporaryOutput.Close()
outputLines := func(lines ...string) {
for _, line := range lines {
_, err = temporaryOutput.Write([]byte(line))
if err != nil {
panic(err)
}
_, err = temporaryOutput.Write([]byte("\n"))
if err != nil {
panic(err)
}
}
}
var lastFrame *types.FrameInformation
for {
frame := processor.NextFrameOutput()
if frame == nil {
break
}
lastFrame = frame
//Force playback
if !processor.Playing {
processor.Playing = true
}
if !processor.Playing || processor.Loops > 0 {
break
}
//TODO: handle multiple sounds
if processor.Audio != nil && frameOffset == 0 {
if processor.Audio.Start == nil {
fmt.Printf("Skipped frame %d: audio not started\n", frame.FrameNumber)
continue
}
frameOffset = *processor.Audio.Start
fmt.Printf("Set start frame offset to %d\n", frameOffset)
} else if processor.Audio == nil {
//TODO: make this an option
fmt.Printf("Skipped frame %d: no audio\n", frame.FrameNumber)
continue
}
frame.FrameOffset = frameOffset
rendered := frame.Frame.Render(0, nil, types.None[math.ColorTransform](), types.None[math.MatrixTransform]())
if frame.GetFrameNumber() == 0 {
for _, object := range rendered {
fmt.Printf("frame 0: object %d depth: %s\n", object.ObjectId, object.Depth.String())
}
}
filteredRendered := make(types.RenderedFrame, 0, len(rendered))
var drawCalls, drawItems, filteredObjects, clipCalls, clipItems int
for _, object := range rendered {
if ks.Filter(object) {
filteredObjects++
continue
}
if object.Clip != nil {
clipCalls++
clipItems += len(object.Clip.GetShape())
}
for _, p := range object.DrawPathList {
drawCalls++
drawItems += len(p.Shape)
}
filteredRendered = append(filteredRendered, object)
}
fmt.Printf("=== frame %d/%d ~ %d : Depth count: %d :: Object count: %d :: Paths: %d draw calls, %d items :: Filtered: %d :: Clips %d draw calls, %d items\n",
frame.GetFrameNumber(),
processor.ExpectedFrameCount,
frameOffset,
len(frame.Frame.DepthMap),
len(filteredRendered),
drawCalls,
drawItems,
filteredObjects,
clipCalls,
clipItems,
)
if *fromFrame > 0 {
if frame.GetFrameNumber() < *fromFrame {
continue
} /*else {
for _, object := range rendered {
var count int
for i, command := range object.DrawPathList {
for j, record := range command.Shape.Edges {
}
}
}
}*/
}
outputLines(assRenderer.RenderFrame(*frame, filteredRendered, settings.GlobalSettings.KeyFrameInterval, settings.GlobalSettings.FlushInterval, settings.GlobalSettings.FlushCountLimit)...)
if *toFrame != math2.MaxInt64 && frame.GetFrameNumber() >= *toFrame {
break
}
}
if lastFrame == nil {
panic("no frames generated")
}
outputLines(assRenderer.Flush(*lastFrame)...)
if *outputAudio != "" && processor.Audio != nil && processor.Audio.Format == swftag.SoundFormatMP3 {
_ = os.WriteFile(*outputAudio, processor.Audio.Data, 0664)
}
stats := assRenderer.AggregateStatistics()
stats.SortBySize()
for _, s := range stats.Strings() {
print(s + "\n")
}
output, err := os.Create(*outputFile)
if err != nil {
panic(err)
}
defer output.Close()
err = processing.PostProcess(temporaryOutput, output)
if err != nil {
panic(err)
}
temporaryOutput.Close()
os.Remove(*outputFile + ".tmp")
}