refactor: persist stream selection in .stream_state instead of config.toml

This commit is contained in:
Honney OC committed 2026-09-14 02:09:02 +00:00
1 parent 8e3db0d7f4
commit 30015d5cad
3 files changed
+34 -32

No files matched your search

+18 -28
View File
@@ -493,33 +493,23 @@ for _file_idx, INPUT_FILE in enumerate(INPUT_FILES):
print(f"-" * 50)
print(f"All done. Files are in:", OUT_DIR)
# ─── Save selected audio stream indices to config.toml ───
if len(_audio_stream_selections) >= 2 and CONFIG_PATH and os.path.exists(CONFIG_PATH):
# ─── Save selected audio stream indices to .stream_state (gitignored) ───
if len(_audio_stream_selections) >= 2:
try:
with open(CONFIG_PATH, "r") as f:
cfg_text = f.read()
# Update or add audio_stream_a and audio_stream_b in [extract] section
import re
def _cfg_set(key: str, val) -> str:
nonlocal cfg_text
pat = re.compile(rf'(^{key}\s*=\s*).*$')
replacement = f'{key} = {val}'
if pat.search(cfg_text):
cfg_text = pat.sub(replacement, cfg_text, count=1)
else:
# Insert before the next section header or at end of [extract]
ins_pat = re.compile(r'(\[audio\])')
if ins_pat.search(cfg_text):
cfg_text = ins_pat.sub(f'{replacement}\n\n\\1', cfg_text, count=1)
else:
cfg_text += f"\n{replacement}\n"
_cfg_set("audio_stream_a", _audio_stream_selections[0]
if _audio_stream_selections[0] is not None else "null")
_cfg_set("audio_stream_b", _audio_stream_selections[1]
if _audio_stream_selections[1] is not None else "null")
with open(CONFIG_PATH, "w") as f:
f.write(cfg_text)
print(f"[config] saved audio_stream_a={_audio_stream_selections[0]}, "
f"audio_stream_b={_audio_stream_selections[1]} → {CONFIG_PATH}")
import json
state_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".stream_state")
state = {}
if os.path.exists(state_path):
with open(state_path, "r") as f:
state = json.load(f)
# Record which files these selections belong to
state["file_a"] = INPUT_FILES[0]
state["file_b"] = INPUT_FILES[1]
for i, sel in enumerate(_audio_stream_selections):
key = f"audio_stream_{chr(ord('a') + i)}"
state[key] = sel if sel is not None else None
with open(state_path, "w") as f:
json.dump(state, f, indent=2)
print(f"[state] saved stream selection → {state_path}")
except Exception as e:
print(f"[config] warn: could not save stream indices: {e}", file=sys.stderr)
print(f"[state] warn: could not save stream indices: {e}", file=sys.stderr)