You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. ''' Parse OptionsManager.options. '''
  3. import os.path
  4. from .utils import remove_shortcuts
  5. SUBS_LANG = {
  6. "English": "en",
  7. "Greek": "gr",
  8. "Portuguese": "pt",
  9. "French": "fr",
  10. "Italian": "it",
  11. "Russian": "ru",
  12. "Spanish": "es",
  13. "German": "de"
  14. }
  15. VIDEO_FORMATS = {
  16. "default": "0",
  17. "none": "0",
  18. "3gp [176x144]": "17",
  19. "3gp [320x240]": "36",
  20. "flv [400x240]": "5",
  21. "flv [640x360]": "34",
  22. "flv [854x480]": "35",
  23. "webm [640x360]": "43",
  24. "webm [854x480]": "44",
  25. "webm [1280x720]": "45",
  26. "webm [1920x1080]": "46",
  27. "mp4 [640x360]": "18",
  28. "mp4 [1280x720]": "22",
  29. "mp4 [1920x1080]": "37",
  30. "mp4 [4096x3072]": "38",
  31. "mp4 144p (DASH)": "160",
  32. "mp4 240p (DASH)": "133",
  33. "mp4 360p (DASH)": "134",
  34. "mp4 480p (DASH)": "135",
  35. "mp4 720p (DASH)": "136",
  36. "mp4 1080p (DASH)": "137",
  37. "mp4 1440p (DASH)": "264",
  38. "mp4 2160p (DASH)": "138",
  39. "webm 240p (DASH)": "242",
  40. "webm 360p (DASH)": "243",
  41. "webm 480p (DASH)": "244",
  42. "webm 720p (DASH)": "247",
  43. "webm 1080p (DASH)": "248",
  44. "webm 1440p (DASH)": "271",
  45. "webm 2160p (DASH)": "272",
  46. "mp4 360p (3D)": "82",
  47. "mp4 480p (3D)": "83",
  48. "mp4 720p (3D)": "84",
  49. "mp4 1080p (3D)": "85",
  50. "webm 360p (3D)": "100",
  51. "webm 480p (3D)": "101",
  52. "webm 720p (3D)": "102",
  53. "m4a 48k (DASH AUDIO)": "139",
  54. "m4a 128k (DASH AUDIO)": "140",
  55. "m4a 256k (DASH AUDIO)": "141",
  56. "webm 48k (DASH AUDIO)": "171",
  57. "webm 256k (DASH AUDIO)": "172"
  58. }
  59. AUDIO_QUALITY = {
  60. "high": "0",
  61. "mid": "5",
  62. "low": "9"
  63. }
  64. FILESIZE_UNITS = {
  65. 'Bytes': '',
  66. 'Kilobytes': 'k',
  67. 'Megabytes': 'm',
  68. 'Gigabytes': 'g',
  69. 'Terabytes': 't',
  70. 'Petabytes': 'p',
  71. 'Exabytes': 'e',
  72. 'Zettabytes': 'z',
  73. 'Yottabytes': 'y'
  74. }
  75. class OptionHolder():
  76. def __init__(self, name, flag, default_value, requirements=[]):
  77. self.name = name
  78. self.flag = flag
  79. self.requirements = requirements
  80. self.default_value = default_value
  81. def is_boolean(self):
  82. return type(self.default_value) is bool
  83. def check_requirements(self, options_dict):
  84. if not self.requirements:
  85. return True
  86. return any(map(lambda x: options_dict[x], self.requirements))
  87. class OptionsParser():
  88. '''
  89. OUT_OF_DATE
  90. Parse OptionsManager.options into youtube-dl options list.
  91. Params
  92. opt_manager: OptionsManager.OptionsManager object
  93. Accessible Methods
  94. parse()
  95. Params: None
  96. Return: Options list
  97. '''
  98. def __init__(self):
  99. self._ydl_options = [
  100. OptionHolder('playlist_start', '--playlist-start', 1),
  101. OptionHolder('playlist_end', '--playlist-end', 0),
  102. OptionHolder('max_downloads', '--max-downloads', 0),
  103. OptionHolder('username', '-u', ''),
  104. OptionHolder('password', '-p', ''),
  105. OptionHolder('video_password', '--video-password', ''),
  106. OptionHolder('retries', '-R', 10),
  107. OptionHolder('proxy', '--proxy', ''),
  108. OptionHolder('user_agent', '--user-agent', ''),
  109. OptionHolder('referer', '--referer', ''),
  110. OptionHolder('ignore_errors', '-i', False),
  111. OptionHolder('write_description', '--write-description', False),
  112. OptionHolder('write_info', '--write-info-json', False),
  113. OptionHolder('write_thumbnail', '--write-thumbnail', False),
  114. OptionHolder('min_filesize', '--min-filesize', 0),
  115. OptionHolder('max_filesize', '--max-filesize', 0),
  116. OptionHolder('write_all_subs', '--all-subs', False),
  117. OptionHolder('write_auto_subs', '--write-auto-sub', False),
  118. OptionHolder('write_subs', '--write-sub', False),
  119. OptionHolder('keep_video', '-k', False),
  120. OptionHolder('restrict_filenames', '--restrict-filenames', False),
  121. OptionHolder('save_path', '-o', ''),
  122. OptionHolder('embed_subs', '--embed-subs', False, ['write_auto_subs', 'write_subs']),
  123. OptionHolder('to_audio', '-x', False),
  124. OptionHolder('audio_format', '--audio-format', '', ['to_audio']),
  125. OptionHolder('video_format', '-f', ''),
  126. OptionHolder('subs_lang', '--sub-lang', '', ['write_subs']),
  127. OptionHolder('audio_quality', '--audio-quality', '5', ['to_audio'])
  128. ]
  129. def parse(self, options_dictionary):
  130. ''' Parse OptionsHandler.options and return options list. '''
  131. options_list = ['--newline']
  132. # Create a copy of options_dictionary
  133. # We don't want to edit the original options dictionary
  134. # and change some of the options values like 'save_path' etc..
  135. options_dict = options_dictionary.copy()
  136. self._build_savepath(options_dict)
  137. self._build_subslang(options_dict)
  138. self._build_videoformat(options_dict)
  139. self._build_audioquality(options_dict)
  140. self._build_filesizes(options_dict)
  141. # Parse basic youtube-dl command line options
  142. for option in self._ydl_options:
  143. if option.check_requirements(options_dict):
  144. value = options_dict[option.name]
  145. if value != option.default_value:
  146. options_list.append(option.flag)
  147. if not option.is_boolean():
  148. options_list.append(str(value))
  149. # Parse cmd_args
  150. for option in options_dict['cmd_args'].split():
  151. options_list.append(option)
  152. return options_list
  153. def _build_savepath(self, options_dict):
  154. save_path = remove_shortcuts(options_dict['save_path'])
  155. if options_dict['output_format'] == 'id':
  156. save_path = os.path.join(save_path, '%(id)s.%(ext)s')
  157. elif options_dict['output_format'] == 'title':
  158. save_path = os.path.join(save_path, '%(title)s.%(ext)s')
  159. else:
  160. save_path = os.path.join(save_path, options_dict['output_template'])
  161. options_dict['save_path'] = save_path
  162. def _build_videoformat(self, options_dict):
  163. first_vf = VIDEO_FORMATS[options_dict['video_format']]
  164. second_vf = VIDEO_FORMATS[options_dict['second_video_format']]
  165. if first_vf != '0' and second_vf != '0':
  166. options_dict['video_format'] = first_vf + '+' + second_vf
  167. elif first_vf != '0' and second_vf == '0':
  168. options_dict['video_format'] = first_vf
  169. else:
  170. options_dict['video_format'] = ''
  171. def _build_subslang(self, options_dict):
  172. options_dict['subs_lang'] = SUBS_LANG[options_dict['subs_lang']]
  173. def _build_audioquality(self, options_dict):
  174. options_dict['audio_quality'] = AUDIO_QUALITY[options_dict['audio_quality']]
  175. def _build_filesizes(self, options_dict):
  176. if options_dict['min_filesize']:
  177. size_unit = FILESIZE_UNITS[options_dict['min_filesize_unit']]
  178. options_dict['min_filesize'] = str(options_dict['min_filesize']) + size_unit
  179. if options_dict['max_filesize']:
  180. size_unit = FILESIZE_UNITS[options_dict['max_filesize_unit']]
  181. options_dict['max_filesize'] = str(options_dict['max_filesize']) + size_unit