forked from Hell0wor1d/youkuMP4Patcher
/
patcher.go
135 lines (122 loc) · 2.9 KB
/
patcher.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
// patcher.go
// 修复从手机端导出的缓存优酷MP4文件只能在优酷播放器播放的问题
// 优酷对MP4源文件进行了简单的加密处理,导致只能在优酷播放器里播放
// 修复后的MP4文件可以在任意播放器里播放
// fix youku mp4 file.
// https://github.com/Hell0wor1d
package main
import (
"fmt"
"github.com/cheggaaa/pb"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
)
const (
FILE_EXT = ".mp4"
FILE_BUFFER = 1024
)
func main() {
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) <= 0 {
fmt.Println("Please enter a directory or file path.\r\nroot@kev7n.com")
return
}
target, err := os.Stat(argsWithoutProg[0])
if os.IsNotExist(err) {
fmt.Printf("No such file or directory: %s", argsWithoutProg[0])
return
}
//process directory
if target.IsDir() {
files, _ := ioutil.ReadDir(argsWithoutProg[0])
for _, file := range files {
if file.IsDir() {
continue
} else {
filePath := path.Join(argsWithoutProg[0], file.Name())
if path.Ext(filePath) == FILE_EXT {
PatchFile(filePath)
}
}
}
} else {
if path.Ext(argsWithoutProg[0]) == FILE_EXT {
PatchFile(argsWithoutProg[0])
}
}
}
func PatchFile(fName string) {
srcFile, err := os.Open(fName) // For read access.
if err != nil {
log.Fatal(err)
}
// close srcFile on exit and check for its returned error
defer func() {
if err := srcFile.Close(); err != nil {
log.Fatal(err)
}
}()
stat, err := srcFile.Stat()
if err != nil {
log.Fatal(err)
}
srcFileSize := stat.Size()
srcFile.Seek(srcFileSize-8, 0)
data := make([]byte, 8)
count, err := srcFile.Read(data)
if err != nil {
log.Fatal(err)
}
flat := string(data[:count])
if strings.Contains(flat, "ftyp") {
fNameInfo := strings.Split(fName, ".")
newFileName := fNameInfo[0] + "_patched." + fNameInfo[1]
// equivalent to Python's `if os.path.exists(filename)`
if _, err := os.Stat(newFileName); err == nil {
//log.Println("Patched file exists.", newFileName)
return
}
newFile, err := os.Create(newFileName)
if err != nil {
log.Fatal(err)
}
// close newFile on exit and check for its returned error
defer func() {
if err := newFile.Close(); err != nil {
log.Fatal(err)
}
}()
newFile.Write(data)
// make a buffer to keep chunks that are read
buf := make([]byte, FILE_BUFFER)
srcFile.Seek(8, 0)
bar := pb.StartNew(int(srcFileSize) - 8)
bar.ShowCounters = false
var readLength int64 = 0
for {
n, err := srcFile.Read(buf)
readLength += int64(n)
if err != nil && err != io.EOF {
log.Fatal(err)
}
if n == 0 {
break
}
if readLength >= (srcFileSize - 8) {
n = n - 8
}
bar.Add(n)
// write a chunk
if _, err := newFile.Write(buf[:n]); err != nil {
log.Fatal(err)
}
}
bar.FinishPrint("The srcFile " + fName + " has been patched successfully.")
} else {
log.Println("The file dose not need to patch.", fName)
}
}