forked from crunchy-labs/crunchy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
235 lines (210 loc) · 6.45 KB
/
format.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
package crunchyroll
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"fmt"
"github.com/grafov/m3u8"
"io/ioutil"
"math"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
const (
EPISODE FormatType = "episodes"
MOVIE = "movies"
)
type FormatType string
type Format struct {
crunchy *Crunchyroll
ID string
// FormatType represents if the format parent is an episode or a movie
FormatType FormatType
Video *m3u8.Variant
AudioLocale LOCALE
Hardsub LOCALE
Subtitles []*Subtitle
}
// Download calls DownloadGoroutines with 4 goroutines.
// See DownloadGoroutines for more details
//
// Deprecated: Use DownloadGoroutines instead
func (f *Format) Download(output *os.File, onSegmentDownload func(segment *m3u8.MediaSegment, current, total int, file *os.File, err error) error) error {
return f.DownloadGoroutines(output, 4, func(segment *m3u8.MediaSegment, current, total int, file *os.File) error {
return onSegmentDownload(segment, current, total, file, nil)
})
}
// DownloadGoroutines downloads the format to the given output file (as .ts file).
// See Format.DownloadSegments for more information
func (f *Format) DownloadGoroutines(output *os.File, goroutines int, onSegmentDownload func(segment *m3u8.MediaSegment, current, total int, file *os.File) error) error {
downloadDir, err := os.MkdirTemp("", "crunchy_")
if err != nil {
return err
}
defer os.RemoveAll(downloadDir)
if err := f.DownloadSegments(downloadDir, goroutines, onSegmentDownload); err != nil {
return err
}
return f.mergeSegments(downloadDir, output)
}
// DownloadSegments downloads every mpeg transport stream segment to a given directory (more information below).
// After every segment download onSegmentDownload will be called with:
// the downloaded segment, the current position, the total size of segments to download, the file where the segment content was written to an error (if occurred).
// The filename is always <number of downloaded segment>.ts
//
// Short explanation:
// The actual crunchyroll video is split up in multiple segments (or video files) which have to be downloaded and merged after to generate a single video file.
// And this function just downloads each of this segment into the given directory.
// See https://en.wikipedia.org/wiki/MPEG_transport_stream for more information
func (f *Format) DownloadSegments(outputDir string, goroutines int, onSegmentDownload func(segment *m3u8.MediaSegment, current, total int, file *os.File) error) error {
resp, err := f.crunchy.Client.Get(f.Video.URI)
if err != nil {
return err
}
defer resp.Body.Close()
// reads the m3u8 file
playlist, _, err := m3u8.DecodeFrom(resp.Body, true)
if err != nil {
return err
}
// extracts the segments from the playlist
var segments []*m3u8.MediaSegment
for _, segment := range playlist.(*m3u8.MediaPlaylist).Segments {
// some segments are nil, so they have to be filtered out
if segment != nil {
segments = append(segments, segment)
}
}
var wg sync.WaitGroup
chunkSize := int(math.Ceil(float64(len(segments)) / float64(goroutines)))
// when a onSegmentDownload call returns an error, this channel will be set to true and stop all goroutines
quit := make(chan bool)
// receives the decrypt block and iv from the first segment.
// in my tests, only the first segment has specified this data, so the decryption data from this first segments will be used in every other segment too
block, iv, err := f.getCrypt(segments[0])
if err != nil {
return err
}
var total int32
for i := 0; i < len(segments); i += chunkSize {
wg.Add(1)
end := i + chunkSize
if end > len(segments) {
end = len(segments)
}
i := i
go func() {
for j, segment := range segments[i:end] {
select {
case <-quit:
break
default:
var file *os.File
k := 1
for ; k < 4; k++ {
file, err = f.downloadSegment(segment, filepath.Join(outputDir, fmt.Sprintf("%d.ts", i+j)), block, iv)
if err == nil {
break
}
// sleep if an error occurs. very useful because sometimes the connection times out
time.Sleep(5 * time.Duration(k) * time.Second)
}
if k == 4 {
quit <- true
return
}
if onSegmentDownload != nil {
if err = onSegmentDownload(segment, int(atomic.AddInt32(&total, 1)), len(segments), file); err != nil {
quit <- true
file.Close()
return
}
}
file.Close()
}
}
wg.Done()
}()
}
wg.Wait()
select {
case <-quit:
return err
default:
return nil
}
}
// getCrypt extracts the key and iv of a m3u8 segment and converts it into a cipher.Block block and a iv byte sequence
func (f *Format) getCrypt(segment *m3u8.MediaSegment) (block cipher.Block, iv []byte, err error) {
var resp *http.Response
resp, err = f.crunchy.Client.Get(segment.Key.URI)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
key, err := ioutil.ReadAll(resp.Body)
block, err = aes.NewCipher(key)
if err != nil {
return nil, nil, err
}
iv = []byte(segment.Key.IV)
if len(iv) == 0 {
iv = key
}
return block, iv, nil
}
// downloadSegment downloads a segment, decrypts it and names it after the given index
func (f *Format) downloadSegment(segment *m3u8.MediaSegment, filename string, block cipher.Block, iv []byte) (*os.File, error) {
// every segment is aes-128 encrypted and has to be decrypted when downloaded
content, err := decryptSegment(f.crunchy.Client, segment, block, iv)
if err != nil {
return nil, err
}
file, err := os.Create(filename)
if err != nil {
return nil, err
}
defer file.Close()
if _, err = file.Write(content); err != nil {
return nil, err
}
return file, nil
}
// mergeSegments reads every file in tempPath and writes their content to output
func (f *Format) mergeSegments(tempPath string, output *os.File) error {
dir, err := os.ReadDir(tempPath)
if err != nil {
return err
}
writer := bufio.NewWriter(output)
defer writer.Flush()
// sort the directory files after their numeric names
sort.Slice(dir, func(i, j int) bool {
iNum, err := strconv.Atoi(strings.Split(dir[i].Name(), ".")[0])
if err != nil {
return false
}
jNum, err := strconv.Atoi(strings.Split(dir[j].Name(), ".")[0])
if err != nil {
return false
}
return iNum < jNum
})
for _, file := range dir {
bodyAsBytes, err := ioutil.ReadFile(filepath.Join(tempPath, file.Name()))
if err != nil {
return err
}
if _, err = writer.Write(bodyAsBytes); err != nil {
return err
}
}
return nil
}