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.

438 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. 'Xavier Beynon'
  82. )
  83. __license__ = 'Public Domain'
  84. import codecs
  85. import io
  86. import os
  87. import random
  88. import sys
  89. from .options import (
  90. parseOpts,
  91. )
  92. from .utils import (
  93. compat_getpass,
  94. compat_print,
  95. DateRange,
  96. DEFAULT_OUTTMPL,
  97. decodeOption,
  98. DownloadError,
  99. MaxDownloadsReached,
  100. preferredencoding,
  101. read_batch_urls,
  102. SameFileError,
  103. setproctitle,
  104. std_headers,
  105. write_string,
  106. )
  107. from .update import update_self
  108. from .downloader import (
  109. FileDownloader,
  110. )
  111. from .extractor import gen_extractors
  112. from .YoutubeDL import YoutubeDL
  113. from .postprocessor import (
  114. AtomicParsleyPP,
  115. FFmpegAudioFixPP,
  116. FFmpegMetadataPP,
  117. FFmpegVideoConvertor,
  118. FFmpegExtractAudioPP,
  119. FFmpegEmbedSubtitlePP,
  120. XAttrMetadataPP,
  121. ExecAfterDownloadPP,
  122. )
  123. def _real_main(argv=None):
  124. # Compatibility fixes for Windows
  125. if sys.platform == 'win32':
  126. # https://github.com/rg3/youtube-dl/issues/820
  127. codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
  128. setproctitle(u'youtube-dl')
  129. parser, opts, args = parseOpts(argv)
  130. # Set user agent
  131. if opts.user_agent is not None:
  132. std_headers['User-Agent'] = opts.user_agent
  133. # Set referer
  134. if opts.referer is not None:
  135. std_headers['Referer'] = opts.referer
  136. # Custom HTTP headers
  137. if opts.headers is not None:
  138. for h in opts.headers:
  139. if h.find(':', 1) < 0:
  140. parser.error(u'wrong header formatting, it should be key:value, not "%s"'%h)
  141. key, value = h.split(':', 2)
  142. if opts.verbose:
  143. write_string(u'[debug] Adding header from command line option %s:%s\n'%(key, value))
  144. std_headers[key] = value
  145. # Dump user agent
  146. if opts.dump_user_agent:
  147. compat_print(std_headers['User-Agent'])
  148. sys.exit(0)
  149. # Batch file verification
  150. batch_urls = []
  151. if opts.batchfile is not None:
  152. try:
  153. if opts.batchfile == '-':
  154. batchfd = sys.stdin
  155. else:
  156. batchfd = io.open(opts.batchfile, 'r', encoding='utf-8', errors='ignore')
  157. batch_urls = read_batch_urls(batchfd)
  158. if opts.verbose:
  159. write_string(u'[debug] Batch file urls: ' + repr(batch_urls) + u'\n')
  160. except IOError:
  161. sys.exit(u'ERROR: batch file could not be read')
  162. all_urls = batch_urls + args
  163. all_urls = [url.strip() for url in all_urls]
  164. _enc = preferredencoding()
  165. all_urls = [url.decode(_enc, 'ignore') if isinstance(url, bytes) else url for url in all_urls]
  166. extractors = gen_extractors()
  167. if opts.list_extractors:
  168. for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
  169. compat_print(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie._WORKING else ''))
  170. matchedUrls = [url for url in all_urls if ie.suitable(url)]
  171. for mu in matchedUrls:
  172. compat_print(u' ' + mu)
  173. sys.exit(0)
  174. if opts.list_extractor_descriptions:
  175. for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
  176. if not ie._WORKING:
  177. continue
  178. desc = getattr(ie, 'IE_DESC', ie.IE_NAME)
  179. if desc is False:
  180. continue
  181. if hasattr(ie, 'SEARCH_KEY'):
  182. _SEARCHES = (u'cute kittens', u'slithering pythons', u'falling cat', u'angry poodle', u'purple fish', u'running tortoise', u'sleeping bunny')
  183. _COUNTS = (u'', u'5', u'10', u'all')
  184. desc += u' (Example: "%s%s:%s" )' % (ie.SEARCH_KEY, random.choice(_COUNTS), random.choice(_SEARCHES))
  185. compat_print(desc)
  186. sys.exit(0)
  187. # Conflicting, missing and erroneous options
  188. if opts.usenetrc and (opts.username is not None or opts.password is not None):
  189. parser.error(u'using .netrc conflicts with giving username/password')
  190. if opts.password is not None and opts.username is None:
  191. parser.error(u'account username missing\n')
  192. if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
  193. parser.error(u'using output template conflicts with using title, video ID or auto number')
  194. if opts.usetitle and opts.useid:
  195. parser.error(u'using title conflicts with using video ID')
  196. if opts.username is not None and opts.password is None:
  197. opts.password = compat_getpass(u'Type account password and press [Return]: ')
  198. if opts.ratelimit is not None:
  199. numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
  200. if numeric_limit is None:
  201. parser.error(u'invalid rate limit specified')
  202. opts.ratelimit = numeric_limit
  203. if opts.min_filesize is not None:
  204. numeric_limit = FileDownloader.parse_bytes(opts.min_filesize)
  205. if numeric_limit is None:
  206. parser.error(u'invalid min_filesize specified')
  207. opts.min_filesize = numeric_limit
  208. if opts.max_filesize is not None:
  209. numeric_limit = FileDownloader.parse_bytes(opts.max_filesize)
  210. if numeric_limit is None:
  211. parser.error(u'invalid max_filesize specified')
  212. opts.max_filesize = numeric_limit
  213. if opts.retries is not None:
  214. try:
  215. opts.retries = int(opts.retries)
  216. except (TypeError, ValueError):
  217. parser.error(u'invalid retry count specified')
  218. if opts.buffersize is not None:
  219. numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
  220. if numeric_buffersize is None:
  221. parser.error(u'invalid buffer size specified')
  222. opts.buffersize = numeric_buffersize
  223. if opts.playliststart <= 0:
  224. raise ValueError(u'Playlist start must be positive')
  225. if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart:
  226. raise ValueError(u'Playlist end must be greater than playlist start')
  227. if opts.extractaudio:
  228. if opts.audioformat not in ['best', 'aac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']:
  229. parser.error(u'invalid audio format specified')
  230. if opts.audioquality:
  231. opts.audioquality = opts.audioquality.strip('k').strip('K')
  232. if not opts.audioquality.isdigit():
  233. parser.error(u'invalid audio quality specified')
  234. if opts.recodevideo is not None:
  235. if opts.recodevideo not in ['mp4', 'flv', 'webm', 'ogg', 'mkv']:
  236. parser.error(u'invalid video recode format specified')
  237. if opts.date is not None:
  238. date = DateRange.day(opts.date)
  239. else:
  240. date = DateRange(opts.dateafter, opts.datebefore)
  241. if opts.default_search not in ('auto', 'auto_warning', 'error', 'fixup_error', None) and ':' not in opts.default_search:
  242. parser.error(u'--default-search invalid; did you forget a colon (:) at the end?')
  243. # Do not download videos when there are audio-only formats
  244. if opts.extractaudio and not opts.keepvideo and opts.format is None:
  245. opts.format = 'bestaudio/best'
  246. # --all-sub automatically sets --write-sub if --write-auto-sub is not given
  247. # this was the old behaviour if only --all-sub was given.
  248. if opts.allsubtitles and (opts.writeautomaticsub == False):
  249. opts.writesubtitles = True
  250. if sys.version_info < (3,):
  251. # In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
  252. if opts.outtmpl is not None:
  253. opts.outtmpl = opts.outtmpl.decode(preferredencoding())
  254. outtmpl =((opts.outtmpl is not None and opts.outtmpl)
  255. or (opts.format == '-1' and opts.usetitle and u'%(title)s-%(id)s-%(format)s.%(ext)s')
  256. or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
  257. or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
  258. or (opts.usetitle and u'%(title)s-%(id)s.%(ext)s')
  259. or (opts.useid and u'%(id)s.%(ext)s')
  260. or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
  261. or DEFAULT_OUTTMPL)
  262. if not os.path.splitext(outtmpl)[1] and opts.extractaudio:
  263. parser.error(u'Cannot download a video and extract audio into the same'
  264. u' file! Use "{0}.%(ext)s" instead of "{0}" as the output'
  265. u' template'.format(outtmpl))
  266. 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
  267. download_archive_fn = os.path.expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive
  268. ydl_opts = {
  269. 'usenetrc': opts.usenetrc,
  270. 'username': opts.username,
  271. 'password': opts.password,
  272. 'twofactor': opts.twofactor,
  273. 'videopassword': opts.videopassword,
  274. 'quiet': (opts.quiet or any_printing),
  275. 'no_warnings': opts.no_warnings,
  276. 'forceurl': opts.geturl,
  277. 'forcetitle': opts.gettitle,
  278. 'forceid': opts.getid,
  279. 'forcethumbnail': opts.getthumbnail,
  280. 'forcedescription': opts.getdescription,
  281. 'forceduration': opts.getduration,
  282. 'forcefilename': opts.getfilename,
  283. 'forceformat': opts.getformat,
  284. 'forcejson': opts.dumpjson,
  285. 'simulate': opts.simulate,
  286. 'skip_download': (opts.skip_download or opts.simulate or any_printing),
  287. 'format': opts.format,
  288. 'format_limit': opts.format_limit,
  289. 'listformats': opts.listformats,
  290. 'outtmpl': outtmpl,
  291. 'autonumber_size': opts.autonumber_size,
  292. 'restrictfilenames': opts.restrictfilenames,
  293. 'ignoreerrors': opts.ignoreerrors,
  294. 'ratelimit': opts.ratelimit,
  295. 'nooverwrites': opts.nooverwrites,
  296. 'retries': opts.retries,
  297. 'buffersize': opts.buffersize,
  298. 'noresizebuffer': opts.noresizebuffer,
  299. 'continuedl': opts.continue_dl,
  300. 'noprogress': opts.noprogress,
  301. 'progress_with_newline': opts.progress_with_newline,
  302. 'playliststart': opts.playliststart,
  303. 'playlistend': opts.playlistend,
  304. 'noplaylist': opts.noplaylist,
  305. 'logtostderr': opts.outtmpl == '-',
  306. 'consoletitle': opts.consoletitle,
  307. 'nopart': opts.nopart,
  308. 'updatetime': opts.updatetime,
  309. 'writedescription': opts.writedescription,
  310. 'writeannotations': opts.writeannotations,
  311. 'writeinfojson': opts.writeinfojson,
  312. 'writethumbnail': opts.writethumbnail,
  313. 'writesubtitles': opts.writesubtitles,
  314. 'writeautomaticsub': opts.writeautomaticsub,
  315. 'allsubtitles': opts.allsubtitles,
  316. 'listsubtitles': opts.listsubtitles,
  317. 'subtitlesformat': opts.subtitlesformat,
  318. 'subtitleslangs': opts.subtitleslangs,
  319. 'matchtitle': decodeOption(opts.matchtitle),
  320. 'rejecttitle': decodeOption(opts.rejecttitle),
  321. 'max_downloads': opts.max_downloads,
  322. 'prefer_free_formats': opts.prefer_free_formats,
  323. 'verbose': opts.verbose,
  324. 'dump_intermediate_pages': opts.dump_intermediate_pages,
  325. 'write_pages': opts.write_pages,
  326. 'test': opts.test,
  327. 'keepvideo': opts.keepvideo,
  328. 'min_filesize': opts.min_filesize,
  329. 'max_filesize': opts.max_filesize,
  330. 'min_views': opts.min_views,
  331. 'max_views': opts.max_views,
  332. 'daterange': date,
  333. 'cachedir': opts.cachedir,
  334. 'youtube_print_sig_code': opts.youtube_print_sig_code,
  335. 'age_limit': opts.age_limit,
  336. 'download_archive': download_archive_fn,
  337. 'cookiefile': opts.cookiefile,
  338. 'nocheckcertificate': opts.no_check_certificate,
  339. 'prefer_insecure': opts.prefer_insecure,
  340. 'proxy': opts.proxy,
  341. 'socket_timeout': opts.socket_timeout,
  342. 'bidi_workaround': opts.bidi_workaround,
  343. 'debug_printtraffic': opts.debug_printtraffic,
  344. 'prefer_ffmpeg': opts.prefer_ffmpeg,
  345. 'include_ads': opts.include_ads,
  346. 'default_search': opts.default_search,
  347. 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
  348. 'encoding': opts.encoding,
  349. 'exec_cmd': opts.exec_cmd,
  350. }
  351. with YoutubeDL(ydl_opts) as ydl:
  352. ydl.print_debug_header()
  353. ydl.add_default_info_extractors()
  354. # PostProcessors
  355. # Add the metadata pp first, the other pps will copy it
  356. if opts.addmetadata:
  357. ydl.add_post_processor(FFmpegMetadataPP())
  358. if opts.extractaudio:
  359. ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))
  360. if opts.recodevideo:
  361. ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))
  362. if opts.embedsubtitles:
  363. ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))
  364. if opts.xattrs:
  365. ydl.add_post_processor(XAttrMetadataPP())
  366. if opts.embedthumbnail:
  367. if not opts.addmetadata:
  368. ydl.add_post_processor(FFmpegAudioFixPP())
  369. ydl.add_post_processor(AtomicParsleyPP())
  370. # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way.
  371. # So if the user is able to remove the file before your postprocessor runs it might cause a few problems.
  372. if opts.exec_cmd:
  373. ydl.add_post_processor(ExecAfterDownloadPP(
  374. verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd))
  375. # Update version
  376. if opts.update_self:
  377. update_self(ydl.to_screen, opts.verbose)
  378. # Remove cache dir
  379. if opts.rm_cachedir:
  380. ydl.cache.remove()
  381. # Maybe do nothing
  382. if (len(all_urls) < 1) and (opts.load_info_filename is None):
  383. if not (opts.update_self or opts.rm_cachedir):
  384. parser.error(u'you must provide at least one URL')
  385. else:
  386. sys.exit()
  387. try:
  388. if opts.load_info_filename is not None:
  389. retcode = ydl.download_with_info_file(opts.load_info_filename)
  390. else:
  391. retcode = ydl.download(all_urls)
  392. except MaxDownloadsReached:
  393. ydl.to_screen(u'--max-download limit reached, aborting.')
  394. retcode = 101
  395. sys.exit(retcode)
  396. def main(argv=None):
  397. try:
  398. _real_main(argv)
  399. except DownloadError:
  400. sys.exit(1)
  401. except SameFileError:
  402. sys.exit(u'ERROR: fixed output name but more than one file to download')
  403. except KeyboardInterrupt:
  404. sys.exit(u'\nERROR: Interrupted by user')