Last active
August 29, 2015 13:57
-
-
Save gwarser/9439356 to your computer and use it in GitHub Desktop.
Split binary joined ogg vorbis files (inverts copy /b file1.ogg+file2.ogg filetwo.ogg)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! python3 | |
# -*- coding: UTF-8 -*- | |
try: | |
import os, sys | |
except ImportError: | |
import traceback | |
print('Import fails:\n') | |
traceback.print_exc() | |
input('Press enter...') | |
def find_indexes(content): | |
curidx = -1 | |
idxlst = [] | |
while True: | |
try: | |
curidx = content.index(b'\x4F\x67\x67\x53\x00\x02', curidx+1) | |
except ValueError: | |
break | |
idxlst.append(curidx) | |
if idxlst: | |
idxlst.append(len(content)) | |
return idxlst | |
def dump_parts(idxlst, content, basename): | |
for i in range(0, len(idxlst) - 1): | |
path_ext = os.path.splitext(basename) | |
with open(path_ext[0] + '-' + str(i) + path_ext[1], 'wb') as f: | |
f.write(content[idxlst[i]:idxlst[i+1]]) | |
def process(f): | |
print('File:', f.name) | |
content = f.read() | |
print('Length:', len(content)) | |
indexes = find_indexes(content) | |
print('Indexes:', indexes) | |
if indexes: | |
dump_parts(indexes, content, f.name) | |
def main(): | |
if len(sys.argv) == 2 and os.path.isfile(sys.argv[1]): | |
with open(sys.argv[1], 'rb') as file: | |
process(file) | |
''' | |
def main(): | |
if len(sys.argv) > 1: | |
flist = [os.path.normpath(p) for p in sys.argv[1:]] | |
else: | |
flist = os.listdir(os.getcwd()) | |
process(flist) | |
''' | |
if __name__ == '__main__': | |
try: | |
main() | |
except Exception: | |
import traceback | |
print('Unhandled exception:\n') | |
traceback.print_exc() | |
input('Press enter...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment