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.

437 lines
16 KiB

13 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. __authors__ = (
  4. 'Ricardo Garcia Gonzalez',
  5. 'Danny Colligan',
  6. 'Benjamin Johnson',
  7. 'Vasyl\' Vavrychuk',
  8. 'Witold Baryluk',
  9. 'Paweł Paprota',
  10. 'Gergely Imreh',
  11. 'Rogério Brito',
  12. 'Philipp Hagemeister',
  13. 'Sören Schulze',
  14. 'Kevin Ngo',
  15. 'Ori Avtalion',
  16. 'shizeeg',
  17. 'Filippo Valsorda',
  18. 'Christian Albrecht',
  19. 'Dave Vasilevsky',
  20. 'Jaime Marquínez Ferrándiz',
  21. 'Jeff Crouse',
  22. 'Osama Khalid',
  23. 'Michael Walter',
  24. 'M. Yasoob Ullah Khalid',
  25. 'Julien Fraichard',
  26. 'Johny Mo Swag',
  27. 'Axel Noack',
  28. 'Albert Kim',
  29. 'Pierre Rudloff',
  30. 'Huarong Huo',
  31. 'Ismael Mejía',
  32. 'Steffan \'Ruirize\' James',
  33. 'Andras Elso',
  34. 'Jelle van der Waa',
  35. 'Marcin Cieślak',
  36. 'Anton Larionov',
  37. 'Takuya Tsuchida',
  38. 'Sergey M.',
  39. 'Michael Orlitzky',
  40. 'Chris Gahan',
  41. 'Saimadhav Heblikar',
  42. 'Mike Col',
  43. 'Oleg Prutz',
  44. 'pulpe',
  45. 'Andreas Schmitz',
  46. 'Michael Kaiser',
  47. 'Niklas Laxström',
  48. 'David Triendl',
  49. 'Anthony Weems',
  50. 'David Wagner',
  51. 'Juan C. Olivares',
  52. 'Mattias Harrysson',
  53. 'phaer',
  54. 'Sainyam Kapoor',
  55. 'Nicolas Évrard',
  56. 'Jason Normore',
  57. 'Hoje Lee',
  58. 'Adam Thalhammer',
  59. 'Georg Jähnig',
  60. 'Ralf Haring',
  61. 'Koki Takahashi',
  62. 'Ariset Llerena',
  63. 'Adam Malcontenti-Wilson',
  64. 'Tobias Bell',
  65. 'Naglis Jonaitis',
  66. 'Charles Chen',
  67. 'Hassaan Ali',
  68. 'Dobrosław Żybort',
  69. 'David Fabijan',
  70. 'Sebastian Haas',
  71. 'Alexander Kirk',
  72. 'Erik Johnson',
  73. 'Keith Beckman',
  74. 'Ole Ernst',
  75. 'Aaron McDaniel (mcd1992)',
  76. 'Magnus Kolstad',
  77. 'Hari Padmanaban',
  78. 'Carlos Ramos',
  79. '5moufl',
  80. 'lenaten',
  81. )
  82. __license__ = 'Public Domain'
  83. import codecs
  84. import io
  85. import os
  86. import random
  87. import sys
  88. from .options import (
  89. parseOpts,
  90. )
  91. from .utils import (
  92. compat_getpass,
  93. compat_print,
  94. DateRange,
  95. DEFAULT_OUTTMPL,
  96. decodeOption,
  97. DownloadError,
  98. MaxDownloadsReached,
  99. preferredencoding,
  100. read_batch_urls,
  101. SameFileError,
  102. setproctitle,
  103. std_headers,
  104. write_string,
  105. )
  106. from .update import update_self
  107. from .downloader import (
  108. FileDownloader,
  109. )
  110. from .extractor import gen_extractors
  111. from .YoutubeDL import YoutubeDL
  112. from .postprocessor import (
  113. AtomicParsleyPP,
  114. FFmpegAudioFixPP,
  115. FFmpegMetadataPP,
  116. FFmpegVideoConvertor,
  117. FFmpegExtractAudioPP,
  118. FFmpegEmbedSubtitlePP,
  119. XAttrMetadataPP,
  120. ExecAfterDownloadPP,
  121. )
  122. def _real_main(argv=None):
  123. # Compatibility fixes for Windows
  124. if sys.platform == 'win32':
  125. # https://github.com/rg3/youtube-dl/issues/820
  126. codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
  127. setproctitle(u'youtube-dl')
  128. parser, opts, args = parseOpts(argv)
  129. # Set user agent
  130. if opts.user_agent is not None:
  131. std_headers['User-Agent'] = opts.user_agent
  132. # Set referer
  133. if opts.referer is not None:
  134. std_headers['Referer'] = opts.referer
  135. # Custom HTTP headers
  136. if opts.headers is not None:
  137. for h in opts.headers:
  138. if h.find(':', 1) < 0:
  139. parser.error(u'wrong header formatting, it should be key:value, not "%s"'%h)
  140. key, value = h.split(':', 2)
  141. if opts.verbose:
  142. write_string(u'[debug] Adding header from command line option %s:%s\n'%(key, value))
  143. std_headers[key] = value
  144. # Dump user agent
  145. if opts.dump_user_agent:
  146. compat_print(std_headers['User-Agent'])
  147. sys.exit(0)
  148. # Batch file verification
  149. batch_urls = []
  150. if opts.batchfile is not None:
  151. try:
  152. if opts.batchfile == '-':
  153. batchfd = sys.stdin
  154. else:
  155. batchfd = io.open(opts.batchfile, 'r', encoding='utf-8', errors='ignore')
  156. batch_urls = read_batch_urls(batchfd)
  157. if opts.verbose:
  158. write_string(u'[debug] Batch file urls: ' + repr(batch_urls) + u'\n')
  159. except IOError:
  160. sys.exit(u'ERROR: batch file could not be read')
  161. all_urls = batch_urls + args
  162. all_urls = [url.strip() for url in all_urls]
  163. _enc = preferredencoding()
  164. all_urls = [url.decode(_enc, 'ignore') if isinstance(url, bytes) else url for url in all_urls]
  165. extractors = gen_extractors()
  166. if opts.list_extractors:
  167. for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
  168. compat_print(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie._WORKING else ''))
  169. matchedUrls = [url for url in all_urls if ie.suitable(url)]
  170. for mu in matchedUrls:
  171. compat_print(u' ' + mu)
  172. sys.exit(0)
  173. if opts.list_extractor_descriptions:
  174. for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
  175. if not ie._WORKING:
  176. continue
  177. desc = getattr(ie, 'IE_DESC', ie.IE_NAME)
  178. if desc is False:
  179. continue
  180. if hasattr(ie, 'SEARCH_KEY'):
  181. _SEARCHES = (u'cute kittens', u'slithering pythons', u'falling cat', u'angry poodle', u'purple fish', u'running tortoise', u'sleeping bunny')
  182. _COUNTS = (u'', u'5', u'10', u'all')
  183. desc += u' (Example: "%s%s:%s" )' % (ie.SEARCH_KEY, random.choice(_COUNTS), random.choice(_SEARCHES))
  184. compat_print(desc)
  185. sys.exit(0)
  186. # Conflicting, missing and erroneous options
  187. if opts.usenetrc and (opts.username is not None or opts.password is not None):
  188. parser.error(u'using .netrc conflicts with giving username/password')
  189. if opts.password is not None and opts.username is None:
  190. parser.error(u'account username missing\n')
  191. if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
  192. parser.error(u'using output template conflicts with using title, video ID or auto number')
  193. if opts.usetitle and opts.useid:
  194. parser.error(u'using title conflicts with using video ID')
  195. if opts.username is not None and opts.password is None:
  196. opts.password = compat_getpass(u'Type account password and press [Return]: ')
  197. if opts.ratelimit is not None:
  198. numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
  199. if numeric_limit is None:
  200. parser.error(u'invalid rate limit specified')
  201. opts.ratelimit = numeric_limit
  202. if opts.min_filesize is not None:
  203. numeric_limit = FileDownloader.parse_bytes(opts.min_filesize)
  204. if numeric_limit is None:
  205. parser.error(u'invalid min_filesize specified')
  206. opts.min_filesize = numeric_limit
  207. if opts.max_filesize is not None:
  208. numeric_limit = FileDownloader.parse_bytes(opts.max_filesize)
  209. if numeric_limit is None:
  210. parser.error(u'invalid max_filesize specified')
  211. opts.max_filesize = numeric_limit
  212. if opts.retries is not None:
  213. try:
  214. opts.retries = int(opts.retries)
  215. except (TypeError, ValueError):
  216. parser.error(u'invalid retry count specified')
  217. if opts.buffersize is not None:
  218. numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
  219. if numeric_buffersize is None:
  220. parser.error(u'invalid buffer size specified')
  221. opts.buffersize = numeric_buffersize
  222. if opts.playliststart <= 0:
  223. raise ValueError(u'Playlist start must be positive')
  224. if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart:
  225. raise ValueError(u'Playlist end must be greater than playlist start')
  226. if opts.extractaudio:
  227. if opts.audioformat not in ['best', 'aac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']:
  228. parser.error(u'invalid audio format specified')
  229. if opts.audioquality:
  230. opts.audioquality = opts.audioquality.strip('k').strip('K')
  231. if not opts.audioquality.isdigit():
  232. parser.error(u'invalid audio quality specified')
  233. if opts.recodevideo is not None:
  234. if opts.recodevideo not in ['mp4', 'flv', 'webm', 'ogg', 'mkv']:
  235. parser.error(u'invalid video recode format specified')
  236. if opts.date is not None:
  237. date = DateRange.day(opts.date)
  238. else:
  239. date = DateRange(opts.dateafter, opts.datebefore)
  240. if opts.default_search not in ('auto', 'auto_warning', 'error', 'fixup_error', None) and ':' not in opts.default_search:
  241. parser.error(u'--default-search invalid; did you forget a colon (:) at the end?')
  242. # Do not download videos when there are audio-only formats
  243. if opts.extractaudio and not opts.keepvideo and opts.format is None:
  244. opts.format = 'bestaudio/best'
  245. # --all-sub automatically sets --write-sub if --write-auto-sub is not given
  246. # this was the old behaviour if only --all-sub was given.
  247. if opts.allsubtitles and (opts.writeautomaticsub == False):
  248. opts.writesubtitles = True
  249. if sys.version_info < (3,):
  250. # In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
  251. if opts.outtmpl is not None:
  252. opts.outtmpl = opts.outtmpl.decode(preferredencoding())
  253. outtmpl =((opts.outtmpl is not None and opts.outtmpl)
  254. or (opts.format == '-1' and opts.usetitle and u'%(title)s-%(id)s-%(format)s.%(ext)s')
  255. or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
  256. or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
  257. or (opts.usetitle and u'%(title)s-%(id)s.%(ext)s')
  258. or (opts.useid and u'%(id)s.%(ext)s')
  259. or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
  260. or DEFAULT_OUTTMPL)
  261. if not os.path.splitext(outtmpl)[1] and opts.extractaudio:
  262. parser.error(u'Cannot download a video and extract audio into the same'
  263. u' file! Use "{0}.%(ext)s" instead of "{0}" as the output'
  264. u' template'.format(outtmpl))
  265. any_printing = opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson
  266. download_archive_fn = os.path.expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive
  267. ydl_opts = {
  268. 'usenetrc': opts.usenetrc,
  269. 'username': opts.username,
  270. 'password': opts.password,
  271. 'twofactor': opts.twofactor,
  272. 'videopassword': opts.videopassword,
  273. 'quiet': (opts.quiet or any_printing),
  274. 'no_warnings': opts.no_warnings,
  275. 'forceurl': opts.geturl,
  276. 'forcetitle': opts.gettitle,
  277. 'forceid': opts.getid,
  278. 'forcethumbnail': opts.getthumbnail,
  279. 'forcedescription': opts.getdescription,
  280. 'forceduration': opts.getduration,
  281. 'forcefilename': opts.getfilename,
  282. 'forceformat': opts.getformat,
  283. 'forcejson': opts.dumpjson,
  284. 'simulate': opts.simulate,
  285. 'skip_download': (opts.skip_download or opts.simulate or any_printing),
  286. 'format': opts.format,
  287. 'format_limit': opts.format_limit,
  288. 'listformats': opts.listformats,
  289. 'outtmpl': outtmpl,
  290. 'autonumber_size': opts.autonumber_size,
  291. 'restrictfilenames': opts.restrictfilenames,
  292. 'ignoreerrors': opts.ignoreerrors,
  293. 'ratelimit': opts.ratelimit,
  294. 'nooverwrites': opts.nooverwrites,
  295. 'retries': opts.retries,
  296. 'buffersize': opts.buffersize,
  297. 'noresizebuffer': opts.noresizebuffer,
  298. 'continuedl': opts.continue_dl,
  299. 'noprogress': opts.noprogress,
  300. 'progress_with_newline': opts.progress_with_newline,
  301. 'playliststart': opts.playliststart,
  302. 'playlistend': opts.playlistend,
  303. 'noplaylist': opts.noplaylist,
  304. 'logtostderr': opts.outtmpl == '-',
  305. 'consoletitle': opts.consoletitle,
  306. 'nopart': opts.nopart,
  307. 'updatetime': opts.updatetime,
  308. 'writedescription': opts.writedescription,
  309. 'writeannotations': opts.writeannotations,
  310. 'writeinfojson': opts.writeinfojson,
  311. 'writethumbnail': opts.writethumbnail,
  312. 'writesubtitles': opts.writesubtitles,
  313. 'writeautomaticsub': opts.writeautomaticsub,
  314. 'allsubtitles': opts.allsubtitles,
  315. 'listsubtitles': opts.listsubtitles,
  316. 'subtitlesformat': opts.subtitlesformat,
  317. 'subtitleslangs': opts.subtitleslangs,
  318. 'matchtitle': decodeOption(opts.matchtitle),
  319. 'rejecttitle': decodeOption(opts.rejecttitle),
  320. 'max_downloads': opts.max_downloads,
  321. 'prefer_free_formats': opts.prefer_free_formats,
  322. 'verbose': opts.verbose,
  323. 'dump_intermediate_pages': opts.dump_intermediate_pages,
  324. 'write_pages': opts.write_pages,
  325. 'test': opts.test,
  326. 'keepvideo': opts.keepvideo,
  327. 'min_filesize': opts.min_filesize,
  328. 'max_filesize': opts.max_filesize,
  329. 'min_views': opts.min_views,
  330. 'max_views': opts.max_views,
  331. 'daterange': date,
  332. 'cachedir': opts.cachedir,
  333. 'youtube_print_sig_code': opts.youtube_print_sig_code,
  334. 'age_limit': opts.age_limit,
  335. 'download_archive': download_archive_fn,
  336. 'cookiefile': opts.cookiefile,
  337. 'nocheckcertificate': opts.no_check_certificate,
  338. 'prefer_insecure': opts.prefer_insecure,
  339. 'proxy': opts.proxy,
  340. 'socket_timeout': opts.socket_timeout,
  341. 'bidi_workaround': opts.bidi_workaround,
  342. 'debug_printtraffic': opts.debug_printtraffic,
  343. 'prefer_ffmpeg': opts.prefer_ffmpeg,
  344. 'include_ads': opts.include_ads,
  345. 'default_search': opts.default_search,
  346. 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
  347. 'encoding': opts.encoding,
  348. 'exec_cmd': opts.exec_cmd,
  349. }
  350. with YoutubeDL(ydl_opts) as ydl:
  351. ydl.print_debug_header()
  352. ydl.add_default_info_extractors()
  353. # PostProcessors
  354. # Add the metadata pp first, the other pps will copy it
  355. if opts.addmetadata:
  356. ydl.add_post_processor(FFmpegMetadataPP())
  357. if opts.extractaudio:
  358. ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))
  359. if opts.recodevideo:
  360. ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))
  361. if opts.embedsubtitles:
  362. ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))
  363. if opts.xattrs:
  364. ydl.add_post_processor(XAttrMetadataPP())
  365. if opts.embedthumbnail:
  366. if not opts.addmetadata:
  367. ydl.add_post_processor(FFmpegAudioFixPP())
  368. ydl.add_post_processor(AtomicParsleyPP())
  369. # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way.
  370. # So if the user is able to remove the file before your postprocessor runs it might cause a few problems.
  371. if opts.exec_cmd:
  372. ydl.add_post_processor(ExecAfterDownloadPP(
  373. verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd))
  374. # Update version
  375. if opts.update_self:
  376. update_self(ydl.to_screen, opts.verbose)
  377. # Remove cache dir
  378. if opts.rm_cachedir:
  379. ydl.cache.remove()
  380. # Maybe do nothing
  381. if (len(all_urls) < 1) and (opts.load_info_filename is None):
  382. if not (opts.update_self or opts.rm_cachedir):
  383. parser.error(u'you must provide at least one URL')
  384. else:
  385. sys.exit()
  386. try:
  387. if opts.load_info_filename is not None:
  388. retcode = ydl.download_with_info_file(opts.load_info_filename)
  389. else:
  390. retcode = ydl.download(all_urls)
  391. except MaxDownloadsReached:
  392. ydl.to_screen(u'--max-download limit reached, aborting.')
  393. retcode = 101
  394. sys.exit(retcode)
  395. def main(argv=None):
  396. try:
  397. _real_main(argv)
  398. except DownloadError:
  399. sys.exit(1)
  400. except SameFileError:
  401. sys.exit(u'ERROR: fixed output name but more than one file to download')
  402. except KeyboardInterrupt:
  403. sys.exit(u'\nERROR: Interrupted by user')