cli_to_api.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Allow direct execution
  2. import os
  3. import sys
  4. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  5. import yt_dlp
  6. import yt_dlp.options
  7. create_parser = yt_dlp.options.create_parser
  8. def parse_patched_options(opts):
  9. patched_parser = create_parser()
  10. patched_parser.defaults.update({
  11. 'ignoreerrors': False,
  12. 'retries': 0,
  13. 'fragment_retries': 0,
  14. 'extract_flat': False,
  15. 'concat_playlist': 'never',
  16. })
  17. yt_dlp.options.create_parser = lambda: patched_parser
  18. try:
  19. return yt_dlp.parse_options(opts)
  20. finally:
  21. yt_dlp.options.create_parser = create_parser
  22. default_opts = parse_patched_options([]).ydl_opts
  23. def cli_to_api(opts, cli_defaults=False):
  24. opts = (yt_dlp.parse_options if cli_defaults else parse_patched_options)(opts).ydl_opts
  25. diff = {k: v for k, v in opts.items() if default_opts[k] != v}
  26. if 'postprocessors' in diff:
  27. diff['postprocessors'] = [pp for pp in diff['postprocessors']
  28. if pp not in default_opts['postprocessors']]
  29. return diff
  30. if __name__ == '__main__':
  31. from pprint import pprint
  32. print('\nThe arguments passed translate to:\n')
  33. pprint(cli_to_api(sys.argv[1:]))
  34. print('\nCombining these with the CLI defaults gives:\n')
  35. pprint(cli_to_api(sys.argv[1:], True))