-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdm4.go
More file actions
181 lines (144 loc) · 3.5 KB
/
Copy pathpdm4.go
File metadata and controls
181 lines (144 loc) · 3.5 KB
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
package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
)
type ByLength []string
type InputParam struct {
outputname, pathtofolder, inputname string
}
// regexp for detect files and sort it
// example name file: samefilename_X_XXX.mp4
var digitsRegexp = regexp.MustCompile(`(\w+?)_(\d)_(\d+)\.mp4`)
// regexp for detect first file
// example: samefilename_X_.mp4
var firstfile = regexp.MustCompile(`\w+_\d_\.mp4`)
func (s ByLength) Len() int {
return len(s)
}
func (s ByLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByLength) Less(i, j int) bool {
res_i := digitsRegexp.FindStringSubmatch(s[i])
res_j := digitsRegexp.FindStringSubmatch(s[j])
if len(res_i) == 0 {
return false
}
if len(res_j) == 0 {
return false
}
res_i_in_digit, _ := strconv.Atoi(res_i[3])
res_j_in_digit, _ := strconv.Atoi(res_j[3])
return res_i_in_digit < res_j_in_digit
}
func RemoveOutputFile(filename string) {
os.Remove(filename)
}
func PackToMP4(file string, outputfilename string) {
fmt.Println(file)
r, err := os.Open(file)
if err != nil {
panic(err)
}
defer r.Close()
w, err := os.OpenFile(outputfilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer w.Close()
// do the actual work
n, err := io.Copy(w, r)
if err != nil {
panic(err)
}
fmt.Printf("Copied %v bytes\n", n)
}
func OpenFolder(pathtofolder string) []os.FileInfo {
d, err := os.Open(pathtofolder)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer d.Close()
files, err := d.Readdir(-1)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return files
}
func FindFiles(param InputParam) (ByLength, string) {
files := OpenFolder(param.pathtofolder)
var filelist ByLength
var firstfilename string
for _, file := range files {
if !(file.Mode().IsRegular()) {
continue
}
if !(filepath.Ext(file.Name()) == ".mp4") {
continue
}
if file.Name() == param.outputname {
continue
}
matched, _ := filepath.Match(param.inputname+"*", file.Name())
if !(matched) {
continue
}
res := firstfile.MatchString(file.Name())
if res {
firstfilename = file.Name()
// PackToMP4(file.Name(), param.outputname)
} else {
filelist = append(filelist, file.Name())
}
}
return filelist, firstfilename
}
func ParseInputParam() InputParam {
var param InputParam
outputname := flag.String("o", "out.mp4", "Name for output file")
pathtofolder := flag.String("p", "."+string(filepath.Separator), "Path to work folder")
inputname := flag.String("i", "All", "Base name for input files")
flag.Parse()
param.outputname = *outputname
param.pathtofolder = *pathtofolder
param.inputname = *inputname
fmt.Printf("\ninput param:\n output name - %s;\n work dir path - %s;\n input filter (by name) - %s\n",
param.outputname,
param.pathtofolder,
param.inputname)
if param.inputname == "All" {
param.inputname = ""
}
RemoveOutputFile(param.outputname)
return param
}
func ResultFileStat(name string) {
stat, _ := os.Stat(name)
fmt.Println("Create file ", name, " size - ", stat.Size())
}
func main() {
param := ParseInputParam()
filelist, firstfilename := FindFiles(param)
fmt.Println("\nCopy files to ", param.outputname, ":")
if firstfilename == "" {
fmt.Println("error! no first file youstream_0_.mp4")
os.Exit(1)
}
PackToMP4(firstfilename, param.outputname)
sort.Sort(filelist)
for _, filename := range filelist {
if digitsRegexp.MatchString(filename) {
PackToMP4(filename, param.outputname)
}
}
ResultFileStat(param.outputname)
}