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.

165 lines
5.2 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
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 python
  2. '''
  3. Parse OptionHandler object into list
  4. and call youtube_dl.main(list) using
  5. subprocess (we use this method to let
  6. youtube_dl.main() handle all the hard
  7. work)
  8. '''
  9. from .Utils import (
  10. video_is_dash,
  11. get_os_type,
  12. fix_path
  13. )
  14. LANGUAGES = {"English":"en",
  15. "Greek":"gr",
  16. "Portuguese":"pt",
  17. "French":"fr",
  18. "Italian":"it",
  19. "Russian":"ru",
  20. "Spanish":"es",
  21. "German":"de"}
  22. VIDEOFORMATS = {"highest available":"auto",
  23. "mp4 [1280x720]":"22",
  24. "mp4 [640x360]":"18",
  25. "webm [640x360]":"43",
  26. "flv [400x240]":"5",
  27. "3gp [320x240]":"36",
  28. "mp4 1080p(DASH)":"137",
  29. "mp4 720p(DASH)":"136",
  30. "mp4 480p(DASH)":"135",
  31. "mp4 360p(DASH)":"134"}
  32. DASH_AUDIO_FORMATS = {"NO SOUND":"None",
  33. "DASH m4a audio 128k":"140",
  34. "DASH webm audio 48k":"171"}
  35. class YoutubeDLInterpreter():
  36. def __init__(self, optionsList, youtubeDLFile):
  37. self.youtubeDLFile = youtubeDLFile
  38. self.optionsList = optionsList
  39. self.opts = []
  40. self.set_os()
  41. self.set_progress_opts()
  42. self.set_download_opts()
  43. self.set_connection_opts()
  44. self.set_video_opts()
  45. self.set_playlist_opts()
  46. self.set_subtitles_opts()
  47. self.set_output_opts()
  48. self.set_audio_opts()
  49. self.set_other_opts()
  50. def get_options(self):
  51. return self.opts
  52. def set_os(self):
  53. if get_os_type() == 'nt':
  54. self.opts = [self.youtubeDLFile]
  55. else:
  56. path = fix_path(self.optionsList.updatePath)
  57. self.opts = ['python', path + self.youtubeDLFile]
  58. def set_download_opts(self):
  59. if (self.optionsList.rateLimit != '0' and self.optionsList.rateLimit != ''):
  60. self.opts.append('-r')
  61. self.opts.append(self.optionsList.rateLimit)
  62. if (self.optionsList.retries != '10' and self.optionsList.retries != ''):
  63. self.opts.append('-R')
  64. self.opts.append(self.optionsList.retries)
  65. if (self.optionsList.minFileSize != '0' and self.optionsList.minFileSize != ''):
  66. self.opts.append('--min-filesize')
  67. self.opts.append(self.optionsList.minFileSize)
  68. if (self.optionsList.maxFileSize != '0' and self.optionsList.maxFileSize != ''):
  69. self.opts.append('--max-filesize')
  70. self.opts.append(self.optionsList.maxFileSize)
  71. if self.optionsList.writeDescription:
  72. self.opts.append('--write-description')
  73. if self.optionsList.writeInfo:
  74. self.opts.append('--write-info-json')
  75. if self.optionsList.writeThumbnail:
  76. self.opts.append('--write-thumbnail')
  77. def set_progress_opts(self):
  78. self.opts.append('--newline')
  79. def set_connection_opts(self):
  80. if self.optionsList.proxy != '':
  81. self.opts.append('--proxy')
  82. self.opts.append(self.optionsList.proxy)
  83. if self.optionsList.username != '':
  84. self.opts.append('--username')
  85. self.opts.append(self.optionsList.username)
  86. if self.optionsList.userAgent != '':
  87. self.opts.append('--user-agent')
  88. self.opts.append(self.optionsList.userAgent)
  89. if self.optionsList.referer != '':
  90. self.opts.append('--referer')
  91. self.opts.append(self.optionsList.referer)
  92. def set_video_opts(self):
  93. if self.optionsList.videoFormat != 'highest available':
  94. self.opts.append('-f')
  95. if video_is_dash(self.optionsList.videoFormat):
  96. vf = VIDEOFORMATS[self.optionsList.videoFormat]
  97. af = DASH_AUDIO_FORMATS[self.optionsList.dashAudioFormat]
  98. if af != 'None':
  99. self.opts.append(vf+'+'+af)
  100. else:
  101. self.opts.append(vf)
  102. else:
  103. self.opts.append(VIDEOFORMATS[self.optionsList.videoFormat])
  104. def set_playlist_opts(self):
  105. if (self.optionsList.startTrack != '1' and self.optionsList.startTrack != ''):
  106. self.opts.append('--playlist-start')
  107. self.opts.append(self.optionsList.startTrack)
  108. if (self.optionsList.endTrack != '0' and self.optionsList.endTrack != ''):
  109. self.opts.append('--playlist-end')
  110. self.opts.append(self.optionsList.endTrack)
  111. if (self.optionsList.maxDownloads != '0' and self.optionsList.maxDownloads != ''):
  112. self.opts.append('--max-downloads')
  113. self.opts.append(self.optionsList.maxDownloads)
  114. def set_subtitles_opts(self):
  115. if self.optionsList.writeAllSubs:
  116. self.opts.append('--all-subs')
  117. if (self.optionsList.writeAutoSubs):
  118. self.opts.append('--write-auto-sub')
  119. if self.optionsList.writeSubs:
  120. self.opts.append('--write-subs')
  121. if self.optionsList.subsLang != 'English':
  122. self.opts.append('--sub-lang')
  123. self.opts.append(LANGUAGES[self.optionsList.subsLang])
  124. def set_output_opts(self):
  125. path = fix_path(self.optionsList.savePath)
  126. self.opts.append('-o')
  127. if self.optionsList.idAsName:
  128. self.opts.append(path + '%(id)s.%(ext)s')
  129. else:
  130. self.opts.append(path + '%(title)s.%(ext)s')
  131. def set_audio_opts(self):
  132. if self.optionsList.toAudio:
  133. self.opts.append('-x')
  134. self.opts.append('--audio-format')
  135. self.opts.append(self.optionsList.audioFormat)
  136. if self.optionsList.audioQuality != 5:
  137. self.opts.append('--audio-quality')
  138. self.opts.append(str(self.optionsList.audioQuality))
  139. if self.optionsList.keepVideo:
  140. self.opts.append('-k')
  141. def set_other_opts(self):
  142. if self.optionsList.ignoreErrors:
  143. self.opts.append('-i')
  144. if self.optionsList.cmdArgs != '':
  145. for option in self.optionsList.cmdArgs.split():
  146. self.opts.append(option)