settings.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import logging
  2. import os
  3. import sys
  4. from lvc import execute
  5. ffmpeg_version = None
  6. _search_path_extra = []
  7. def add_to_search_path(directory):
  8. """Add a path to the list of paths that which() searches."""
  9. _search_path_extra.append(directory)
  10. def which(name):
  11. if sys.platform == 'win32':
  12. name = name + '.exe' # we're looking for ffmpeg.exe in this case
  13. if sys.platform == 'darwin' and 'Contents/Resources' in __file__:
  14. # look for a bundled version
  15. path = os.path.join(os.path.dirname(__file__),
  16. '..', '..', '..', '..', 'Helpers', name)
  17. if os.path.exists(path):
  18. return path
  19. dirs_to_search = os.environ['PATH'].split(os.pathsep)
  20. dirs_to_search += _search_path_extra
  21. for dirname in dirs_to_search:
  22. fullpath = os.path.join(dirname, name)
  23. # XXX check for +x bit
  24. if os.path.exists(fullpath):
  25. return fullpath
  26. logging.warn("Can't find path to %s (searched in %s)", name,
  27. dirs_to_search)
  28. def memoize(func):
  29. cache = []
  30. def wrapper():
  31. if not cache:
  32. cache.append(func())
  33. return cache[0]
  34. return wrapper
  35. @memoize
  36. def get_ffmpeg_executable_path():
  37. return which("ffmpeg")
  38. avconv = which('avconv')
  39. if avconv is not None:
  40. return avconv
  41. return which("ffmpeg")
  42. def get_ffmpeg_version():
  43. global ffmpeg_version
  44. if ffmpeg_version is None:
  45. commandline = [get_ffmpeg_executable_path(), '-version']
  46. p = execute.Popen(commandline, stderr=open(os.devnull, "wb"))
  47. stdout, _ = p.communicate()
  48. lines = stdout.split('\n')
  49. version = lines[0].rsplit(' ', 1)[1].split('.')
  50. def maybe_int(v):
  51. try:
  52. return int(v)
  53. except ValueError:
  54. return v
  55. ffmpeg_version = tuple(maybe_int(v) for v in version)
  56. return ffmpeg_version
  57. def customize_ffmpeg_parameters(params):
  58. """Takes a list of parameters and modifies it based on
  59. platform-specific issues. Returns the newly modified list of
  60. parameters.
  61. :param params: list of parameters to modify
  62. :returns: list of modified parameters that will get passed to
  63. ffmpeg
  64. """
  65. if get_ffmpeg_version() < (0, 8):
  66. # Fallback for older versions of FFmpeg (Ubuntu Natty, in particular).
  67. # see also #18969
  68. params = ['-vpre' if i == '-preset' else i for i in params]
  69. try:
  70. profile_index = params.index('-profile:v')
  71. except ValueError:
  72. pass
  73. else:
  74. if params[profile_index + 1] == 'baseline':
  75. params[profile_index:profile_index+2] = [
  76. '-coder', '0', '-bf', '0', '-refs', '1',
  77. '-flags2', '-wpred-dct8x8']
  78. return params