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. 'Dennis Scheiba',
  82. 'Damon Timm',
  83. )
  84. __license__ = 'Public Domain'
  85. import codecs
  86. import io
  87. import os
  88. import random
  89. import sys
  90. from .options import (
  91. parseOpts,
  92. )
  93. from .utils import (
  94. compat_getpass,
  95. compat_print,
  96. DateRange,
  97. DEFAULT_OUTTMPL,
  98. decodeOption,
  99. DownloadError,
  100. MaxDownloadsReached,
  101. preferredencoding,
  102. read_batch_urls,
  103. SameFileError,
  104. setproctitle,
  105. std_headers,
  106. write_string,
  107. )
  108. from .update import update_self
  109. from .downloader import (
  110. FileDownloader,
  111. )
  112. from .extractor import gen_extractors
  113. from .YoutubeDL import YoutubeDL
  114. from .postprocessor import (
  115. AtomicParsleyPP,
  116. FFmpegAudioFixPP,
  117. FFmpegMetadataPP,
  118. FFmpegVideoConvertor,
  119. FFmpegExtractAudioPP,
  120. FFmpegEmbedSubtitlePP,
  121. XAttrMetadataPP,
  122. ExecAfterDownloadPP,
  123. )
  124. def _real_main(argv=None):
  125. # Compatibility fixes for Windows
  126. if sys.platform == 'win32':
  127. # https://github.com/rg3/youtube-dl/issues/820
  128. codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)
  129. setproctitle(u'youtube-dl')
  130. parser, opts, args = parseOpts(argv)
  131. # Set user agent
  132. if opts.user_agent is not None:
  133. std_headers['User-Agent'] = opts.user_agent
  134. # Set referer
  135. if opts.referer is not None:
  136. std_headers['Referer'] = opts.referer
  137. # Custom HTTP headers
  138. if opts.headers is not None:
  139. for h in opts.headers:
  140. if h.find(':', 1) < 0:
  141. parser.error(u'wrong header formatting, it should be key:value, not "%s"'%h)
  142. key, value = h.split(':', 2)
  143. if opts.verbose:
  144. write_string(u'[debug] Adding header from command line option %s:%s\n'%(key, value))
  145. std_headers[key] = value
  146. # Dump user agent
  147. if opts.dump_user_agent:
  148. compat_print(std_headers['User-Agent'])
  149. sys.exit(0)
  150. # Batch file verification
  151. batch_urls = []
  152. if opts.batchfile is not None:
  153. try:
  154. if opts.batchfile == '-':
  155. batchfd = sys.stdin
  156. else:
  157. batchfd = io.open(opts.batchfile, 'r', encoding='utf-8', errors='ignore')
  158. batch_urls = read_batch_urls(batchfd)
  159. if opts.verbose:
  160. write_string(u'[debug] Batch file urls: ' + repr(batch_urls) + u'\n')
  161. except IOError:
  162. sys.exit(u'ERROR: batch file could not be read')
  163. all_urls = batch_urls + args
  164. all_urls = [url.strip() for url in all_urls]
  165. _enc = preferredencoding()
  166. all_urls = [url.decode(_enc, 'ignore') if isinstance(url, bytes) else url for url in all_urls]
  167. extractors = gen_extractors()
  168. if opts.list_extractors:
  169. for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
  170. compat_print(ie.IE_NAME + (' (CURRENTLY BROKEN)' if not ie._WORKING else ''))
  171. matchedUrls = [url for url in all_urls if ie.suitable(url)]
  172. for mu in matchedUrls:
  173. compat_print(u' ' + mu)
  174. sys.exit(0)
  175. if opts.list_extractor_descriptions:
  176. for ie in sorted(extractors, key=lambda ie: ie.IE_NAME.lower()):
  177. if not ie._WORKING:
  178. continue
  179. desc = getattr(ie, 'IE_DESC', ie.IE_NAME)
  180. if desc is False:
  181. continue
  182. if hasattr(ie, 'SEARCH_KEY'):
  183. _SEARCHES = (u'cute kittens', u'slithering pythons', u'falling cat', u'angry poodle', u'purple fish', u'running tortoise', u'sleeping bunny')
  184. _COUNTS = (u'', u'5', u'10', u'all')
  185. desc += u' (Example: "%s%s:%s" )' % (ie.SEARCH_KEY, random.choice(_COUNTS), random.choice(_SEARCHES))
  186. compat_print(desc)
  187. sys.exit(0)
  188. # Conflicting, missing and erroneous options
  189. if opts.usenetrc and (opts.username is not None or opts.password is not None):
  190. parser.error(u'using .netrc conflicts with giving username/password')
  191. if opts.password is not None and opts.username is None:
  192. parser.error(u'account username missing\n')
  193. if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
  194. parser.error(u'using output template conflicts with using title, video ID or auto number')
  195. if opts.usetitle and opts.useid:
  196. parser.error(u'using title conflicts with using video ID')
  197. if opts.username is not None and opts.password is None:
  198. opts.password = compat_getpass(u'Type account password and press [Return]: ')
  199. if opts.ratelimit is not None:
  200. numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
  201. if numeric_limit is None:
  202. parser.error(u'invalid rate limit specified')
  203. opts.ratelimit = numeric_limit
  204. if opts.min_filesize is not None:
  205. numeric_limit = FileDownloader.parse_bytes(opts.min_filesize)
  206. if numeric_limit is None:
  207. parser.error(u'invalid min_filesize specified')
  208. opts.min_filesize = numeric_limit
  209. if opts.max_filesize is not None:
  210. numeric_limit = FileDownloader.parse_bytes(opts.max_filesize)
  211. if numeric_limit is None:
  212. parser.error(u'invalid max_filesize specified')
  213. opts.max_filesize = numeric_limit
  214. if opts.retries is not None:
  215. try:
  216. opts.retries = int(opts.retries)
  217. except (TypeError, ValueError):
  218. parser.error(u'invalid retry count specified')
  219. if opts.buffersize is not None:
  220. numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
  221. if numeric_buffersize is None:
  222. parser.error(u'invalid buffer size specified')
  223. opts.buffersize = numeric_buffersize
  224. if opts.playliststart <= 0:
  225. raise ValueError(u'Playlist start must be positive')
  226. if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart:
  227. raise ValueError(u'Playlist end must be greater than playlist start')
  228. if opts.extractaudio:
  229. if opts.audioformat not in ['best', 'aac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']:
  230. parser.error(u'invalid audio format specified')
  231. if opts.audioquality:
  232. opts.audioquality = opts.audioquality.strip('k').strip('K')
  233. if not opts.audioquality.isdigit():
  234. parser.error(u'invalid audio quality specified')
  235. if opts.recodevideo is not None:
  236. if opts.recodevideo not in ['mp4', 'flv', 'webm', 'ogg', 'mkv']:
  237. parser.error(u'invalid video recode format specified')
  238. if opts.date is not None:
  239. date = DateRange.day(opts.date)
  240. else:
  241. date = DateRange(opts.dateafter, opts.datebefore)
  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. 'extract_flat': opts.extract_flat,
  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')