-
Notifications
You must be signed in to change notification settings - Fork 0
/
subkers.go
129 lines (103 loc) · 2.94 KB
/
subkers.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
package subkers
import (
"errors"
"fmt"
"io"
"strings"
"github.com/asticode/go-astisub"
)
// SubtitleType needed for ProcessSpecific
type SubtitleType int
const (
SRT SubtitleType = iota
SSA
ASS
STL
TTML
WebVVT
)
var subTypes = []string{
"srt",
"ssa",
"ass",
"stl",
"ttml",
"vtt",
}
// SubtitlesType returns SubtitleType from string
func SubtitlesType(ext string) (SubtitleType, error) {
for i := range subTypes {
if subTypes[i] == ext {
return SubtitleType(i), nil
}
}
return SubtitleType(0), errors.New(fmt.Sprint("Can't recognize extension, got", ext, "\nneed", subTypes))
}
// Process subtitles file to .csv format (markers) for Adobe Audition
func Process(filename string) ([]Marker, error) {
subtitles, err := astisub.OpenFile(filename)
if err != nil {
return nil, errors.New(fmt.Sprint("Can't read:", err))
}
markers, err := markers(subtitles)
if err != nil {
return nil, errors.New(fmt.Sprint("Can't convert to markers:", err))
}
decodedMarkers, err := decode(markers)
if err != nil {
return nil, errors.New(fmt.Sprint("Can't translate to UTF-8:", err))
}
return decodedMarkers, nil
}
// ProcessSpecific process specific extension subtitles to .csv format (markers) for Adobe Audition
func ProcessSpecific(subtitlesType SubtitleType, reader io.Reader) ([]Marker, error) {
var subtitles *astisub.Subtitles
var err error
switch subtitlesType {
case SRT:
subtitles, err = astisub.ReadFromSRT(reader)
case SSA, ASS:
subtitles, err = astisub.ReadFromSSA(reader)
case STL:
subtitles, err = astisub.ReadFromSTL(reader)
case TTML:
subtitles, err = astisub.ReadFromTTML(reader)
case WebVVT:
subtitles, err = astisub.ReadFromWebVTT(reader)
default:
return nil, errors.New(fmt.Sprint("Wrong file extension, types that can be processed:\n", subTypes))
}
if err != nil {
return nil, errors.New(fmt.Sprint("Can't read:", err))
}
markers, err := markers(subtitles)
if err != nil {
return nil, errors.New(fmt.Sprint("Can't convert to markers:", err))
}
decodedMarkers, err := decode(markers)
if err != nil {
return nil, errors.New(fmt.Sprint("Can't translate to UTF-8:", err))
}
return decodedMarkers, nil
}
// WriteAll markers to writer
func WriteAll(markers []Marker, writer io.Writer) error {
// Required line at the beginning of the file
if _, err := writer.Write([]byte("Name\tStart\tDuration\tTime Format\tType\tDescription\n")); err != nil {
return errors.New(fmt.Sprint("Can't write:", err))
}
// Writing markers
for _, val := range markers {
if strings.ReplaceAll(strings.Join(val.Lines, " "), "\\N", "") == "" {
continue
}
// Marker line .. marker line is a cue in the Adobe Audition
line := strings.ReplaceAll(strings.Join(val.Lines, " "), "\\N", "") + "\t" +
timeToString(val.StartAt) + "\t" +
timeToString(val.Duration) + "\tdecimal\tCue\t\n"
if _, err := writer.Write([]byte(line)); err != nil {
return errors.New(fmt.Sprint("Can't write:", err))
}
}
return nil
}