Update offset_adjust.py

Update code to allow for shift-jis or utf-8 as well as making the process multi-threaded
This commit is contained in:
Cainan 2024-06-17 01:50:40 +01:00 committed by GitHub
parent 7481de651a
commit 6878ea4b82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,10 +2,11 @@ import re
import os import os
import shutil import shutil
from pydub import AudioSegment from pydub import AudioSegment
from concurrent.futures import ThreadPoolExecutor
def process_tja_and_audio(tja_path, output_dir=None, original_name=False): def process_tja_and_audio(tja_path, output_dir=None, original_name=False, encoding='shift_jis'):
# Read the TJA file contents # Read the TJA file contents
with open(tja_path, 'r', encoding='shift_jis') as file: with open(tja_path, 'r', encoding=encoding) as file:
tja_contents = file.read() tja_contents = file.read()
# Extract BPM, OFFSET, and WAVE # Extract BPM, OFFSET, and WAVE
@ -76,7 +77,7 @@ def process_tja_and_audio(tja_path, output_dir=None, original_name=False):
new_content += ',' + '\n' new_content += ',' + '\n'
# Save the modified TJA content to a new file # Save the modified TJA content to a new file
with open(new_tja_path, 'w', encoding='shift_jis') as new_file: with open(new_tja_path, 'w', encoding=encoding) as new_file:
new_file.write(new_content) new_file.write(new_content)
print(f"One measure duration (ms): {one_measure_ms}") print(f"One measure duration (ms): {one_measure_ms}")
@ -87,7 +88,8 @@ def process_tja_and_audio(tja_path, output_dir=None, original_name=False):
else: else:
print("BPM, OFFSET, or WAVE not found in the TJA file.") print("BPM, OFFSET, or WAVE not found in the TJA file.")
def process_directory(input_dir, output_dir): def process_directory(input_dir, output_dir, encoding='shift_jis'):
with ThreadPoolExecutor() as executor:
for root, _, files in os.walk(input_dir): for root, _, files in os.walk(input_dir):
for file in files: for file in files:
if file.endswith('.tja'): if file.endswith('.tja'):
@ -95,7 +97,7 @@ def process_directory(input_dir, output_dir):
relative_path = os.path.relpath(root, input_dir) relative_path = os.path.relpath(root, input_dir)
output_subdir = os.path.join(output_dir, relative_path) output_subdir = os.path.join(output_dir, relative_path)
os.makedirs(output_subdir, exist_ok=True) os.makedirs(output_subdir, exist_ok=True)
process_tja_and_audio(tja_path, output_subdir, original_name=True) executor.submit(process_tja_and_audio, tja_path, output_subdir, original_name=True, encoding=encoding)
if __name__ == "__main__": if __name__ == "__main__":
import argparse import argparse
@ -103,15 +105,16 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process TJA and OGG files.') parser = argparse.ArgumentParser(description='Process TJA and OGG files.')
parser.add_argument('--file', type=str, help='Path to a single TJA file') parser.add_argument('--file', type=str, help='Path to a single TJA file')
parser.add_argument('--path', type=str, help='Path to a directory containing folders with TJA files') parser.add_argument('--path', type=str, help='Path to a directory containing folders with TJA files')
parser.add_argument('--encoding', type=str, default='shift_jis', choices=['shift_jis', 'utf-8'], help='Encoding type (shift_jis or utf-8)')
args = parser.parse_args() args = parser.parse_args()
if args.file: if args.file:
process_tja_and_audio(args.file, original_name=True) process_tja_and_audio(args.file, original_name=True, encoding=args.encoding)
elif args.path: elif args.path:
output_dir = args.path + '_adjusted' output_dir = args.path + '_adjusted'
shutil.rmtree(output_dir, ignore_errors=True) # Remove the output directory if it exists shutil.rmtree(output_dir, ignore_errors=True) # Remove the output directory if it exists
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
process_directory(args.path, output_dir) process_directory(args.path, output_dir, encoding=args.encoding)
else: else:
print("Please specify either --file or --path.") print("Please specify either --file or --path.")