Skip to content

Instantly share code, notes, and snippets.

@bakugo
Last active May 18, 2018 19:44
Show Gist options
  • Select an option

  • Save bakugo/1ca9af707ee562e7d7645719eef4b74b to your computer and use it in GitHub Desktop.

Select an option

Save bakugo/1ca9af707ee562e7d7645719eef4b74b to your computer and use it in GitHub Desktop.
Python script to extract FWAVs from BARS files in Nintendo Switch & Wii U games
import sys
import os
def main(argv):
files = argv[1:]
if len(files) == 0:
return 1
for file in files:
try:
file_o = open(file, "rb")
except:
continue
print(os.path.basename(file))
pathout = (
os.path.dirname(file) + "/" +
os.path.splitext(os.path.basename(file))[0]
)
file_o.seek(0x0, os.SEEK_SET)
if file_o.read(0x4) != b"BARS":
continue
names = []
file_o.seek(0x0, os.SEEK_SET)
while True:
tmp = file_o.read(0x4)
if tmp == b"":
break
if tmp == b"STRG":
name = b""
file_o.seek(0x4, os.SEEK_CUR)
while True:
tmp = file_o.read(0x1)
if tmp == b"": break
if tmp == b"\x00": break
name += tmp
continue
if name != b"":
names.append(
name.decode("utf8"))
file_o.seek(
-(file_o.tell() % 0x4),
os.SEEK_CUR
)
continue
file_o.seek(0x0, os.SEEK_SET)
while True:
tmp = file_o.read(0x20)
if tmp == b"":
break
if tmp[0:0x4] == b"FWAV":
try:
endian = None
if tmp[0x4:0x6] == b"\xFF\xFE": endian = "little"
if tmp[0x4:0x6] == b"\xFE\xFF": endian = "big"
if endian == None:
raise Exception()
size = int.from_bytes(
bytes = tmp[0xC:0x10],
byteorder = endian,
signed = False
)
if size <= 0:
raise Exception()
if size > (1024*1024*100):
raise Exception()
file_o.seek(-0x20, os.SEEK_CUR)
name = (names.pop(0) + ".bfwav")
print(" " + name)
os.makedirs(name=pathout, exist_ok=True)
file_out_p = os.path.join(pathout, name)
file_out_o = open(file_out_p, "wb")
size_d = 0
size_c = 0
while size_d < size:
size_c = \
min((size - size_d), (1024*128))
file_out_o.write(
file_o.read(size_c))
size_d += size_c
file_out_o.close()
except:
pass
file_o.seek(
-(file_o.tell() % 0x20),
os.SEEK_CUR
)
continue
file_o.close()
continue
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment