-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (239 loc) · 5.41 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"encoding/hex"
"errors"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"math"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/liyue201/goqr"
"gocv.io/x/gocv"
//"log"
"bufio"
"encoding/base64"
"encoding/json"
"flag"
"os"
"path/filepath"
"github.com/cheggaaa/pb/v3"
"golang.org/x/crypto/blake2b"
)
type MetaData struct {
Filename string
ChunkCount int
Filehash string
ConverterUrl string
ConverterVersion string
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func hashFile(filename string) (hashstr string) {
hasher, _ := blake2b.New256(nil)
path := filename
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if _, err := io.Copy(hasher, f); err != nil {
log.Fatal(err)
}
hash := hasher.Sum(nil)
hashstr = hex.EncodeToString(hash[:])
return
}
func recognizeQR(img image.Image) (string, error) {
qrCodes, err := goqr.Recognize(img)
if err != nil {
return "", err
}
if len(qrCodes) != 1 {
return "", errors.New("more then 1")
}
return string(qrCodes[0].Payload), nil
}
const WIDTH = 1080
const HEIGHT = 1080
func main() {
source := flag.String("src", "", "-src source.file")
destination := flag.String("dst", "", "-dst destination file for F2V, destination folder for V2F")
convert := flag.Bool("F2V", false, "-F2V for convert file to video")
reconvert := flag.Bool("V2F", false, "-V2F for convert video to file")
flag.Parse()
if *convert == *reconvert || *source == "" || *destination == "" {
flag.Usage()
os.Exit(2)
}
if *convert {
convert2Video(*source, *destination)
} else if *reconvert {
convert2File(*source, *destination)
}
}
func convert2File(source, destination string) {
video, err := gocv.OpenVideoCapture(source)
checkErr(err)
defer video.Close()
img := gocv.NewMat()
defer img.Close()
fmt.Printf("Start reading device: %v\n", video)
checkErr(err)
firstFrame := true
var metadata MetaData
var bar *pb.ProgressBar
var file *os.File
for {
if ok := video.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", video)
return
}
if img.Empty() {
continue
}
img, err := img.ToImage()
checkErr(err)
res, err := recognizeQR(img)
checkErr(err)
data, err := base64.StdEncoding.DecodeString(res)
checkErr(err)
if firstFrame {
json.Unmarshal(data, &metadata)
firstFrame = false
file, err = os.Create(filepath.Join(destination, metadata.Filename))
checkErr(err)
bar = pb.StartNew(metadata.ChunkCount)
} else {
_, err = file.Write(data)
checkErr(err)
bar.Increment()
}
}
file.Close()
receivedHash := hashFile(filepath.Join(destination, metadata.Filename))
metadataHash := metadata.Filehash
if receivedHash != metadataHash {
checkErr(errors.New("expected: " + metadataHash + "\n received: " + receivedHash))
}
bar.Finish()
}
func convert2Video(source, destination string) {
path := filepath.Dir(destination)
stat, err := os.Stat(path)
checkErr(err)
if !stat.IsDir() {
checkErr(errors.New("parent is not directory"))
}
videowriter, err := gocv.VideoWriterFile(destination, "H264", 24, WIDTH, HEIGHT, true)
checkErr(err)
fileChunks := make(chan []byte, 5)
count := readCunks(source, 500, fileChunks)
bar := pb.StartNew(count + 1)
base64Chunks := make(chan string, 5)
go func() {
defer close(base64Chunks)
for chunk := range fileChunks {
base64Chunks <- encodeBase64(chunk)
}
}()
images := make(chan image.Image, 5)
go func() {
defer close(images)
for chunk := range base64Chunks {
images <- encodeQr(chunk)
}
}()
done := make(chan int)
go func() {
for image := range images {
//test, err := recognizeQR(image)
//checkErr(err)
//fmt.Println(test)
img, err := gocv.ImageToMatRGB(image)
checkErr(err)
err = videowriter.Write(img)
checkErr(err)
bar.Increment()
}
videowriter.Close()
bar.Finish()
done <- 1
}()
<-done
}
func encodeBase64(data []byte) string {
encoded := base64.StdEncoding.EncodeToString(data)
return encoded
}
func encodeQr(data string) image.Image {
// Create the barcode
qrCode, err := qr.Encode(data, qr.M, qr.Auto)
checkErr(err)
// Scale the barcode to 200x200 pixels
qrCode, err = barcode.Scale(qrCode, WIDTH, HEIGHT)
checkErr(err)
return qrCode
}
// https://zetcode.com/golang/readfile/
func readCunks(filename string, chunkSize int, chunkChan chan []byte) (count int) {
hash := hashFile(filename)
f, err := os.Open(filename)
checkErr(err)
fi, err := f.Stat()
checkErr(err)
filesize := fi.Size()
d := float64(filesize) / float64(chunkSize)
count = int(math.Ceil(d))
m := MetaData{
Filename: filepath.Base(filename),
ChunkCount: count,
Filehash: hash,
ConverterUrl: "https://github.com/karaketir16/go-File2Video",
ConverterVersion: "V0.1",
}
metadata, err := json.Marshal(m)
checkErr(err)
chunkChan <- metadata
go func() {
defer f.Close()
defer close(chunkChan)
reader := bufio.NewReader(f)
checker := false
var err error
n := 0
a := 0
var buf []byte
for {
if checker {
a, err = reader.Read(buf[n:])
n = n + a
} else {
buf = make([]byte, chunkSize)
n, err = reader.Read(buf)
}
if err != nil {
if err != io.EOF {
checkErr(err)
}
break
}
checker = true
if n < chunkSize {
continue
}
chunkChan <- buf[:n]
checker = false
}
if checker {
chunkChan <- buf[:n]
}
}()
return
}