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.

435 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. # Do not download videos when there are audio-only formats
  241. if opts.extractaudio and not opts.keepvideo and opts.format is None:
  242. opts.format = 'bestaudio/best'
  243. # --all-sub automatically sets --write-sub if --write-auto-sub is not given
  244. # this was the old behaviour if only --all-sub was given.
  245. if opts.allsubtitles and (opts.writeautomaticsub == False):
  246. opts.writesubtitles = True
  247. if sys.version_info < (3,):
  248. # In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
  249. if opts.outtmpl is not None:
  250. opts.outtmpl = opts.outtmpl.decode(preferredencoding())
  251. outtmpl =((opts.outtmpl is not None and opts.outtmpl)
  252. or (opts.format == '-1' and opts.usetitle and u'%(title)s-%(id)s-%(format)s.%(ext)s')
  253. or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
  254. or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
  255. or (opts.usetitle and u'%(title)s-%(id)s.%(ext)s')
  256. or (opts.useid and u'%(id)s.%(ext)s')
  257. or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
  258. or DEFAULT_OUTTMPL)
  259. if not os.path.splitext(outtmpl)[1] and opts.extractaudio:
  260. parser.error(u'Cannot download a video and extract audio into the same'
  261. u' file! Use "{0}.%(ext)s" instead of "{0}" as the output'
  262. u' template'.format(outtmpl))
  263. 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
  264. download_archive_fn = os.path.expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive
  265. ydl_opts = {
  266. 'usenetrc': opts.usenetrc,
  267. 'username': opts.username,
  268. 'password': opts.password,
  269. 'twofactor': opts.twofactor,
  270. 'videopassword': opts.videopassword,
  271. 'quiet': (opts.quiet or any_printing),
  272. 'no_warnings': opts.no_warnings,
  273. 'forceurl': opts.geturl,
  274. 'forcetitle': opts.gettitle,
  275. 'forceid': opts.getid,
  276. 'forcethumbnail': opts.getthumbnail,
  277. 'forcedescription': opts.getdescription,
  278. 'forceduration': opts.getduration,
  279. 'forcefilename': opts.getfilename,
  280. 'forceformat': opts.getformat,
  281. 'forcejson': opts.dumpjson,
  282. 'simulate': opts.simulate,
  283. 'skip_download': (opts.skip_download or opts.simulate or any_printing),
  284. 'format': opts.format,
  285. 'format_limit': opts.format_limit,
  286. 'listformats': opts.listformats,
  287. 'outtmpl': outtmpl,
  288. 'autonumber_size': opts.autonumber_size,
  289. 'restrictfilenames': opts.restrictfilenames,
  290. 'ignoreerrors': opts.ignoreerrors,
  291. 'ratelimit': opts.ratelimit,
  292. 'nooverwrites': opts.nooverwrites,
  293. 'retries': opts.retries,
  294. 'buffersize': opts.buffersize,
  295. 'noresizebuffer': opts.noresizebuffer,
  296. 'continuedl': opts.continue_dl,
  297. 'noprogress': opts.noprogress,
  298. 'progress_with_newline': opts.progress_with_newline,
  299. 'playliststart': opts.playliststart,
  300. 'playlistend': opts.playlistend,
  301. 'noplaylist': opts.noplaylist,
  302. 'logtostderr': opts.outtmpl == '-',
  303. 'consoletitle': opts.consoletitle,
  304. 'nopart': opts.nopart,
  305. 'updatetime': opts.updatetime,
  306. 'writedescription': opts.writedescription,
  307. 'writeannotations': opts.writeannotations,
  308. 'writeinfojson': opts.writeinfojson,
  309. 'writethumbnail': opts.writethumbnail,
  310. 'writesubtitles': opts.writesubtitles,
  311. 'writeautomaticsub': opts.writeautomaticsub,
  312. 'allsubtitles': opts.allsubtitles,
  313. 'listsubtitles': opts.listsubtitles,
  314. 'subtitlesformat': opts.subtitlesformat,
  315. 'subtitleslangs': opts.subtitleslangs,
  316. 'matchtitle': decodeOption(opts.matchtitle),
  317. 'rejecttitle': decodeOption(opts.rejecttitle),
  318. 'max_downloads': opts.max_downloads,
  319. 'prefer_free_formats': opts.prefer_free_formats,
  320. 'verbose': opts.verbose,
  321. 'dump_intermediate_pages': opts.dump_intermediate_pages,
  322. 'write_pages': opts.write_pages,
  323. 'test': opts.test,
  324. 'keepvideo': opts.keepvideo,
  325. 'min_filesize': opts.min_filesize,
  326. 'max_filesize': opts.max_filesize,
  327. 'min_views': opts.min_views,
  328. 'max_views': opts.max_views,
  329. 'daterange': date,
  330. 'cachedir': opts.cachedir,
  331. 'youtube_print_sig_code': opts.youtube_print_sig_code,
  332. 'age_limit': opts.age_limit,
  333. 'download_archive': download_archive_fn,
  334. 'cookiefile': opts.cookiefile,
  335. 'nocheckcertificate': opts.no_check_certificate,
  336. 'prefer_insecure': opts.prefer_insecure,
  337. 'proxy': opts.proxy,
  338. 'socket_timeout': opts.socket_timeout,
  339. 'bidi_workaround': opts.bidi_workaround,
  340. 'debug_printtraffic': opts.debug_printtraffic,
  341. 'prefer_ffmpeg': opts.prefer_ffmpeg,
  342. 'include_ads': opts.include_ads,
  343. 'default_search': opts.default_search,
  344. 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
  345. 'encoding': opts.encoding,
  346. 'exec_cmd': opts.exec_cmd,
  347. }
  348. with YoutubeDL(ydl_opts) as ydl:
  349. ydl.print_debug_header()
  350. ydl.add_default_info_extractors()
  351. # PostProcessors
  352. # Add the metadata pp first, the other pps will copy it
  353. if opts.addmetadata:
  354. ydl.add_post_processor(FFmpegMetadataPP())
  355. if opts.extractaudio:
  356. ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))
  357. if opts.recodevideo:
  358. ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))
  359. if opts.embedsubtitles:
  360. ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))
  361. if opts.xattrs:
  362. ydl.add_post_processor(XAttrMetadataPP())
  363. if opts.embedthumbnail:
  364. if not opts.addmetadata:
  365. ydl.add_post_processor(FFmpegAudioFixPP())
  366. ydl.add_post_processor(AtomicParsleyPP())
  367. # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way.
  368. # So if the user is able to remove the file before your postprocessor runs it might cause a few problems.
  369. if opts.exec_cmd:
  370. ydl.add_post_processor(ExecAfterDownloadPP(
  371. verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd))
  372. # Update version
  373. if opts.update_self:
  374. update_self(ydl.to_screen, opts.verbose)
  375. # Remove cache dir
  376. if opts.rm_cachedir:
  377. ydl.cache.remove()
  378. # Maybe do nothing
  379. if (len(all_urls) < 1) and (opts.load_info_filename is None):
  380. if not (opts.update_self or opts.rm_cachedir):
  381. parser.error(u'you must provide at least one URL')
  382. else:
  383. sys.exit()
  384. try:
  385. if opts.load_info_filename is not None:
  386. retcode = ydl.download_with_info_file(opts.load_info_filename)
  387. else:
  388. retcode = ydl.download(all_urls)
  389. except MaxDownloadsReached:
  390. ydl.to_screen(u'--max-download limit reached, aborting.')
  391. retcode = 101
  392. sys.exit(retcode)
  393. def main(argv=None):
  394. try:
  395. _real_main(argv)
  396. except DownloadError:
  397. sys.exit(1)
  398. except SameFileError:
  399. sys.exit(u'ERROR: fixed output name but more than one file to download')
  400. except KeyboardInterrupt:
  401. sys.exit(u'\nERROR: Interrupted by user')