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.

175 lines
5.9 KiB

  1. #! /usr/bin/env python
  2. from .Utils import (
  3. video_is_dash,
  4. fix_path
  5. )
  6. SUBS_LANG = {
  7. "English": "en",
  8. "Greek": "gr",
  9. "Portuguese": "pt",
  10. "French": "fr",
  11. "Italian": "it",
  12. "Russian": "ru",
  13. "Spanish": "es",
  14. "German": "de"
  15. }
  16. VIDEO_FORMATS = {
  17. "default": "0",
  18. "mp4 [1280x720]": "22",
  19. "mp4 [640x360]": "18",
  20. "webm [640x360]": "43",
  21. "flv [400x240]": "5",
  22. "3gp [320x240]": "36",
  23. "mp4 1080p(DASH)": "137",
  24. "mp4 720p(DASH)": "136",
  25. "mp4 480p(DASH)": "135",
  26. "mp4 360p(DASH)": "134"
  27. }
  28. DASH_AUDIO_FORMATS = {
  29. "none": "none",
  30. "DASH m4a audio 128k": "140",
  31. "DASH webm audio 48k": "171"
  32. }
  33. AUDIO_QUALITY = {
  34. "high": "0",
  35. "mid": "5",
  36. "low": "9"
  37. }
  38. class OptionsParser():
  39. ''' Parse OptionsHandler object into youtube-dl options list '''
  40. def __init__(self, opt_manager):
  41. self._options = opt_manager.options
  42. self.opts = []
  43. def parse(self):
  44. self._set_progress_opts()
  45. self._set_output_opts()
  46. self._set_auth_opts()
  47. self._set_connection_opts()
  48. self._set_video_opts()
  49. self._set_playlist_opts()
  50. self._set_filesystem_opts()
  51. self._set_subtitles_opts()
  52. self._set_audio_opts()
  53. self._set_other_opts()
  54. return self.opts
  55. def _set_progress_opts(self):
  56. ''' Do NOT change this option '''
  57. self.opts.append('--newline')
  58. def _set_playlist_opts(self):
  59. if self._options['playlist_start'] != 1:
  60. self.opts.append('--playlist-start')
  61. self.opts.append(str(self._options['playlist_start']))
  62. if self._options['playlist_end'] != 0:
  63. self.opts.append('--playlist-end')
  64. self.opts.append(str(self._options['playlist_end']))
  65. if self._options['max_downloads'] != 0:
  66. self.opts.append('--max-downloads')
  67. self.opts.append(str(self._options['max_downloads']))
  68. if self._options['min_filesize'] != '0':
  69. self.opts.append('--min-filesize')
  70. self.opts.append(self._options['min_filesize'])
  71. if self._options['max_filesize'] != '0':
  72. self.opts.append('--max-filesize')
  73. self.opts.append(self._options['max_filesize'])
  74. def _set_auth_opts(self):
  75. if self._options['username'] != '':
  76. self.opts.append('-u')
  77. self.opts.append(self._options['username'])
  78. if self._options['password'] != '':
  79. self.opts.append('-p')
  80. self.opts.append(self._options['password'])
  81. if self._options['video_password'] != '':
  82. self.opts.append('--video-password')
  83. self.opts.append(self._options['video_password'])
  84. def _set_connection_opts(self):
  85. if self._options['retries'] != 10:
  86. self.opts.append('-R')
  87. self.opts.append(str(self._options['retries']))
  88. if self._options['proxy'] != '':
  89. self.opts.append('--proxy')
  90. self.opts.append(self._options['proxy'])
  91. if self._options['user_agent'] != '':
  92. self.opts.append('--user-agent')
  93. self.opts.append(self._options['user_agent'])
  94. if self._options['referer'] != '':
  95. self.opts.append('--referer')
  96. self.opts.append(self._options['referer'])
  97. def _set_video_opts(self):
  98. if self._options['video_format'] != 'default':
  99. self.opts.append('-f')
  100. if video_is_dash(self._options['video_format']):
  101. vf = VIDEO_FORMATS[self._options['video_format']]
  102. af = DASH_AUDIO_FORMATS[self._options['dash_audio_format']]
  103. if af != 'none':
  104. self.opts.append(vf + '+' + af)
  105. else:
  106. self.opts.append(vf)
  107. else:
  108. self.opts.append(VIDEO_FORMATS[self._options['video_format']])
  109. def _set_filesystem_opts(self):
  110. if self._options['ignore_errors']:
  111. self.opts.append('-i')
  112. if self._options['write_description']:
  113. self.opts.append('--write-description')
  114. if self._options['write_info']:
  115. self.opts.append('--write-info-json')
  116. if self._options['write_thumbnail']:
  117. self.opts.append('--write-thumbnail')
  118. def _set_subtitles_opts(self):
  119. if self._options['write_all_subs']:
  120. self.opts.append('--all-subs')
  121. if self._options['write_auto_subs']:
  122. self.opts.append('--write-auto-sub')
  123. if self._options['write_subs']:
  124. self.opts.append('--write-sub')
  125. if self._options['subs_lang'] != 'English':
  126. self.opts.append('--sub-lang')
  127. self.opts.append(SUBS_LANG[self._options['subs_lang']])
  128. if self._options['embed_subs']:
  129. self.opts.append('--embed-subs')
  130. def _set_output_opts(self):
  131. path = fix_path(self._options['save_path'])
  132. self.opts.append('-o')
  133. if self._options['output_format'] == 'id':
  134. self.opts.append(path + '%(id)s.%(ext)s')
  135. elif self._options['output_format'] == 'title':
  136. self.opts.append(path + '%(title)s.%(ext)s')
  137. elif self._options['output_format'] == 'custom':
  138. self.opts.append(path + self._options['output_template'])
  139. if self._options['restrict_filenames']:
  140. self.opts.append('--restrict-filenames')
  141. def _set_audio_opts(self):
  142. if self._options['to_audio']:
  143. self.opts.append('-x')
  144. self.opts.append('--audio-format')
  145. self.opts.append(self._options['audio_format'])
  146. if self._options['audio_quality'] != 'mid':
  147. self.opts.append('--audio-quality')
  148. self.opts.append(AUDIO_QUALITY[self._options['audio_quality']])
  149. if self._options['keep_video']:
  150. self.opts.append('-k')
  151. def _set_other_opts(self):
  152. if self._options['cmd_args'] != '':
  153. for option in self._options['cmd_args'].split():
  154. self.opts.append(option)