-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io.go
117 lines (98 loc) · 2.21 KB
/
file_io.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
package main
import (
"path/filepath"
"io/ioutil"
"os"
"fmt"
)
func convertToAbsolutePath(path string) (string, error) {
// Check if the path is already absolute
if filepath.IsAbs(path) {
return path, nil
}
// Convert relative path to absolute path
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
return absPath, nil
}
func getBasenameWithoutExt(absPath string) string {
// Get the base name of the file
base := filepath.Base(absPath)
// Remove the file extension
ext := filepath.Ext(base)
basename := base[:len(base)-len(ext)]
return basename
}
func cleanPathsAndTempDir() {
var err error
inputVideo, err = convertToAbsolutePath(inputVideo)
if err != nil {
fmt.Println("Couldn't find absolute path of input video")
selfExit()
return
}
outputDir, err = convertToAbsolutePath(outputDir)
if err != nil {
fmt.Println("Couldn't find absolute path of output directory")
selfExit()
return
}
// Create a temporary directory for frames
tempDirFrames, err = ioutil.TempDir("", "tinyframes")
if err != nil {
fmt.Println(err.Error())
selfExit()
return
}
// Create a temporary directory for tiny video
tempDirTinyVid, err = ioutil.TempDir("", "tinyvideo")
if err != nil {
fmt.Println(err.Error())
selfExit()
return
}
}
func rmTempDirs() {
fmt.Println("Cleaned up.")
os.RemoveAll(tempDirFrames)
os.RemoveAll(tempDirTinyVid)
}
func getFilesInPath(path string) ([]string, bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, false, err
}
if fileInfo.IsDir() {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, true, err
}
var fileNames []string
for _, file := range files {
fileNames = append(fileNames, file.Name())
}
return fileNames, true, nil
}
return nil, false, nil
}
func deleteFilesInFolder(folderPath string) error {
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
err := os.Remove(path)
if err != nil {
return err
}
// fmt.Printf("Deleted file: %s\n", path)
}
return nil
})
if err != nil {
return err
}
return nil
}