Skip to content

Instantly share code, notes, and snippets.

@rniwase
Created August 8, 2023 14:59
Show Gist options
  • Save rniwase/6f430e676c59175591f3388cd73acdd3 to your computer and use it in GitHub Desktop.
Save rniwase/6f430e676c59175591f3388cd73acdd3 to your computer and use it in GitHub Desktop.
バイナリファイル内に含まれるRIFFフォーマットを検索・抽出してWAVファイルとして出力する
with open("inputfile", "rb") as f:
s = f.read()
idx = -1
count = -1
while True:
idx = s.find(b"RIFF", idx + 1)
if idx == -1:
break
if s[idx+8:idx+12] != b"WAVE":
continue
fsize = int.from_bytes(s[idx+4:idx+8], "big") + 8
if fsize < 64:
continue
count += 1
print(fsize)
with open("out/{:0>2}.wav".format(count), "wb") as f:
f.write(s[idx:idx+fsize])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment