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.

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