def parsecmdargs(argstr): args = shlex.split(argstr) # This ensures the correct code path is taken in the monkey patch args.append("--update") youtube_dl_init.YoutubeDL = DummyYoutubeDL youtube_dl_init.sys = DummySys() youtube_dl_init.update_self = dummy_update_self try: youtube_dl_init._real_main(args) except BreakerError: pass return lastargs, args
def get_json(): """docstring for get_json""" try: with Capturing() as output: youtube_dl._real_main(["-j", sys.argv[1]]) except SystemExit: pass metadata = json.loads(output[0]) # with open("example.json") as f: # metadata = json.load(f) return metadata
def getVideo(request): videoUrl = request.GET.get('videoUrl') video_id = request.GET.get('videoId') myArgs = [] myArgs.append('-f') myArgs.append('18') myArgs.append(videoUrl) videoData = youtube_dl._real_main(myArgs, VIDEO_FOLDER) returnObject = {'status': 'success', 'videoId':videoData['id']} response = sendObjectAsJson(returnObject) return response
def getVideoDetails(request): originalUrl = request.GET.get('videoUrl') myArgs = [] myArgs.append('-f') myArgs.append('18') myArgs.append('--skip-download') myArgs.append(originalUrl) videoData = youtube_dl._real_main(myArgs, VIDEO_FOLDER) if videoData is not None: videoUrl = videoData['url'] thumbnail = videoData['thumbnail'] videoId = videoData['id'] returnObject = {'status': 'success','originalUrl':originalUrl, 'videoId':videoId, 'thumbnail':thumbnail, 'videoUrl':videoUrl} response = sendObjectAsJson(returnObject) return response
def download_youtube(self, url, index): try: download_url = _real_main(url) except youtube_dl.utils.DownloadError: print "Error: get download url failed, index={}".format(index) return None try: r = requests.get(download_url) filename = "{}/videos/{}.mp4".format(self.output, index) if os.path.exists(filename): os.remove(filename) with open(filename, "w") as f: f.write(r.content) except Exception as ex: print "Error: download youtube video failed, index={}".format( index) return None return filename
def download(): """ qs下载引擎,使用--video or -v使用youtube-dl下载视频 Qs download engine, use --video or -v use the default video download engine download """ if any([i in sys.argv for i in ['-h', '-help', '--help']]): from . import user_lang, qs_default_console, qs_info_string qs_default_console.print( qs_info_string, 'Usage: "qs dl [url...]"\n' ' [--video] | [-v] :-> download video (use youtube-dl)\n' ' [--proxy] | [-px] :-> use default proxy set in ~/.qsrc\n' ' [--name fileName] :-> Set File Name' if user_lang != 'zh' else '使用: "qs dl [链接...]"\n' ' [--video] | [-v] :-> 使用youtube-dl下载视频\n' ' [--proxy] | [-px] :-> 使用配置表中的默认代理下载\n' ' [--name fileName] :-> 设置文件名') return global _real_main ytb_flag = '--video' in sys.argv or '-v' in sys.argv use_proxy = '--proxy' in sys.argv or '-px' in sys.argv other_args = [] set_name = None if '--name' in sys.argv: set_name = sys.argv[sys.argv.index('--name') + 1] if ytb_flag or use_proxy or set_name: [ sys.argv.remove(i) if i in sys.argv else None for i in ['--video', '-v', '--proxy', '-px', '--name', set_name] ] nxt_flag = False for item in sys.argv[2:]: if item.startswith('-') or nxt_flag: if nxt_flag: nxt_flag = False if item.startswith('--'): nxt_flag = True other_args.append(item) [sys.argv.remove(item) for item in other_args] urls = sys.argv[2:] if not urls: from . import requirePackage pyperclip = requirePackage('pyperclip') urls = pyperclip.paste().split() if urls: if ytb_flag: from . import requirePackage from youtube_dl import _real_main _real_main = requirePackage('youtube_dl', '_real_main') from .NetTools.NormalDL import normal_dl from . import qs_config for url in urls: if url.endswith('.m3u8'): m3u8_dl(url) else: if use_proxy: normal_dl(url, set_name=set_name, set_proxy=qs_config.basicSelect('default_proxy')) \ if not ytb_flag else _real_main([url, '--proxy', qs_config.basicSelect('default_proxy'), '--merge-output-format', 'mp4'] + other_args) else: normal_dl(url, set_name=set_name) \ if not ytb_flag else _real_main([url, '--merge-output-format', 'mp4'] + other_args) else: from . import user_lang, qs_default_console, qs_error_string qs_default_console.log( qs_error_string, "No url found!" if user_lang != 'zh' else '无链接输入')
# -*- coding: utf-8 -*- from youtube_dl import _real_main print(_real_main("https://www.youtube.com/watch?v=dTCcegIiKXE")) __all__ = ['main', 'YoutubeDL', 'gen_extractors', 'list_extractors']