-
Notifications
You must be signed in to change notification settings - Fork 101
/
mp4time.go
151 lines (140 loc) · 3.77 KB
/
mp4time.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
package m3u8d
import (
"encoding/binary"
"errors"
"github.com/xiaoqidun/setft"
"github.com/yapingcat/gomedia/go-mp4"
"io"
"os"
"time"
)
func UpdateMp4Time(firstTsName string, mp4FileName string) error {
stat, err := os.Stat(firstTsName)
if err != nil {
return errors.New("读取文件状态失败: " + err.Error())
}
mTime := stat.ModTime()
err = updateMp4CreateTime(mp4FileName, mTime)
if err != nil {
return errors.New("更新mp4创建时间失败: " + err.Error())
}
err = setft.SetFileTime(mp4FileName, mTime, mTime, mTime)
if err != nil {
return errors.New("更新文件时间属性失败: " + err.Error())
}
return nil
}
func mov_tag(tag [4]byte) uint32 {
return binary.LittleEndian.Uint32(tag[:])
}
func updateMp4CreateTime(mp4Path string, ctime time.Time) error {
timeBuf := make([]byte, 4)
binary.BigEndian.PutUint32(timeBuf, uint32(ctime.Unix()))
mp4Fd, err := os.OpenFile(mp4Path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
defer mp4Fd.Close()
var posList []int64
var zeroPosList []int64
for err == nil {
basebox := mp4.BasicBox{}
_, err = basebox.Decode(mp4Fd)
if err != nil {
break
}
if basebox.Size < mp4.BasicBoxLen {
err = errors.New("mp4 Parser error")
break
}
tagName := mov_tag(basebox.Type)
switch tagName {
case mov_tag([4]byte{'m', 'o', 'o', 'v'}):
break
case mov_tag([4]byte{'m', 'v', 'h', 'd'}):
var offset int64
if offset, err = mp4Fd.Seek(0, io.SeekCurrent); err != nil {
break
}
mvhd := mp4.MovieHeaderBox{Box: new(mp4.FullBox)}
if _, err = mvhd.Decode(mp4Fd); err != nil {
break
}
if mvhd.Box.Version == 0 {
posList = append(posList, offset+4) //create time
posList = append(posList, offset+8) //modify time
} else {
zeroPosList = append(zeroPosList, offset+4)
posList = append(posList, offset+8) //create time
zeroPosList = append(zeroPosList, offset+12)
posList = append(posList, offset+16) //modify time
}
case mov_tag([4]byte{'t', 'r', 'a', 'k'}):
break
case mov_tag([4]byte{'m', 'd', 'i', 'a'}):
break
case mov_tag([4]byte{'m', 'd', 'h', 'd'}):
var offset int64
if offset, err = mp4Fd.Seek(0, io.SeekCurrent); err != nil {
break
}
mdhd := mp4.MediaHeaderBox{Box: new(mp4.FullBox)}
if _, err = mdhd.Decode(mp4Fd); err != nil {
break
}
if mdhd.Box.Version == 0 {
posList = append(posList, offset+4) //create time
posList = append(posList, offset+8) //modify time
} else {
zeroPosList = append(zeroPosList, offset+4)
posList = append(posList, offset+8) //create time
zeroPosList = append(zeroPosList, offset+12)
posList = append(posList, offset+16) //modify time
}
case mov_tag([4]byte{'t', 'k', 'h', 'd'}):
var offset int64
if offset, err = mp4Fd.Seek(0, io.SeekCurrent); err != nil {
break
}
tkhd := mp4.TrackHeaderBox{Box: new(mp4.FullBox)}
if _, err = tkhd.Decode(mp4Fd); err != nil {
break
}
if tkhd.Box.Version == 0 {
posList = append(posList, offset+4) //create time
posList = append(posList, offset+8) //modify time
} else {
zeroPosList = append(zeroPosList, offset+4)
posList = append(posList, offset+8) //create time
zeroPosList = append(zeroPosList, offset+12)
posList = append(posList, offset+16) //modify time
}
default:
_, err = mp4Fd.Seek(int64(basebox.Size)-mp4.BasicBoxLen, io.SeekCurrent)
}
}
if err != io.EOF {
return err
}
for _, pos := range posList {
_, err = mp4Fd.Seek(pos, io.SeekStart)
if err != nil {
return err
}
_, err = mp4Fd.Write(timeBuf)
if err != nil {
return err
}
}
for _, pos := range zeroPosList {
_, err = mp4Fd.Seek(pos, io.SeekStart)
if err != nil {
return err
}
_, err = mp4Fd.Write([]byte{0, 0, 0, 0})
if err != nil {
return err
}
}
return nil
}