This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
95 lines (76 loc) · 2.28 KB
/
example.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
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"github.com/Cryptkeeper/go-fseq"
"github.com/Cryptkeeper/go-fseq/pkg/v1"
"github.com/Cryptkeeper/go-fseq/pkg/v2"
"io"
"io/ioutil"
"log"
"os"
)
func main() {
var path = flag.String("path", "in.fseq", "input file to read")
flag.Parse()
b, err := ioutil.ReadFile(*path)
if err != nil {
log.Fatal(err)
}
_, majorVersion := fseq.ReadVersion(b) // discard minorVersion return
switch majorVersion {
case v1.MajorVersion:
// go-fseq supports the v1 format, this is simply an example usage of version matching
log.Fatal("outdated format version, v1!")
case v2.MajorVersion:
var r = bytes.NewReader(b)
// fseq files are encoded as little-endian
var h v2.Header
if err := binary.Read(r, binary.LittleEndian, &h); err != nil {
log.Fatal(err)
}
// If the file is compressed, dump the Compression Blocks into files
// These will not be decompressed, simply read and separated
if h.Compression != v2.None {
var blocks = make([]v2.CompressionBlock, h.CompressionBlockCount)
for i := 0; i < int(h.CompressionBlockCount); i++ {
if err := binary.Read(r, binary.LittleEndian, &blocks[i]); err != nil {
log.Fatal(err)
}
// fpp source code ignores 0 length Compression Blocks (no idea why they're encoded, alignment?)
if blocks[i].Length == 0 {
// Trim array since it isn't full
blocks = blocks[:i]
break
}
}
log.Printf("%+v\n", blocks)
// Compression Block offsets are relative to Channel Data Start Offset
if _, err := r.Seek(int64(h.ChannelDataStartOffset), io.SeekStart); err != nil {
log.Fatal(err)
}
for _, block := range blocks {
var b = make([]uint8, block.Length)
if n, err := r.Read(b); err != nil {
log.Fatal(err)
} else if n != int(block.Length) {
log.Fatalf("n = %d, expected %d", n, block.Length)
}
var out = fmt.Sprintf("frame_%d.bin", block.FrameNumber)
log.Println("writing:", out)
// Write the Compression Block out to a file
if err := ioutil.WriteFile(out, b, os.ModePerm); err != nil {
log.Fatal(err)
}
}
// Ensure the full file has been read
if r.Len() != 0 {
log.Fatalf("%d bytes remaining, should be empty!", r.Len())
}
}
default:
log.Fatal("unknown format version!")
}
}