txtp_segmenter: add names with regex

This commit is contained in:
bnnm 2021-10-10 11:24:03 +02:00
parent d71bbc16be
commit 472141b1a8

View File

@ -1,10 +1,6 @@
# !/usr/bin/python # !/usr/bin/python
import os import os, glob, argparse, re
import sys
import glob
import argparse
import re
def parse(): def parse():
@ -12,28 +8,34 @@ def parse():
"creates segmented .txtp from a list of files obtained using wildcards" "creates segmented .txtp from a list of files obtained using wildcards"
) )
epilog = ( epilog = (
"examples:\n" 'examples:\n'
"%(prog)s bgm_*.ogg\n" '%(prog)s bgm_*.ogg\n'
"- get all files that start with bgm_ and end with .ogg\n" '- get all files that start with bgm_ and end with .ogg\n'
"%(prog)s bgm_??.* -n bgm_main.txtp -cls 2\n" '%(prog)s bgm_??.* -n bgm_main.txtp -cla\n'
"- get all files that start with bgm_ and end with 2 chars plus any extension\n" '- get all files that start with bgm_ and end with 2 chars plus any extension + loop\n'
"%(prog)s files/bgm_*_all.ogg -s\n" '%(prog)s files/bgm_*_all.ogg -s\n'
"- create single .txtp per every bgm_(something)_all.ogg inside files dir\n" '- create single .txtp per every bgm_(something)_all.ogg inside files dir\n'
"%(prog)s **/*.ogg -l\n" '%(prog)s **/*.ogg -l\n'
"- find all .ogg in all subdirs but list only\n" '- list results only\n'
"%(prog)s files/*.ogg -f .+(a|all)[.]ogg$\n" '%(prog)s files/*.ogg -fi .+(00[01])[.]ogg$\n'
"- find all .ogg in files except those that end with 'a.ogg' or 'all.ogg'\n" '- find all .ogg in files including those that end with 0.ogg or 1.ogg\n'
"%(prog)s files/*.ogg -f .+(00[01])[.]ogg$\n" '%(prog)s files/*.ogg -fe .+(a|all)[.]ogg$\n'
"- find all .ogg in files that end with '0.ogg' or '1.ogg'\n" '- find all .ogg in files excluding those that end with a.ogg or all.ogg\n'
'%(prog)s files/*.* -fi "(.+)(_intro|_loop)([.].+)$" -n "\\1)_full" -cfa\n'
'- makes intro+loop .txtp named (first part without _intro/_loop)_full.txtp + loops\n'
'%(prog)s files/*.* -fe "(.+)(_intro|_loop)([.].+)$" -s\n'
'- makes single .txtp for files that don\'t have intro+loop pairs\n'
) )
parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter) parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("files", help="files to match") parser.add_argument("files", help="files to match")
parser.add_argument("-n","--name", help="generated txtp name (adapts 'files' by default)") parser.add_argument("-n","--name", help="generated txtp name (auto from 'files' by default)\nMay use regex groups like '\\1.ogg' when used with filter-include")
parser.add_argument("-f","--filter", help="filter matched files with regex and keep rest") parser.add_argument("-fi","--filter-include", help="include files matched with regex and ignore rest")
parser.add_argument("-i","--include", help="include matched files with regex and ignore rest") parser.add_argument("-fe","--filter-exclude", help="exclude files matched with regex and keep rest")
parser.add_argument("-s","--single", help="generate single files per list match", action='store_true') parser.add_argument("-s","--single", help="generate single files per list match", action='store_true')
parser.add_argument("-l","--list", help="list only results and don't write", action='store_true') parser.add_argument("-l","--list", help="list only results and don't write .txtp", action='store_true')
parser.add_argument("-cla","--command-loop-auto", help="sets auto-loop (last segment)", action='store_true')
parser.add_argument("-clf","--command-loop-force", help="sets auto-loop (last segment) even with 1 segment", action='store_true')
parser.add_argument("-cls","--command-loop-start", help="sets loop start segment") parser.add_argument("-cls","--command-loop-start", help="sets loop start segment")
parser.add_argument("-cle","--command-loop-end", help="sets loop end segment") parser.add_argument("-cle","--command-loop-end", help="sets loop end segment")
parser.add_argument("-cv","--command-volume", help="sets volume") parser.add_argument("-cv","--command-volume", help="sets volume")
@ -42,35 +44,36 @@ def parse():
return parser.parse_args() return parser.parse_args()
def is_file_ok(args, glob_file): def is_file_ok(args, file):
if not os.path.isfile(glob_file): if not os.path.isfile(file):
return False return False
if glob_file.endswith(".py"): if file.endswith(".py"):
return False return False
if args.filter: file_test = os.path.basename(file)
filename_test = os.path.basename(glob_file) if args.filter_exclude:
p = re.compile(args.filter) if args.p_exclude.match(file_test) != None:
if p.match(filename_test) != None:
return False return False
if args.include: if args.filter_include:
filename_test = os.path.basename(glob_file) if args.p_include.match(file_test) == None:
p = re.compile(args.include)
if p.match(filename_test) == None:
return False return False
return True return True
def get_txtp_name(args, segment): def get_txtp_name(args, file):
txtp_name = '' txtp_name = ''
if args.name: if args.filter_include and args.name and '\\' in args.name:
file_test = os.path.basename(file)
txtp_name = args.p_include.sub(args.name, file_test)
elif args.name:
txtp_name = args.name txtp_name = args.name
elif args.single: elif args.single:
txtp_name = os.path.splitext(os.path.basename(segment))[0] txtp_name = os.path.splitext(os.path.basename(file))[0]
else: else:
txtp_name = os.path.splitext(os.path.basename(args.files))[0] txtp_name = os.path.splitext(os.path.basename(args.files))[0]
@ -80,45 +83,45 @@ def get_txtp_name(args, segment):
if txtp_name.endswith('_'): if txtp_name.endswith('_'):
txtp_name = txtp_name[:-1] txtp_name = txtp_name[:-1]
if txtp_name == '':
if not txtp_name:
txtp_name = 'bgm' txtp_name = 'bgm'
if not txtp_name.endswith(".txtp"): if not txtp_name.endswith(".txtp"):
txtp_name += ".txtp" txtp_name += ".txtp"
return txtp_name return txtp_name
def main(): def main():
args = parse() args = parse()
if args.filter_include:
args.p_include = re.compile(args.filter_include)
if args.filter_exclude:
args.p_exclude = re.compile(args.filter_exclude)
# get target files # get target files
glob_files = glob.glob(args.files) files = glob.glob(args.files)
# process matches and add to output list # process matches and add to output list
files = [] txtps = {}
segments = [] for file in files:
for glob_file in glob_files: if not is_file_ok(args, file):
if not is_file_ok(args, glob_file):
continue continue
if args.single: name = get_txtp_name(args, file)
name = get_txtp_name(args, glob_file) if not name in txtps:
segments = [glob_file] txtps[name] = []
files.append( (name,segments) ) txtps[name].append(file)
else:
segments.append(glob_file)
if not args.single: if not txtps:
name = get_txtp_name(args, '')
files.append( (name,segments) )
if not files or not segments:
print("no files found") print("no files found")
exit() exit()
# list info # list info
for name, segments in files: for name, segments in txtps.items():
print("file: " + name) print("file: " + name)
for segment in segments: for segment in segments:
print(" " + segment) print(" " + segment)
@ -127,10 +130,17 @@ def main():
exit() exit()
# write resulting files # write resulting files
for name, segments in files: for name, segments in txtps.items():
len_segments = len(segments)
with open(name,"w+") as ftxtp: with open(name,"w+") as ftxtp:
for segment in segments: for i, segment in enumerate(segments):
if args.command_loop_force and len_segments == 1:
ftxtp.write(segment + " #e\n")
else:
ftxtp.write(segment + "\n") ftxtp.write(segment + "\n")
if args.command_loop_auto or args.command_loop_force and len_segments > 1:
ftxtp.write("loop_mode = auto\n")
if args.command_loop_start: if args.command_loop_start:
ftxtp.write("loop_start_segment = " + args.command_loop_start + "\n") ftxtp.write("loop_start_segment = " + args.command_loop_start + "\n")
if args.command_loop_end: if args.command_loop_end: