-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmv.py
165 lines (143 loc) · 5.06 KB
/
gmv.py
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
#
# GMV(Gob MoVie file)
# gmv.gy jpg_dir wav_path fps output_path
#
import os
import sys
import argparse
import glob
from ctypes import *
import struct
import math
from fractions import Fraction
class WaveHeader(LittleEndianStructure):
_pack = 1
_fields_ = [
('RIFF', c_uint32),
('chunk_size', c_uint32),
('WAVEfmt', c_uint8 * 8),
('fmt_chunk_size;', c_uint32),
('audiofmt;', c_uint16),
('channel', c_uint16),
('sample_rate', c_uint32),
('byte_per_sec', c_uint32),
('block_size', c_uint16),
('bit_per_sample', c_uint16)
]
def __init__(self):
self.RIFF = 0x46464952; # RIFF"
self.chunk_size = 0
self.WAVEFmt = [0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20 ]; # "WAVEfmt "
self.fmt_chunk_size = 0
self.audiofmt = 0
self.channel = 0
self.sample_rate = 0;
self.byye_per_sec = 0
self.bit_per_sample = 0
def __repr__(self):
return 'RIFF:{:x} chunk_size:{} fmt_chunk_size:{} audiofmt:{} blocksize:{} channel:{} sample_rate:{} byte_per_sec:{}'.format(self.RIFF, self.chunk_size, self.fmt_chunk_size, self.audiofmt, self.block_size, self.channel, self.sample_rate, self.byte_per_sec)
class SubChunk(LittleEndianStructure):
_pack = 1
_fields_ = [
('identifier', c_uint32),
('chunk_size', c_uint32),
]
def __init__(self, identifier = 0, sz = 0):
self.identifier = identifier
self.chunk_size = sz
def __repr__(self):
return 'identifier:{:x}, chnk_size:{}'.format(self.identifier, self.chunk_size)
class GMV0(LittleEndianStructure):
_pack_ = 1
_fields_ = [
('signature', c_uint32),
('blocks', c_uint32),
('gcfOffset', c_uint32),
('fps', c_float),
]
def __init__(self, blks = 0, fps = 1.0):
self.signature = 0x30564d47 # "GMV0"
self.blocks = blks
self.gcfOffset = sizeof(GMV0) + sizeof(WaveHeader)
self.fps = fps
def loadWav(fname):
wh = WaveHeader()
sub = SubChunk()
data = []
with open(fname, 'rb') as f:
if f.readinto(wh) != sizeof(WaveHeader):
raise OSError
if f.readinto(sub) != sizeof(SubChunk):
raise OSError
data = f.read(sub.chunk_size)
while sub.identifier != 0x61746164: # "data"
if f.readinto(sub) != sizeof(SubChunk):
raise OSError
data = f.read(sub.chunk_size)
return wh, sub, data
def main():
parser = argparse.ArgumentParser(description='Create GMV file from JPEG files and wav file')
parser.add_argument('dirname', help='Target image directory')
parser.add_argument('wavfile', help='Target wav file')
parser.add_argument('fps', type=float, help='Base frame per second')
parser.add_argument('outfile', help='Output filename')
parser.add_argument('--ext', '-e', type=str, default='jpg', help='Target image file extension')
parser.add_argument('--verbose', '-v', action='store_true')
args = parser.parse_args()
wild = '*.' + args.ext
fileFilter = os.path.join(os.getcwd(), args.dirname, wild)
fileList = sorted(glob.glob(fileFilter))
files = len(fileList)
if(files == 0):
print('Not exists image files', fileFilter)
return 0
wh,sub,wdata = loadWav(args.wavfile)
wrest = len(wdata)
wpos = 0
wmod = 0
wblk = int(math.modf(wh.byte_per_sec / args.fps)[1])
wblk &= ~(wh.block_size - 1)
wadd = wh.block_size
frac = Fraction(math.modf(wh.sample_rate / args.fps)[0]).limit_denominator()
if args.verbose:
print('images:{}'.format(files))
print(wh)
print(sub)
print('data len:{} blocksize:{} frac:{} add:{}'.format(wrest, wblk, frac, wadd))
# Output GMV
with open(args.outfile, 'wb') as outf:
# GMV header
header = GMV0(files, args.fps)
outf.write(header)
outf.write(wh)
# Image and wav block
for name in fileList:
# Read imgae block
with open(name, 'rb') as inf:
d = inf.read()
sz = len(d)
# image size
outf.write(struct.pack('<L', sz))
# wav block size
wsz = wblk
wmod += frac
if wmod >= Fraction(1,1):
wmod -= Fraction(1,1)
wsz += wadd
if(wsz > wrest): wsz = wrest
outf.write(struct.pack('<L', wsz))
# image data and wav data
if sz > 0:
outf.write(d)
if wsz > 0:
outf.write(wdata[wpos: wpos + wsz])
wpos += wsz
wrest -= wsz
if args.verbose:
print('Image {} size:{} wsize:{} wacu:{}'.format(name, sz, wsz, wpos))
outf.write(struct.pack('<L', 0xFFFFFFFF))
outf.write(struct.pack('<L', 0xFFFFFFFF))
outf.flush();
outf.close()
if __name__ == '__main__':
sys.exit(main())