-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (125 loc) · 3.26 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
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
)
// mov spec: https://developer.apple.com/standards/qtff-2001.pdf
// Page 31-33 contain information used in this file
const appleEpochAdjustment = 2082844800
const (
movieResourceAtomType = "moov"
movieHeaderAtomType = "mvhd"
referenceMovieAtomType = "rmra"
compressedMovieAtomType = "cmov"
)
var separator = "/"
func init() {
if runtime.GOOS == "windows" {
separator = "\\"
}
}
func main() {
dirpath := os.Args[1]
filepathList := ReadFileAbsolutePath(dirpath)
fmt.Println(filepathList)
for _, path := range filepathList {
func(path string) {
var oldname, newname string
fd, err := os.Open(path)
if err != nil {
panic(err)
}
defer func() {
fd.Close()
err = os.Rename(oldname, newname)
fmt.Println(err)
}()
info, _ := fd.Stat()
created, err := getVideoCreationTimeMetadata(fd)
if err != nil {
fmt.Println(err.Error())
return
}
filename := info.Name()
arr := strings.Split(filename, "_")
idx := arr[len(arr)-1]
nextname := fmt.Sprintf("VID_%s_%s", created.Format("20060102"), idx)
fmt.Printf("Movie created at %s (%s) %s\n", path, created, nextname)
oldname = path
newname = strings.ReplaceAll(oldname, filename, nextname)
}(path)
}
}
func ReadFileAbsolutePath(path string) []string {
ans := []string{}
fd, err := os.Open(path)
if err != nil {
panic(err)
}
defer fd.Close()
s, err := fd.Stat()
if err != nil {
panic(err)
}
if s.IsDir() {
dirabsolute := path + separator
entries, err := os.ReadDir(dirabsolute)
if err != nil {
panic(err)
}
for i := 0; i < len(entries); i++ {
entry := entries[i]
ans = append(ans, ReadFileAbsolutePath(path+separator+entry.Name())...)
}
return ans
}
lowerName := strings.ToLower(s.Name())
if strings.HasSuffix(lowerName, ".mov") || strings.HasSuffix(lowerName, ".mp4") {
ans = append(ans, path)
}
return ans
}
func getVideoCreationTimeMetadata(videoBuffer io.ReadSeeker) (time.Time, error) {
buf := make([]byte, 8)
// Traverse videoBuffer to find movieResourceAtom
for {
// bytes 1-4 is atom size, 5-8 is type
// Read atom
if _, err := videoBuffer.Read(buf); err != nil {
return time.Time{}, err
}
if bytes.Equal(buf[4:8], []byte(movieResourceAtomType)) {
break // found it!
}
atomSize := binary.BigEndian.Uint32(buf) // check size of atom
videoBuffer.Seek(int64(atomSize)-8, 1) // jump over data and set seeker at beginning of next atom
}
// read next atom
if _, err := videoBuffer.Read(buf); err != nil {
return time.Time{}, err
}
atomType := string(buf[4:8]) // skip size and read type
switch atomType {
case movieHeaderAtomType:
// read next atom
if _, err := videoBuffer.Read(buf); err != nil {
return time.Time{}, err
}
// byte 1 is version, byte 2-4 is flags, 5-8 Creation time
appleEpoch := int64(binary.BigEndian.Uint32(buf[4:])) // Read creation time
return time.Unix(appleEpoch-appleEpochAdjustment, 0).Local(), nil
case compressedMovieAtomType:
return time.Time{}, errors.New("Compressed video")
case referenceMovieAtomType:
return time.Time{}, errors.New("Reference video")
default:
return time.Time{}, errors.New("Did not find movie header atom (mvhd)")
}
}