diff --git a/aux.py b/aux.py index f74f062..4ad2262 100644 --- a/aux.py +++ b/aux.py @@ -3,6 +3,8 @@ import os import pathlib +import over +prefix = over.core.text.prefix # -------------------------------------------------- @@ -27,3 +29,74 @@ def to_Path(raw_path): raw_path = os.path.expanduser(raw_path) return pathlib.Path(raw_path) + +# -------------------------------------------------- + +def _serialize(d): + """ + Transforms d into a string compatible with over config files. + """ + + if type(d) is str: + return '"' + d.replace('"', '\\"') + '"' + else: + return str(d) + +def update_cfg_context(main): + """ + All main.options that are sourced from either the cfg file or defaults get overridden by those in .over-video. + Those that are from command line get saved into .over-video. + + File format: + name=value + """ + + options = {o.name: o for o in main.options} + overrides = {} + + try: + with open(".over-video") as f: + for i, line in enumerate(f): + line = line.strip() + + if not line: + continue + + try: + name, value = line.split("=") + except ValueError: + main.print(".over-video §rsyntax error§/ on line §B{:d}§/: §y{:s}§/".format(i, line), prefix.fail) + main.print("fix the error or set §B--no-§gcontext§/") + main.exit(1) + + try: + option = options[name] + + try: + overrides[name] = over.core.app._parse(value, option.dtype) + except: + main.print(".over-video §rdata syntax error§/ on line §B{:d}§/: §y{:s}§/".format(i, line), prefix.fail) + main.print("fix the error or set §B--no-§gcontext§/") + main.exit(1) + except KeyError: + main.print(".over-video option §y{:s}§/ §rdoes not exist§/".format(name), prefix.warn) + except FileNotFoundError: + pass + + main.print("syncing context (from .over-video)", prefix.start) + + for option in options.values(): + if option.name in overrides and option.source is not "cmdline": + override = overrides[option.name] + main.print("using §B--§g{:s}§/ = §m{:s}§/".format(option.name, str(override))) + option.value = override + elif option.source is "cmdline": + overrides[option.name] = option.value + main.print("storing §B--§g{:s}§/ = §m{:s}§/".format(option.name, str(option.value))) + + with open(".over-video", "w") as f: + for name, value in overrides.items(): + line = "{:s}={:s}\n".format(name, _serialize(value)) + f.write(line) + + main.print("syncing context", prefix.done) diff --git a/over-video.py b/over-video.py index 76f9b22..0f9f4cd 100755 --- a/over-video.py +++ b/over-video.py @@ -10,7 +10,7 @@ import tempfile import time import version -from aux import parse_fps, to_Path +from aux import parse_fps, to_Path, update_cfg_context Command = over.core.cmd.Command prefix = over.core.text.prefix @@ -45,6 +45,7 @@ if __name__ == '__main__': main.add_option('video', 'str', 'x264', 'Video codec to use, either §mx265§/, §mx264§/, §mtheora§/, §mcopy§/ or §mdrop§/.', short_name='v') main.add_option('video-preset', 'str', 'slow', 'Video encoding preset, if supported by the selected encoder.', short_name='P') main.add_option('video-quality', 'float', 22, 'Video encoding quality (CRF). Use §m0§/-§m10§/ for Theora (§m0§/ being the lowest, §m5§/-§m7§/ is generally watchable) and §m0§/-§m51§/ for x264/5 (§m0§/ being lossless, §m18§/-§m28§/ is reasonable).', short_name='Q') + main.add_option('context', 'bool', True, 'Use .over-video file in CWD, if available, to remember encoding parameters per-directory.', short_name='C') main.add_option('normalize', 'bool', True, 'Normalize the audio track.', short_name='n') main.add_option('normalize-target', 'float', -20.0, 'Target mean volume to target.') main.add_option('normalize-override', 'float', 0.0, 'Volume correction to use instead of computing the required value in a (lengthy) pre-pass.', short_name='N', use_cfg_file=False) @@ -66,6 +67,9 @@ if __name__ == '__main__': video_words = [] files.container = 'mkv' + if main.cfg.context: + update_cfg_context(main) + if main.cfg.audio in ('copy', 'drop'): audio_words.append('§c%s§/' %(main.cfg.audio)) else: