-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
227 lines (194 loc) · 4.16 KB
/
main.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
// bifextract - CLI utility for extracting images from a BIF file.
// https://sdkdocs.roku.com/display/sdkdoc/Trick+Mode+Support
package main
import (
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
const (
usage = `
Usage:
bifextract <file-path|url> <output-dir>
`
version = "0.1.0"
)
var (
file string
outputDir string
)
// BIF represents BIF file data.
type BIF struct {
File *os.File
}
// Frame represents each frame in BIF.
type Frame struct {
Timestamp uint32
Offset uint32
}
// Initialize and parse flags/arguments
func init() {
if len(os.Args[1:]) < 1 {
fmt.Println("Please provide a BIF path or URL.")
fmt.Println(usage)
os.Exit(1)
}
// TODO: Support URL paths.
// isHTTP := strings.HasPrefix(os.Args[len(os.Args)-1], "http")
}
func main() {
args := os.Args[1:]
option := args[0]
if option == "version" {
fmt.Printf("Version: %s\n", version)
} else {
file = option
outputDir = args[1]
extractBIF()
}
}
func extractBIF() {
// Open file.
f, err := os.Open(file)
if err != nil {
panic(err)
}
// Load BIF instance.
bif, _ := NewBIF(f)
// Version
v := bif.getVersion()
fmt.Printf("BIF Version: %d\n", v)
// Frame Count
fc := bif.getFramesCount()
fmt.Printf("Number of frames: %d\n", fc)
// Framewise Separation
fs := bif.getFramewiseSeparation()
fmt.Printf("Framewise Separation: %d ms\n", fs)
// Get frames
var byteIndex int64 = 64 // BIF index starts at byte 64.
var frames []Frame
for i := 0; i < fc; i++ {
ts, offset := bif.readFrame(byteIndex)
frame := Frame{
Timestamp: ts * fs,
Offset: offset,
}
frames = append(frames, frame)
byteIndex += 8 // Next frame every 8 bytes.
}
// Generate frame images to output dir.
fmt.Printf("Generating %d frames...\n", len(frames))
for k, v := range frames {
var nextOffset uint32
if k == len(frames)-1 {
// nextOffset = frames[len(frames)-1].Offset
fmt.Println("Finished.")
} else {
nextOffset = frames[k+1].Offset
// Calculate frame image size from next offset.
frameLen := nextOffset - v.Offset
// Create image.
bif.createFrameImage(k, int64(v.Offset), int(frameLen), outputDir)
}
}
f.Close()
}
// NewBIF Creates a BIF instance.
func NewBIF(f *os.File) (*BIF, error) {
// Validate filetype
err := checkBIF(f)
if err != nil {
return &BIF{
File: f,
}, err
}
return &BIF{
File: f,
}, nil
}
func checkBIF(f *os.File) error {
b := make([]byte, 8)
_, err := f.ReadAt(b, 0)
if err != nil {
panic(err)
}
magic := string(b)
isBIF := strings.Contains(magic, "BIF")
if !isBIF {
return errors.New("invalid BIF file")
}
return nil
}
func (b *BIF) getVersion() uint32 {
f := b.File
f.Seek(8, 0)
buf := make([]byte, 4)
_, err := f.Read(buf)
if err != nil {
panic(err)
}
version := binary.LittleEndian.Uint32(buf)
return version
}
func (b *BIF) getFramesCount() int {
f := b.File
f.Seek(12, 0)
buf := make([]byte, 4)
_, err := f.Read(buf)
if err != nil {
panic(err)
}
numFrames := binary.LittleEndian.Uint32(buf)
return int(numFrames)
}
func (b *BIF) getFramewiseSeparation() uint32 {
f := b.File
f.Seek(16, 0)
buf := make([]byte, 4)
_, err := f.Read(buf)
if err != nil {
panic(err)
}
framewiseSeparation := binary.LittleEndian.Uint32(buf)
return framewiseSeparation
}
func (b *BIF) readFrame(offset int64) (uint32, uint32) {
f := b.File
f.Seek(offset, 0)
buf := make([]byte, 4)
f.Read(buf)
frameTimestamp := binary.LittleEndian.Uint32(buf)
f.Seek(offset+4, 0)
b2 := make([]byte, 4)
f.Read(b2)
frameOffset := binary.LittleEndian.Uint32(b2)
return frameTimestamp, frameOffset
}
func (b *BIF) getFrameImage(offset int64, len int) string {
f := b.File
f.Seek(offset, 0)
buf := make([]byte, len)
f.Read(buf)
enc := base64.StdEncoding.EncodeToString(buf)
return enc
}
func (b *BIF) createFrameImage(i int, offset int64, len int, out string) {
// Create output dir.
if _, err := os.Stat(out); os.IsNotExist(err) {
os.Mkdir(out, os.ModePerm)
}
f := b.File
f.Seek(offset, 0)
buf := make([]byte, len)
f.Read(buf)
filename := fmt.Sprintf("%s/frame_%s.jpg", out, strconv.Itoa(i))
err := ioutil.WriteFile(filename, buf, 0644)
if err != nil {
panic(err)
}
}