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
| import struct
# BMP 파일 헤더 읽기 예시
def read_bmp_header(filepath: str) -> dict:
with open(filepath, 'rb') as f:
# BMP 파일 헤더 (14바이트)
header = f.read(14)
# 리틀 엔디언으로 파싱
magic, file_size, _, _, offset = struct.unpack('<2sIHHI', header)
return {
'magic': magic, # b'BM'
'file_size': file_size,
'data_offset': offset
}
# WAV 파일 헤더 예시
def read_wav_header(filepath: str) -> dict:
with open(filepath, 'rb') as f:
# RIFF 청크
riff, size, wave = struct.unpack('<4sI4s', f.read(12))
# fmt 청크
fmt, fmt_size = struct.unpack('<4sI', f.read(8))
audio_fmt, channels, sample_rate, byte_rate, block_align, bits = \
struct.unpack('<HHIIHH', f.read(16))
return {
'channels': channels,
'sample_rate': sample_rate,
'bits_per_sample': bits
}
|