def download_videos(): print("starting video download") download_dir = Path(os.environ["AD_ARCHIVE_GTR_DIR"]).as_posix() youtube_creatives = CreativeInfo.objects.filter( direct_ad_url__contains="youtube.com", scraped=True, processed=False) ydl_opts = { # Only download mp4 Videos 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', 'merge_output_format': 'mp4', "outtmpl": Path(download_dir).as_posix() + '/%(id)s.%(ext)s', } for creative in youtube_creatives: try: print(f"downloading creative {creative.direct_ad_url}") file_stem = youtube_dl.extractor.YoutubeIE.extract_id( creative.direct_ad_url) ext = ".mp4" filename = f"{file_stem}{ext}" ad_file, created = AdFile.objects.get_or_create( ad_filepath=filename) if not created: # don't download video again creative.processed = True creative.was_available = True creative.AdFile_ID = ad_file creative.save() print( f"Already downloaded ad. Skip downloading. {creative.direct_ad_url}" ) continue with YoutubeDL(ydl_opts) as ytd: info = ytd.extract_info(creative.direct_ad_url, download=False) if info["duration"] > 60 * 5: # skip downloading if video is too long creative.processed = True creative.was_available = True creative.unable_to_scrape_reason = "video length past cutoff " creative.save() print( f"skipping video - past cutoff length, {creative.direct_ad_url}" ) continue ytd.download([creative.direct_ad_url]) assert Path(download_dir).joinpath(filename).exists() ad_file.ad_filepath = filename ad_file.save() creative.was_available = True creative.processed = True creative.AdFile_ID = ad_file creative.save() except youtube_dl.utils.DownloadError as exc: print(exc.exc_info) if "Video unavailable" in exc.args[0]: creative.was_available = False creative.processed = True creative.save() print( f"ad unavailable from youtube - {creative.direct_ad_url}") print("skipped processing") continue else: # Skip unhandled download reason and get as many videos as possible print(exc) continue
async def download_video(v_url): url = v_url.pattern_match.group(2) type = v_url.pattern_match.group(1).lower() await v_url.edit("`Bersiap untuk mengunduh...`") if type == "aud": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", }], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } video = False song = True elif type == "vid": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegVideoConvertor", "preferedformat": "mp4" }], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await v_url.edit("`Mengambil data, harap tunggu..`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: return await v_url.edit(f"`{str(DE)}`") except ContentTooShortError: return await v_url.edit("`The download content was too short.`") except GeoRestrictedError: return await v_url.edit( "`Video is not available from your geographic location " "due to geographic restrictions imposed by a website.`") except MaxDownloadsReached: return await v_url.edit("`Max-downloads limit has been reached.`") except PostProcessingError: return await v_url.edit("`There was an error during post processing.`") except UnavailableVideoError: return await v_url.edit( "`Media is not available in the requested format.`") except XAttrMetadataError as XAME: return await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") except ExtractorError: return await v_url.edit("`There was an error during info extraction.`") except Exception as e: return await v_url.edit(f"{str(type(e)): {str(e)}}") c_time = time.time() if song: await v_url.edit(f"`Bersiap mengunggah lagu:`\n**{rip_data['title']}**" ) await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(rip_data["duration"]), title=str(rip_data["title"]), performer=str(rip_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Mengupload..", f"{rip_data['title']}.mp3")), ) os.remove(f"{rip_data['id']}.mp3") await v_url.delete() elif video: await v_url.edit( f"`Bersiap mengupload video:`\n**{rip_data['title']}**") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp4", supports_streaming=True, caption=rip_data["title"], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Mengupload..", f"{rip_data['title']}.mp4")), ) os.remove(f"{rip_data['id']}.mp4") await v_url.delete()
async def download_video(v_url): pro = v_url sender = await pro.get_sender() me = await pro.client.get_me() pro1 = v_url.text if not sender.id == me.id: dc = await pro.reply("`processing, please weit...`") else: dc = await pro.edit("`processing, please weit...😍`") teamcobra = pro1[8:] if not teamcobra: return await dc.edit("`Error \nusage vsong <song name>`") search = SearchVideos(teamcobra, offset=1, mode="json", max_results=1) test = search.result() p = json.loads(test) q = p.get("search_result") try: teamcobra = q[0]["link"] except: return await dc.edit("`failed to find your desired song`") type = "audio" await dc.edit("`Ok downloading your song🤓...`") if type == "audio": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegVideoConvertor", "preferedformat": "mp4" }], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await dc.edit("`Fetching data, please wait..😋😍😎`") with YoutubeDL(opts) as darkcobra: darkcobra_data = darkcobra.extract_info(teamcobra) except DownloadError as error: await dc.edit(f"`{str(error)}`") return except ContentTooShortError: await dc.edit("`Oof the download content was too short😮🤐.`") return except GeoRestrictedError: await dc.edit( "`Video is not available from your geographic location due to geographic restrictions imposed by a website🤔.`" ) return except MaxDownloadsReached: await dc.edit("`Max-downloads limit has been reached😶.`") return except PostProcessingError: await dc.edit("`There was an error during post processing😐.`") return except UnavailableVideoError: await dc.edit( "`sorry, media is not available in the requested format.`") return except XAttrMetadataError as XAME: await dc.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await dc.edit("`There was an error while fetching your query...`") return except Exception as e: await dc.edit(f"{str(type(e)): {str(e)}}") return c_time = time.time() if song: await dc.edit(f"`Preparing to upload your video song😎 `\ \n**{darkcobra_data['title']}**\ \nby *{darkcobra_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, f"{darkcobra_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(darkcobra_data["duration"]), title=str(darkcobra_data["title"]), performer=str(darkcobra_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress( d, t, v_url, c_time, "Uploading your video song😍..", f"{darkcobra_data['title']}.mp3", )), ) os.remove(f"{darkcobra_data['id']}.mp3") await v_url.delete() elif video: await dc.edit(f"`Preparing to upload your video song🤗❤ :`\ \n**{darkcobra_data['title']}**\ \nby *{darkcobra_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, f"{darkcobra_data['id']}.mp4", supports_streaming=True, caption=darkcobra_data["title"], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{darkcobra_data['title']}.mp4")), ) os.remove(f"{darkcobra_data['id']}.mp4") await dc.delete()
def threadrunner(): jobs = Queue() ydl = YoutubeDL({ "extract_flat": "in_playlist", "simulate": True, "skip_download": True, "quiet": True, "cookiefile": "cookies.txt", "source_address": "0.0.0.0", "call_home": False }) while True: if not jobs.empty(): task, vid, args = jobs.get() if task == "submitdiscovery": tracker.add_item_to_tracker(args, vid) elif task == "discovery": while True: try: info = getmetadata(mysession, str(vid).strip()) break except BaseException as e: print(e) print( "Error in retrieving information, waiting 30 seconds and trying again" ) sleep(30) if info[0] or info[1]: # ccenabled or creditdata if not isdir("out/" + str(vid).strip()): mkdir("out/" + str(vid).strip()) if info[1]: open( "out/" + str(vid).strip() + "/" + str(vid).strip() + "_published_credits.json", "w").write(dumps(info[1])) if info[0]: for langcode in langs: jobs.put(("subtitles", vid, langcode)) for langcode in langs: jobs.put( ("subtitles-forceedit-metadata", vid, langcode)) for langcode in langs: jobs.put( ("subtitles-forceedit-captions", vid, langcode)) jobs.put(("complete", None, "video:" + vid)) for videodisc in info[2]: jobs.put( ("submitdiscovery", videodisc, tracker.ItemType.Video)) for channeldisc in info[3]: jobs.put(("submitdiscovery", channeldisc, tracker.ItemType.Channel)) for mixdisc in info[4]: jobs.put(("submitdiscovery", mixdisc, tracker.ItemType.MixPlaylist)) for playldisc in info[5]: jobs.put(("submitdiscovery", playldisc, tracker.ItemType.Playlist)) elif task == "subtitles": subprrun(mysession, args, vid, "default", needforcemetadata, needforcecaptions) elif task == "subtitles-forceedit-captions": subprrun(mysession, args, vid, "forceedit-captions", needforcemetadata, needforcecaptions) elif task == "subtitles-forceedit-metadata": subprrun(mysession, args, vid, "forceedit-metadata", needforcemetadata, needforcecaptions) elif task == "channel": try: y = ydl.extract_info("https://www.youtube.com/channel/" + desit.split(":", 1)[1], download=False) for itemyv in y["entries"]: jobs.put(("submitdiscovery", itemyv["id"], tracker.ItemType.Video)) jobs.put(("complete", None, "channel:" + args)) except: print( "YouTube-DL error, ignoring but not marking as complete...", "https://www.youtube.com/channel/" + desit.split(":", 1)[1]) elif task == "playlist": try: y = ydl.extract_info( "https://www.youtube.com/playlist?list=" + desit.split(":", 1)[1], download=False) for itemyvp in y["entries"]: jobs.put(("submitdiscovery", itemyvp["id"], tracker.ItemType.Video)) jobs.put(("complete", None, "playlist:" + args)) except: print( "YouTube-DL error, ignoring but not marking as complete...", "https://www.youtube.com/playlist?list=" + desit.split(":", 1)[1]) elif task == "complete": size = 0 if ":" in args: if args.split(":", 1)[0] == "video": #check if dir is empty, make zip if needed if isdir("out/" + args.split(":", 1)[1]): if not listdir("out/" + args.split(":", 1)[1]): rmdir("out/" + args.split(":", 1)[1]) else: #zip it up if not isdir("directory/" + args.split(":", 1)[1]): mkdir("directory/" + args.split(":", 1)[1]) while not isfile("directory/" + args.split(":", 1)[1] + "/" + args.split(":", 1)[1] + ".zip"): print("Attempting to zip item...") system("zip -9 -r -j directory/" + args.split(":", 1)[1] + "/" + args.split(":", 1)[1] + ".zip out/" + args.split(":", 1)[1]) #get a target targetloc = None while not targetloc: targetloc = tracker.request_upload_target() if targetloc: break else: print("Waiting 5 minutes...") sleep(300) if targetloc.startswith("rsync"): system( "rsync -rltv --timeout=300 --contimeout=300 --progress --bwlimit 0 --recursive --partial --partial-dir .rsync-tmp --min-size 1 --no-compress --compress-level 0 directory/" + args.split(":", 1)[1] + "/ " + targetloc) elif targetloc.startswith("http"): system("curl -F " + args.split(":", 1)[1] + ".zip=@directory/" + args.split(":", 1)[1] + "/" + args.split(":", 1)[1] + ".zip " + targetloc) size = getsize("directory/" + args.split(":", 1)[1] + "/" + args.split(":", 1)[1] + ".zip") #cleanup try: del langcnt[args.split(":", 1)[1]] rmtree("directory/" + args.split(":", 1)[1] + "/") rmdir("directory/" + args.split(":", 1)[1] + "/") rmtree("out/" + args.split(":", 1)[1] + "/") rmdir("out/" + args.split(":", 1)[1] + "/") except: pass tracker.mark_item_as_done(args, size) jobs.task_done() else: if not gkiller.kill_now: # get a new task from tracker collect() #cleanup desit = tracker.request_item_from_tracker() print("New task:", desit) if desit: if desit.split(":", 1)[0] == "video": needforcemetadata = { 'ab': None, 'aa': None, 'af': None, 'sq': None, 'ase': None, 'am': None, 'ar': None, 'arc': None, 'hy': None, 'as': None, 'ay': None, 'az': None, 'bn': None, 'ba': None, 'eu': None, 'be': None, 'bh': None, 'bi': None, 'bs': None, 'br': None, 'bg': None, 'yue': None, 'yue-HK': None, 'ca': None, 'chr': None, 'zh-CN': None, 'zh-HK': None, 'zh-Hans': None, 'zh-SG': None, 'zh-TW': None, 'zh-Hant': None, 'cho': None, 'co': None, 'hr': None, 'cs': None, 'da': None, 'nl': None, 'nl-BE': None, 'nl-NL': None, 'dz': None, 'en': None, 'en-CA': None, 'en-IN': None, 'en-IE': None, 'en-GB': None, 'en-US': None, 'eo': None, 'et': None, 'fo': None, 'fj': None, 'fil': None, 'fi': None, 'fr': None, 'fr-BE': None, 'fr-CA': None, 'fr-FR': None, 'fr-CH': None, 'ff': None, 'gl': None, 'ka': None, 'de': None, 'de-AT': None, 'de-DE': None, 'de-CH': None, 'el': None, 'kl': None, 'gn': None, 'gu': None, 'ht': None, 'hak': None, 'hak-TW': None, 'ha': None, 'iw': None, 'hi': None, 'hi-Latn': None, 'ho': None, 'hu': None, 'is': None, 'ig': None, 'id': None, 'ia': None, 'ie': None, 'iu': None, 'ik': None, 'ga': None, 'it': None, 'ja': None, 'jv': None, 'kn': None, 'ks': None, 'kk': None, 'km': None, 'rw': None, 'tlh': None, 'ko': None, 'ku': None, 'ky': None, 'lo': None, 'la': None, 'lv': None, 'ln': None, 'lt': None, 'lb': None, 'mk': None, 'mg': None, 'ms': None, 'ml': None, 'mt': None, 'mni': None, 'mi': None, 'mr': None, 'mas': None, 'nan': None, 'nan-TW': None, 'lus': None, 'mo': None, 'mn': None, 'my': None, 'na': None, 'nv': None, 'ne': None, 'no': None, 'oc': None, 'or': None, 'om': None, 'ps': None, 'fa': None, 'fa-AF': None, 'fa-IR': None, 'pl': None, 'pt': None, 'pt-BR': None, 'pt-PT': None, 'pa': None, 'qu': None, 'ro': None, 'rm': None, 'rn': None, 'ru': None, 'ru-Latn': None, 'sm': None, 'sg': None, 'sa': None, 'sc': None, 'gd': None, 'sr': None, 'sr-Cyrl': None, 'sr-Latn': None, 'sh': None, 'sdp': None, 'sn': None, 'scn': None, 'sd': None, 'si': None, 'sk': None, 'sl': None, 'so': None, 'st': None, 'es': None, 'es-419': None, 'es-MX': None, 'es-ES': None, 'es-US': None, 'su': None, 'sw': None, 'ss': None, 'sv': None, 'tl': None, 'tg': None, 'ta': None, 'tt': None, 'te': None, 'th': None, 'bo': None, 'ti': None, 'tpi': None, 'to': None, 'ts': None, 'tn': None, 'tr': None, 'tk': None, 'tw': None, 'uk': None, 'ur': None, 'uz': None, 'vi': None, 'vo': None, 'vor': None, 'cy': None, 'fy': None, 'wo': None, 'xh': None, 'yi': None, 'yo': None, 'zu': None } needforcecaptions = { 'ab': None, 'aa': None, 'af': None, 'sq': None, 'ase': None, 'am': None, 'ar': None, 'arc': None, 'hy': None, 'as': None, 'ay': None, 'az': None, 'bn': None, 'ba': None, 'eu': None, 'be': None, 'bh': None, 'bi': None, 'bs': None, 'br': None, 'bg': None, 'yue': None, 'yue-HK': None, 'ca': None, 'chr': None, 'zh-CN': None, 'zh-HK': None, 'zh-Hans': None, 'zh-SG': None, 'zh-TW': None, 'zh-Hant': None, 'cho': None, 'co': None, 'hr': None, 'cs': None, 'da': None, 'nl': None, 'nl-BE': None, 'nl-NL': None, 'dz': None, 'en': None, 'en-CA': None, 'en-IN': None, 'en-IE': None, 'en-GB': None, 'en-US': None, 'eo': None, 'et': None, 'fo': None, 'fj': None, 'fil': None, 'fi': None, 'fr': None, 'fr-BE': None, 'fr-CA': None, 'fr-FR': None, 'fr-CH': None, 'ff': None, 'gl': None, 'ka': None, 'de': None, 'de-AT': None, 'de-DE': None, 'de-CH': None, 'el': None, 'kl': None, 'gn': None, 'gu': None, 'ht': None, 'hak': None, 'hak-TW': None, 'ha': None, 'iw': None, 'hi': None, 'hi-Latn': None, 'ho': None, 'hu': None, 'is': None, 'ig': None, 'id': None, 'ia': None, 'ie': None, 'iu': None, 'ik': None, 'ga': None, 'it': None, 'ja': None, 'jv': None, 'kn': None, 'ks': None, 'kk': None, 'km': None, 'rw': None, 'tlh': None, 'ko': None, 'ku': None, 'ky': None, 'lo': None, 'la': None, 'lv': None, 'ln': None, 'lt': None, 'lb': None, 'mk': None, 'mg': None, 'ms': None, 'ml': None, 'mt': None, 'mni': None, 'mi': None, 'mr': None, 'mas': None, 'nan': None, 'nan-TW': None, 'lus': None, 'mo': None, 'mn': None, 'my': None, 'na': None, 'nv': None, 'ne': None, 'no': None, 'oc': None, 'or': None, 'om': None, 'ps': None, 'fa': None, 'fa-AF': None, 'fa-IR': None, 'pl': None, 'pt': None, 'pt-BR': None, 'pt-PT': None, 'pa': None, 'qu': None, 'ro': None, 'rm': None, 'rn': None, 'ru': None, 'ru-Latn': None, 'sm': None, 'sg': None, 'sa': None, 'sc': None, 'gd': None, 'sr': None, 'sr-Cyrl': None, 'sr-Latn': None, 'sh': None, 'sdp': None, 'sn': None, 'scn': None, 'sd': None, 'si': None, 'sk': None, 'sl': None, 'so': None, 'st': None, 'es': None, 'es-419': None, 'es-MX': None, 'es-ES': None, 'es-US': None, 'su': None, 'sw': None, 'ss': None, 'sv': None, 'tl': None, 'tg': None, 'ta': None, 'tt': None, 'te': None, 'th': None, 'bo': None, 'ti': None, 'tpi': None, 'to': None, 'ts': None, 'tn': None, 'tr': None, 'tk': None, 'tw': None, 'uk': None, 'ur': None, 'uz': None, 'vi': None, 'vo': None, 'vor': None, 'cy': None, 'fy': None, 'wo': None, 'xh': None, 'yi': None, 'yo': None, 'zu': None } jobs.put(("discovery", desit.split(":", 1)[1], None)) elif desit.split(":", 1)[0] == "channel": jobs.put(("channel", None, desit.split(":", 1)[1])) elif desit.split(":", 1)[0] == "playlist": jobs.put(("playlist", None, desit.split(":", 1)[1])) else: print("Ignoring item for now", desit) else: print("Ignoring item for now", desit) else: break
async def download_video(event): a = event.text if a[5] == "s": return await event.edit("`Sedang Memproses Musik, Mohon Tunggu Sebentar...`") url = event.pattern_match.group(1) if not url: return await event.edit( "**List Error**\nCara Penggunaan : -`.musik <Judul Lagu>`") search = SearchVideos(url, offset=1, mode="json", max_results=1) test = search.result() p = json.loads(test) q = p.get("search_result") try: url = q[0]["link"] except BaseException: return await event.edit("`Tidak Dapat Menemukan Musik...`") type = "audio" await event.edit(f"`Persiapan Mendownload {url}...`") if type == "audio": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", }], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } try: await event.edit("`Mendapatkan Info Musik...`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: await event.edit(f"`{str(DE)}`") return except ContentTooShortError: await event.edit("`The download content was too short.`") return except GeoRestrictedError: await event.edit( "`Video is not available from your geographic location due to" + " geographic restrictions imposed by a website.`") return except MaxDownloadsReached: await event.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await event.edit("`There was an error during post processing.`") return except UnavailableVideoError: await event.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: return await event.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") except ExtractorError: return await event.edit("`There was an error during info extraction.`") except Exception as e: return await event.edit(f"{str(type(e)): {str(e)}}") dir = os.listdir() if f"{rip_data['id']}.mp3.jpg" in dir: thumb = f"{rip_data['id']}.mp3.jpg" elif f"{rip_data['id']}.mp3.webp" in dir: thumb = f"{rip_data['id']}.mp3.webp" else: thumb = None upteload = """ Connected to server... • {} • By - {} """.format(rip_data["title"], rip_data["uploader"]) await event.edit(f"`{upteload}`") CAPT = f"╭┈────────────────┈\n➥ {rip_data['title']}\n➥ Uploader - {rip_data['uploader']}\n╭┈────────────────┈╯\n➥ By : {DEFAULTUSER}\n╰┈────────────────┈➤" await event.client.send_file( event.chat_id, f"{rip_data['id']}.mp3", thumb=thumb, supports_streaming=True, caption=CAPT, attributes=[ DocumentAttributeAudio( duration=int(rip_data["duration"]), title=str(rip_data["title"]), performer=str(rip_data["uploader"]), ) ], ) await event.delete() os.remove(f"{rip_data['id']}.mp3") try: os.remove(thumb) except BaseException: pass
async def download_video(v_url): lazy = v_url sender = await lazy.get_sender() me = await lazy.client.get_me() if not sender.id == me.id: rkp = await lazy.reply("`processing...`") else: rkp = await lazy.edit("`processing...`") url = v_url.pattern_match.group(1) if not url: return await rkp.edit("`Error \nusage song <song name>`") search = SearchVideos(url, offset=1, mode="json", max_results=1) test = search.result() p = json.loads(test) q = p.get('search_result') try: url = q[0]['link'] except: return await rkp.edit("`failed to find`") type = "audio" await rkp.edit("`Preparing to download...`") if type == "audio": opts = { 'format': 'bestaudio', 'addmetadata': True, 'key': 'FFmpegMetadata', 'writethumbnail': True, 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '320', }], 'outtmpl': '%(id)s.mp3', 'quiet': True, 'logtostderr': False } video = False song = True try: await rkp.edit("`Fetching data, please wait..`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: await rkp.edit(f"`{str(DE)}`") return except ContentTooShortError: await rkp.edit("`The download content was too short.`") return except GeoRestrictedError: await rkp.edit( "`Video is not available from your geographic location due to geographic restrictions imposed by a website.`" ) return except MaxDownloadsReached: await rkp.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await rkp.edit("`There was an error during post processing.`") return except UnavailableVideoError: await rkp.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: await rkp.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await rkp.edit("`There was an error during info extraction.`") return except Exception as e: await rkp.edit(f"{str(type(e)): {str(e)}}") return c_time = time.time() if song: await rkp.edit(f"`Preparing to upload song:`\ \n**{rip_data['title']}**\ \nby *{rip_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio(duration=int(rip_data['duration']), title=str(rip_data['title']), performer=str(rip_data['uploader'])) ], progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp3"))) os.remove(f"{rip_data['id']}.mp3") await v_url.delete() elif video: await rkp.edit(f"`Preparing to upload song :`\ \n**{rip_data['title']}**\ \nby *{rip_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp4", supports_streaming=True, caption=url, progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp4"))) os.remove(f"{rip_data['id']}.mp4") await rkp.delete()
async def youtube_play(message): query = message.pattern_match.group(2) type = message.pattern_match.group(1) if not query: await message.edit("`Enter query to download`") await message.edit("`Processing...`") try: results = json.loads(YoutubeSearch(query, max_results=7).to_json()) except KeyError: return await message.edit( "`Youtube Search gone retard.\nCan't search this query!`" ) url = f"https://www.youtube.com{results['videos'][0]['url_suffix']}" await message.edit('`Preparing to download...`') if type == "ytmusic" or type == "play": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", } ], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } video = False song = True elif type == "ytvideo": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ {"key": "FFmpegVideoConvertor", "preferedformat": "mp4"} ], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await message.edit("`Fetching data, please wait..`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: return await message.edit(f"`{str(DE)}`") except ContentTooShortError: return await message.edit("`The download content was too short.`") except GeoRestrictedError: return await message.edit( "`Video is not available from your geographic location " "due to geographic restrictions imposed by a website.`" ) except MaxDownloadsReached: return await message.edit("`Max-downloads limit has been reached.`") except PostProcessingError: return await message.edit("`There was an error during post processing.`") except UnavailableVideoError: return await message.edit("`Media is not available in the requested format.`") except XAttrMetadataError as XAME: return await message.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") except ExtractorError: return await message.edit("`There was an error during info extraction.`") except Exception as e: return await message.edit(f"{str(type(e)): {str(e)}}") c_time = time.time() if song: await message.edit(f"`Preparing to upload song:`\n**{rip_data['title']}**") await message.client.send_file( message.chat_id, f"{rip_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(rip_data["duration"]), title=str(rip_data["title"]), performer=str(rip_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress(d, t, message, c_time, "Uploading..", f"{rip_data['title']}.mp3") ), ) os.remove(f"{rip_data['id']}.mp3") await message.delete() elif video: await message.edit(f"`Preparing to upload video:`\n**{rip_data['title']}**") await message.client.send_file( message.chat_id, f"{rip_data['id']}.mp4", supports_streaming=True, caption=rip_data["title"], progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress(d, t, message, c_time, "Uploading..", f"{rip_data['title']}.mp4") ), ) os.remove(f"{rip_data['id']}.mp4") await message.delete()
def download_mp4_from_link(link): print("starting", link) ydl = YoutubeDL({'format': '140'}) ydl.download([link]) print("done downloading", link)
from youtube_dl import YoutubeDL from config import DURATION_LIMIT from helpers.errors import DurationLimitError ydl_opts = { "format": "bestaudio/best", "geo-bypass": True, "nocheckcertificate": True, "outtmpl": "downloads/%(id)s.%(ext)s", } ydl = YoutubeDL(ydl_opts) def download(url: str) -> str: info = ydl.extract_info(url, False) duration = round(info["duration"] / 60) if duration > DURATION_LIMIT: raise DurationLimitError( f"**RAJPUTBOT:** Videos longer than {DURATION_LIMIT} minute(s) aren't allowed, the provided video is {duration} minute(s)" ) ydl.download([url]) return f"downloads/{info['id']}.{info['ext']}"
sys.stdout.flush() if len(sys.argv) != 2: raise IndexError('usage: %s data_as_json' % (sys.argv[0])) input_data = json.loads(sys.argv[1]) url = input_data['url'] dl_type = input_data['type'] print_flush('input: %s' % (input_data)) choices = { 'audio': ('bestaudio[ext=m4a]', 'm4a'), 'video': ('best[ext=mp4]', 'mp4'), } fmt, ext = choices[dl_type] print('format: %s' % (fmt)) opts = { 'format': fmt, } with YoutubeDL(opts) as ydl: ydl.download([url]) f = max(glob.glob('*.' + ext), key=os.path.getctime) if not f: raise IndexError('downloaded file not found') print_flush(f"File successfully downloaded {f}")
def descargarAudio(URL): audio_downloader = YoutubeDL({'format': 'm4a'}) audio_downloader.extract_info(URL) return obtenerRutaAudio()
async def download_video(v_url): """ For media downloader command, download media from YouTube and many other sites. """ if v_url.is_reply and not v_url.pattern_match.group(2): url = await v_url.get_reply_message() url = str(url.text) else: url = str(v_url.pattern_match.group(2)) if not url: return await v_url.edit("Reply to a message with a URL or pass a URL!") type = v_url.pattern_match.group(1).lower() await v_url.edit("Preparing to download...") if type == "a": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", }], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } video = False song = True elif type == "v": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegVideoConvertor", "preferedformat": "mp4" }], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await v_url.edit("Fetching data, please wait..") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: return await v_url.edit(f"{str(DE)}") except ContentTooShortError: return await v_url.edit("The download content was too short.") except GeoRestrictedError: return await v_url.edit( "Video is not available from your geographic location " "due to geographic restrictions imposed by a website.") except MaxDownloadsReached: return await v_url.edit("Max-downloads limit has been reached.") except PostProcessingError: return await v_url.edit("There was an error during post processing.") except UnavailableVideoError: return await v_url.edit( "Media is not available in the requested format.") except XAttrMetadataError as XAME: return await v_url.edit(f"{XAME.code}: {XAME.msg}\n{XAME.reason}") except ExtractorError: return await v_url.edit("There was an error during info extraction.") except Exception as e: return await v_url.edit(f"{str(type(e)): {str(e)}}") c_time = time.time() if song: await v_url.edit(f"Preparing to upload song:\n**{rip_data['title']}**") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(rip_data["duration"]), title=str(rip_data["title"]), performer=str(rip_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp3")), ) os.remove(f"{rip_data['id']}.mp3") await v_url.delete() elif video: await v_url.edit(f"Preparing to upload video:\n**{rip_data['title']}**" ) await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp4", supports_streaming=True, caption=rip_data["title"], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp4")), ) os.remove(f"{rip_data['id']}.mp4") await v_url.delete()
async def youtube_download(client, message): args = message.text.split(None, 1) if len(args) == 1: await edit_or_reply(message, text="missing [url] parameter!!") return url = args[1] opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegVideoConvertor", "preferedformat": "mp4" }], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } try: with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) except DownloadError as DE: await edit_or_reply(message, text=f"`{str(DE)}`") return except ContentTooShortError: await edit_or_reply(message, text="`The download content was too short.`") return except GeoRestrictedError: await edit_or_reply( message, text="`Video is not available.`", ) return except MaxDownloadsReached: await edit_or_reply(message, text="`Max-downloads limit has been reached.`") return except PostProcessingError: await edit_or_reply( message, text="`There was an error during post processing.`") return except UnavailableVideoError: await edit_or_reply( message, text="`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: await edit_or_reply(message, text=f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await edit_or_reply( message, text="`There was an error during info extraction.`") return thumbnail = Path(f"{ytdl_data['id']}.jpg") c_time = time.time() try: await message.reply_video( f"{ytdl_data['id']}.mp4", supports_streaming=True, duration=ytdl_data["duration"], caption=ytdl_data["title"], thumb=thumbnail, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ), except FileNotFoundError: await message.reply_video( f"{ytdl_data['id']}.mp4", supports_streaming=True, duration=ytdl_data["duration"], caption=ytdl_data["title"], progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ), os.remove(f"{ytdl_data['id']}.mp4") if thumbnail: os.remove(thumbnail) await message.delete()
async def post(video: Video, background_tasks: BackgroundTasks, req: Request): """Download video endpoint""" progress_uuid = uuid4() progress = Progress(uuid=progress_uuid, status="downloading", last_update=datetime.utcnow()) def update_progress(): """ Update Redis with new progress """ if progress.last_update < datetime.utcnow() + timedelta(seconds=2): progress.last_update = datetime.utcnow() set_status(progress) # save downloading status update_progress() def update_converting_progress(current: int, total: int): """ Update status with the converting progress """ progress.status = "converting" progress.current = current progress.total = total update_progress() def progress_hook(data): """ Update youtube-dl progress hook """ progress.filename = data["filename"] status = data["status"] if status == "finished": progress.status = "converting" # start converting to gif progress.gif_filename = convert_to_gif(progress.uuid, progress.filename, update_converting_progress) if video.optimize: # status gif optimization optimize_gif(progress.gif_filename) progress.status = "finished" blob_filename = progress.uuid.hex + ".mp4" blob_gif_filename = progress.uuid.hex + ".gif" progress.mp4_url = store_delete_file(progress.filename, blob_filename) progress.gif_url = store_delete_file(progress.gif_filename, blob_gif_filename) # save finished status update_progress() else: progress.status = data["status"] if status == "error": # save error status logger.error("Progress status is in error") update_progress() ydl_opts = { "progress_hooks": [progress_hook], "outtmpl": dir_path + "/output/%(title)s.%(ext)s", } with YoutubeDL(ydl_opts) as ydl: try: background_tasks.add_task(ydl.download, [video.url]) return {"status": "downloading", "id": progress_uuid} except DownloadError as download_err: progress.status = "error" set_status(progress) # save error status logger.exception(download_err) raise HTTPException( status_code=500, detail="Youtube-dl download error", headers={"X-Error": str(download_err)}, ) except Exception as exception: progress.status = "error" set_status(progress) # save error status logger.exception(exception) raise HTTPException( status_code=500, detail="Unknown error", headers={"X-Error": str(exception)}, )
async def download_video(v_url): lazy = v_url sender = await lazy.get_sender() me = await lazy.client.get_me() if not sender.id == me.id: rkp = await edit_or_reply(lazy, "Processing video song request....") else: rkp = await edit_or_reply(lazy, "Processing video song request....") url = v_url.pattern_match.group(1) if not url: return await rkp.edit("**Error** \n__Usage:__ `vsong <song name>`") search = SearchVideos(url, offset=1, mode="json", max_results=1) test = search.result() p = json.loads(test) q = p.get("search_result") try: url = q[0]["link"] except: return await rkp.edit("`failed to find`") type = "audio" await rkp.edit("Video Song Request Processed. **Downloading Now!!**") if type == "audio": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegVideoConvertor", "preferedformat": "mp4" }], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await rkp.edit("Fetching Video Song") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: await rkp.edit(f"`{str(DE)}`") return except ContentTooShortError: await rkp.edit("`The download content was too short.`") return except GeoRestrictedError: await rkp.edit( "`Video is not available from your geographic location due to geographic restrictions imposed by a website.`" ) return except MaxDownloadsReached: await rkp.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await rkp.edit("`There was an error during post processing.`") return except UnavailableVideoError: await rkp.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: await rkp.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await rkp.edit("`There was an error during info extraction.`") return except Exception as e: await rkp.edit(f"{str(type(e)): {str(e)}}") return c_time = time.time() if song: await rkp.edit(f"🎶 Preparing to upload video song 🎶 :-\ \n\n**{rip_data['title']}**\ \nby __{rip_data['uploader']}__") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(rip_data["duration"]), title=str(rip_data["title"]), performer=str(rip_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp3")), ) os.remove(f"{rip_data['id']}.mp3") await v_url.delete() elif video: await rkp.edit(f"🎶 Preparing to upload video song 🎶 :-\ \n\n**{rip_data['title']}**\ \nby __{rip_data['uploader']}__") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp4", supports_streaming=True, caption=rip_data["title"], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp4")), ) os.remove(f"{rip_data['id']}.mp4") await rkp.delete()
async def download_video(v_url): """ For .ytdl command, download media from YouTube and many other sites. """ url = v_url.pattern_match.group(2) type = v_url.pattern_match.group(1).lower() await v_url.edit("جاري التنزيل ..") if type == "ص": opts = { 'format': 'bestaudio', 'addmetadata': True, 'key': 'FFmpegMetadata', 'writethumbnail': True, 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '320', }], 'outtmpl': '%(id)s.mp3', 'quiet': True, 'logtostderr': False } video = False song = True elif type == "ف": opts = { 'format': 'best', 'addmetadata': True, 'key': 'FFmpegMetadata', 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [{ 'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4' }], 'outtmpl': '%(id)s.mp4', 'logtostderr': False, 'quiet': True } song = False video = True try: await v_url.edit("يرجى الانتضار") with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) except DownloadError as DE: await v_url.edit(f"`{str(DE)}`") return except ContentTooShortError: await v_url.edit("محتوى التنزيل قصير جدا") return except GeoRestrictedError: await v_url.edit( "الفيديو غير متاح من موقعك الجغرافي بسبب القيود الجغرافية التي يفرضها موقع الويب." ) return except MaxDownloadsReached: await v_url.edit("تم وصول الحد الاقصى للتنزيلات") return except PostProcessingError: await v_url.edit("حدث خطا اثناء المعالجه") return except UnavailableVideoError: await v_url.edit("الوسائط غير متوفره بتنسيق المطلوب") return except XAttrMetadataError as XAME: await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await v_url.edit("حدث خطا اثناء استخراج معلومات") return except Exception as e: await v_url.edit(f"{str(type(e)): {str(e)}}") return c_time = time.time() if song: await v_url.edit(f"نستعد لتحميل الاغنيه:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, f"{ytdl_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio(duration=int(ytdl_data['duration']), title=str(ytdl_data['title']), performer=str(ytdl_data['uploader'])) ], progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, v_url, c_time, "Uploading..", f"{ytdl_data['title']}.mp3"))) os.remove(f"{ytdl_data['id']}.mp3") await v_url.delete() elif video: await v_url.edit(f"نستعد لتنزيل الفديو:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, f"{ytdl_data['id']}.mp4", supports_streaming=True, caption=ytdl_data['title'], progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, v_url, c_time, "Uploading..", f"{ytdl_data['title']}.mp4"))) os.remove(f"{ytdl_data['id']}.mp4") await v_url.delete()
async def _(sur): url = sur.pattern_match.group(1).decode("UTF-8") getter = sur.sender_id opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", } ], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } song = True await dler(sur) with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) jpg = f"{ytdl_data['id']}.mp3.jpg" png = f"{ytdl_data['id']}.mp3.png" webp = f"{ytdl_data['id']}.mp3.webp" dir = os.listdir() if jpg in dir: thumb = jpg elif png in dir: thumb = png elif webp in dir: thumb = webp else: thumb = None c_time = time.time() if song: await sur.edit( f"`Preparing to upload song:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*" ) await asst.send_file( getter, f"{ytdl_data['id']}.mp3", thumb=thumb, caption=f"**{ytdl_data['title']}\n{convert(ytdl_data['duration'])}\n{ytdl_data['uploader']}**", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(ytdl_data["duration"]), title=str(ytdl_data["title"]), performer=str(ytdl_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress(d, t, sur, c_time, "Uploading..", f"{ytdl_data['title']}.mp3") ), ) os.system(f"rm {ytdl_data['id']}.mp*") await sur.edit( f"Get Your requested file **{ytdl_data['title']}** from here {Var.BOT_USERNAME} ", buttons=Button.switch_inline("Search More", query="yt ", same_peer=True), )
ytdlopts = { "format": "bestaudio/best", "outtmpl": "downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s", "restrictfilenames": True, "noplaylist": True, "nocheckcertificate": True, "ignoreerrors": False, "logtostderr": False, "quiet": True, "no_warnings": True, "default_search": "auto", "source_address": "0.0.0.0", # ipv6 addresses cause issues sometimesTypeError: 'generator' object is not subscriptable } ytdl = YoutubeDL(ytdlopts) class VoiceError(Exception): pass class Song: __slots__ = ("source", "requester") def __init__(self, source: MusicSource): self.source = source self.requester = source.requester def create_embed(self): embed = (
filebase = ".".join(fn.split(".")[:-1]) if Path(filebase + ".mkv").is_file(): subprocess.Popen( f"ffmpeg -hide_banner -loglevel warning -i {filebase + '.mkv'} -map 0 -c copy -c:a aac {filebase + '.mp4'} && del {filebase + '.mkv'}" .split(" "), shell=True) print("file " + filebase + ".mp4", flush=True, end="") t = datetime.now() url = sys.argv[1] # test if link is youtube link if sum(1 for _ in re.compile(r"youtu\.?be").finditer(url)) > 0: dl = YoutubeDL(ydl_opts) # youtube downloader else: dl = YoutubeDL(dl_opts) # generic downloader # extract data (title) data = dl.extract_info(url, download=False) #print(f"{int(data['duration'] / 60)}min {data['duration'] % 60}s") title = data.get('title') site = data.get('extractor') with open("log", "a") as f: f.write(f"[{data.get('extractor')}] {title} URL: {url}\n") # print info #print(f"\033[31m[{data.get('extractor')}]\033[0m {title}")
async def _ytdl(url, is_it, event, tgbot): await event.edit( "`Ok Downloading This Video / Audio - Please Wait.` \n**Powered By @VirtualUserbot**" ) if is_it: opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "480", } ], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } video = False song = True else: opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ {"key": "FFmpegVideoConvertor", "preferedformat": "mp4"} ], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) except Exception as e: await event.edit(f"**Failed To Download** \n**Error :** `{str(e)}`") return c_time = time.time() if song: await event.edit( f"**Uploading Audio**\ \n**Title :** `{ytdl_data['title']}`\ \n**Video Uploader :** `{ytdl_data['uploader']}`" ) lol_m = await tgbot.upload_file( file=f"{ytdl_data['id']}.mp3", progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress( d, t, event, c_time, "**Uploading Audio To TG**", f"{ytdl_data['title']}.mp3", ) ), ) await event.edit( file=lol_m, text=f"{ytdl_data['title']} \n**Uploaded Using @VirtualUserbot**", ) os.remove(f"{ytdl_data['id']}.mp3") elif video: await event.edit( f"**Uploading Video**\ \n**Title :** `{ytdl_data['title']}`\ \n**Video Uploader :** `{ytdl_data['uploader']}`" ) hmmo = await tgbot.upload_file( file=f"{ytdl_data['id']}.mp4", progress_callback=lambda d, t: asyncio.get_event_loop().create_task( progress( d, t, event, c_time, "**Uploading Video To TG**", f"{ytdl_data['title']}.mp4", ) ), ) await event.edit( file=hmmo, text=f"{ytdl_data['title']} \n**Uploaded Using @VirtualUserbot**" ) os.remove(f"{ytdl_data['id']}.mp4")
async def download_video(v_url): """ For .ytdl command, download media from YouTube and many other sites. """ url = v_url.pattern_match.group(2) type = v_url.pattern_match.group(1).lower() lmao = await v_url.reply("`Preparing to download...`") if type == "audio": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "256", } ], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } video = False song = True elif type == "video": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ {"key": "FFmpegVideoConvertor", "preferedformat": "mp4"} ], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await lmao.edit("`Fetching data, please wait..`") with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) except DownloadError as DE: await lmao.edit(f"`{str(DE)}`") return except ContentTooShortError: await lmao.edit("`The download content was too short.`") return except GeoRestrictedError: await lmao.edit( "`Video is not available from your geographic location due to geographic restrictions imposed by a website.`" ) return except MaxDownloadsReached: await lmao.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await lmao.edit("`There was an error during post processing.`") return except UnavailableVideoError: await lmao.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: await lmao.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await lmao.edit("`There was an error during info extraction.`") return except Exception as e: await lmao.edit(f"{str(type(e)): {str(e)}}") return time.time() if song: await lmao.edit( f"`Preparing to upload song:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*" \n*🎧Upload By, @unlimitedworld_TM_channel* ) await v_url.client.send_file( v_url.chat_id, f"{ytdl_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio( duration=int(ytdl_data["duration"]), title=str(ytdl_data["title"]), performer=str(ytdl_data["uploader"]), ) ], ) os.remove(f"{ytdl_data['id']}.mp3") elif video: await lmao.edit( f"`Preparing to upload video:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*" \n*🎧Upload By, @unlimitedworld_TM_channel* ) await v_url.client.send_file( v_url.chat_id, f"{ytdl_data['id']}.mp4", supports_streaming=True, caption=ytdl_data["title"], ) os.remove(f"{ytdl_data['id']}.mp4")
async def download_vsong(ult): x = await eor(ult, "Processing..") url = ult.pattern_match.group(1) if not url: return await x.edit("**Error**\nUsage - `.vsong <song name>`") search = SearchVideos(url, offset=1, mode="json", max_results=1) test = search.result() p = json.loads(test) q = p.get("search_result") try: url = q[0]["link"] except BaseException: return await x.edit("`No matching songs found...`") type = "audio" await x.edit("`Preparing to download...`") if type == "audio": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [ {"key": "FFmpegVideoConvertor", "preferedformat": "mp4"} ], "outtmpl": "%(id)s.mp4", "logtostderr": False, "quiet": True, } try: await x.edit("`Fetching data, please wait..`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: return await x.edit(f"`{str(DE)}`") except ContentTooShortError: return await x.edit("`The download content was too short.`") except GeoRestrictedError: return await x.edit( "`Video is not available from your geographic location due to" + " geographic restrictions imposed by a website.`" ) except MaxDownloadsReached: return await x.edit("`Max-downloads limit has been reached.`") except PostProcessingError: return await x.edit("`There was an error during post processing.`") except UnavailableVideoError: return await x.edit("`Media is not available in the requested format.`") except XAttrMetadataError as XAME: return await x.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") except ExtractorError: return await x.edit("`There was an error during info extraction.`") except Exception as e: return await x.edit(f"{str(type(e)): {str(e)}}") tail = time.time() ttt = await uploader(rip_data['id']+".mp4", rip_data['title']+".mp4", tail, x, "Uploading " + rip_data['title']) CAPT = f"⫸ Song - {rip_data['title']}\n⫸ By - {rip_data['uploader']}\n" await ultroid_bot.send_file( ult.chat_id, ttt, supports_streaming=True, caption=CAPT, ) os.remove(f"{rip_data['id']}.mp4") await x.delete()
# # Sample 2: Download multiple youtube videos # dl = YoutubeDL() # # Put list of song urls in download function to download them, one by one # dl.download(['https://www.youtube.com/watch?v=wNVIn-QS4DE', 'https://www.youtube.com/watch?v=JZjRrg2rpic']) # Sample 3: Download audio # options = { # 'format': 'bestaudio/audio' # Tell the downloader to download only the best quality of audio # } # dl = YoutubeDL(options) # dl.download(['https://www.youtube.com/watch?v=c3jHlYsnEe0']) # Sample 4: Search and then download the first video # options = { # 'default_search': 'ytsearch', # tell downloader to search instead of directly downloading # 'max_downloads': 1 # Tell downloader to download only the first entry (video) # } # dl = YoutubeDL(options) # dl.download(['con điên TAMKA PKL']) # Sample 5: Search and then download the first audio options = { 'default_search': 'ytsearch', # tell downloader to search instead of directly downloading 'max_downloads': 1, # Tell downloader to download only the first entry (audio) 'format': 'bestaudio/audio' } dl = YoutubeDL(options) dl.download(['Nhớ mưa sài gòn lam trường'])
async def download_video(ult): a = ult.text if a[5] == "s": return x = await eor(ult, "Searching...") url = ult.pattern_match.group(1) if not url: return await x.edit("**Error**\nUsage - `.song <song name>`") search = SearchVideos(url, offset=1, mode="json", max_results=1) test = search.result() p = json.loads(test) q = p.get("search_result") try: url = q[0]["link"] except BaseException: return await x.edit("`No matching song found...`") type = "audio" await x.edit(f"`Preparing to download {url}...`") if type == "audio": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "prefer_ffmpeg": True, "nocheckcertificate": True, "postprocessors": [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", } ], "outtmpl": "%(id)s.mp3", "quiet": True, "logtostderr": False, } try: await x.edit("`Getting info...`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: await x.edit(f"`{str(DE)}`") return except ContentTooShortError: await x.edit("`The download content was too short.`") return except GeoRestrictedError: await x.edit( "`Video is not available from your geographic location due to" + " geographic restrictions imposed by a website.`" ) return except MaxDownloadsReached: await x.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await x.edit("`There was an error during post processing.`") return except UnavailableVideoError: await x.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: return await x.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") except ExtractorError: return await x.edit("`There was an error during info extraction.`") except Exception as e: return await x.edit(f"{str(type(e)): {str(e)}}") dir = os.listdir() if f"{rip_data['id']}.mp3.jpg" in dir: thumb = f"{rip_data['id']}.mp3.jpg" elif f"{rip_data['id']}.mp3.webp" in dir: thumb = f"{rip_data['id']}.mp3.webp" else: thumb = None tail = time.time() ttt = await uploader(rip_data['id']+".mp3", rip_data['title']+".mp3", tail, x, "Uploading " + rip_data['title']) CAPT = f"⫸ Song - {rip_data['title']}\n⫸ By - {rip_data['uploader']}\n" await ultroid_bot.send_file( ult.chat_id, ttt, thumb=thumb, supports_streaming=True, caption=CAPT, attributes=[ DocumentAttributeAudio( duration=int(rip_data["duration"]), title=str(rip_data["title"]), performer=str(rip_data["uploader"]), ) ], ) await x.delete() os.remove(f"{rip_data['id']}.mp3") try: os.remove(thumb) except BaseException: pass
async def youtube_download(client, message): args = message.text.split(None, 1) if len(args) == 1: await edit_or_reply(message, text='missing [url] parameter!!') return url = args[1] opts = { 'format': 'best', 'addmetadata': True, 'key': 'FFmpegMetadata', 'writethumbnail': True, 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [ { 'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4', }, ], 'outtmpl': '%(id)s.mp4', 'logtostderr': False, 'quiet': True, } try: with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) except DownloadError as DE: await edit_or_reply(message, text=f'`{str(DE)}`') return except ContentTooShortError: await edit_or_reply( message, text='`The download content was too short.`', ) return except GeoRestrictedError: await edit_or_reply( message, text='`Video is not available.`', ) return except MaxDownloadsReached: await edit_or_reply( message, text='`Max-downloads limit has been reached.`', ) return except PostProcessingError: await edit_or_reply( message, text='`There was an error during post processing.`', ) return except UnavailableVideoError: await edit_or_reply( message, text='`Media is not available in the requested format.`', ) return except XAttrMetadataError as XAME: await edit_or_reply( message, text=f'`{XAME.code}: {XAME.msg}\n{XAME.reason}`', ) return except ExtractorError: await edit_or_reply( message, text='`There was an error during info extraction.`', ) return thumbnail = Path(f"{ytdl_data['id']}.jpg") c_time = time.time() try: await message.reply_video( f"{ytdl_data['id']}.mp4", supports_streaming=True, duration=ytdl_data['duration'], caption=ytdl_data['title'], thumb=thumbnail, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ), except FileNotFoundError: await message.reply_video( f"{ytdl_data['id']}.mp4", supports_streaming=True, duration=ytdl_data['duration'], caption=ytdl_data['title'], progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ), os.remove(f"{ytdl_data['id']}.mp4") if thumbnail: os.remove(thumbnail) await message.delete()
async def download_video(v_url): """ For .ytdl command, download media from YouTube and many other sites. """ url = v_url.pattern_match.group(2) type = v_url.pattern_match.group(1).lower() await v_url.edit("`Preparing to download...`") if type == "a": opts = { 'format': 'bestaudio', 'addmetadata': True, 'noplaylist': False, 'key': 'FFmpegMetadata', 'writethumbnail': True, 'embedthumbnail': True, 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '320', }], 'outtmpl': out_folder + '%(title)s.%(ext)s', 'quiet': True, 'logtostderr': False } video = False song = True elif type == "v": opts = { 'format': 'best', 'addmetadata': True, 'noplaylist': False, 'getthumbnail': True, 'embedthumbnail': True, 'xattrs': True, 'writethumbnail': True, 'key': 'FFmpegMetadata', 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [ { 'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4' }, ], 'outtmpl': out_folder + '%(title)s.%(ext)s', 'logtostderr': False, 'quiet': True } song = False video = True try: await v_url.edit("`Fetching playlist data, please wait..`") with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) # print(ytdl_data['thumbnail']) filename = sorted(get_lst_of_files(out_folder, [])) except DownloadError as DE: await v_url.edit(f"`{str(DE)}`") return except ContentTooShortError: await v_url.edit("`The download content was too short.`") return except GeoRestrictedError: await v_url.edit( "`Video is not available from your geographic location due to geographic restrictions imposed by a website.`" ) return except MaxDownloadsReached: await v_url.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await v_url.edit("`There was an error during post processing.`") return except UnavailableVideoError: await v_url.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await v_url.edit("`There was an error during info extraction.`") return except Exception as e: await v_url.edit(f"{str(type(e)): {str(e)}}") return c_time = time.time() await v_url.edit( "`YouTube Playlist Downloading Processing Now.\nPlease Wait!`") if song: for single_file in filename: if os.path.exists(single_file): caption_rts = os.path.basename(single_file) force_document = True supports_streaming = False document_attributes = [] if single_file.endswith((".mp4", ".mp3", ".flac", ".webm")): metadata = extractMetadata(createParser(single_file)) duration = 0 width = 0 height = 180 if metadata.has("duration"): duration = metadata.get('duration').seconds document_attributes = [ DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=False, supports_streaming=True, ) ] try: ytdl_data_name_audio = os.path.basename(single_file) print(ytdl_data_name_audio) file_path = single_file song_size = file_size(file_path) await v_url.client.send_file( v_url.chat_id, single_file, caption=f"`{ytdl_data_name_audio}`" + "\n" + f"{song_size}", force_document=force_document, supports_streaming=supports_streaming, allow_cache=False, reply_to=v_url.message.id, attributes=document_attributes, progress_callback=lambda d, t: asyncio. get_event_loop().create_task( progress(d, t, v_url, c_time, "Uploading..", f"{ytdl_data_name_audio}"))) # os.remove(thumb) except Exception as e: await v_url.client.send_message( v_url.chat_id, "{} caused `{}`".format(caption_rts, str(e)), ) continue os.remove(single_file) await asyncio.sleep(DELETE_TIMEOUT) await v_url.delete() shutil.rmtree(out_folder) if video: for single_file in filename: if os.path.exists(single_file): caption_rts = os.path.basename(single_file) force_document = False supports_streaming = True document_attributes = [] if single_file.endswith((".mp4", ".mp3", ".flac", ".webm")): metadata = extractMetadata(createParser(single_file)) duration = 0 width = 0 height = 0 if metadata.has("duration"): duration = metadata.get('duration').seconds document_attributes = [ DocumentAttributeVideo( duration=duration, w=width, h=height, round_message=False, supports_streaming=True, ) ] image_link = ytdl_data['thumbnail'] downloaded_image = wget.download(image_link, out_folder) thumb = downloaded_image file_path = single_file video_size = file_size(file_path) try: ytdl_data_name_video = os.path.basename(single_file) await v_url.client.send_file( v_url.chat_id, single_file, caption=f"`{ytdl_data_name_video}`" + "\n" + f"{video_size}", force_document=force_document, supports_streaming=supports_streaming, thumb=thumb, allow_cache=False, reply_to=v_url.message.id, attributes=document_attributes, progress_callback=lambda d, t: asyncio. get_event_loop().create_task( progress(d, t, v_url, c_time, "Uploading..", f"{ytdl_data_name_video}"))) # os.remove(thumb) except Exception as e: await v_url.client.send_message( v_url.chat_id, "{} caused `{}`".format(caption_rts, str(e)), ) continue os.remove(single_file) await asyncio.sleep(DELETE_TIMEOUT) await v_url.delete() shutil.rmtree(out_folder)
import pickle # with open(youtube_channel_id+'_1.data', 'wb') as fp: # pickle.dump(all_video_links, fp) with open(youtube_channel_id + '_1.data', 'rb') as fp: all_video_links = pickle.load(fp) print(all_video_links) # info_from_video = get_information_from_youtube_video("ZJER0vEtzd0") # print(info_from_video) # print(info_from_video['items'][0]['statistics']['viewCount']) from youtube_dl import YoutubeDL # video = all_video_links[0] # video = "https://www.youtube.com/watch?v=ZJER0vEtzd0" video = all_video_links[0][0] print("For: ", video) youtube_dl_opts = {'ignoreerrors': True, 'quiet': True} with YoutubeDL(youtube_dl_opts) as ydl: info_dict = ydl.extract_info(video, download=False) video_id = info_dict.get("id", None) video_views = info_dict.get("view_count", None) video_date = info_dict.get("upload_date", None) video_duration = info_dict.get("duration", None) video_title = info_dict.get('title', None) print(video_id, video_views, video_date, video_duration, video_title)
async def download_video(v_url): """ For .ytdl command, download media from YouTube and many other sites. """ reply = await v_url.get_reply_message() if v_url.pattern_match.group(2) != "": url = v_url.pattern_match.group(2) elif reply is not None: url = reply.message url = re.findall(r"\bhttps?://.*\.\S+", reply.message)[0] else: return type = (v_url.pattern_match.group(1).lower() if v_url.pattern_match.group(1) is not None else "a") out_folder = Config.TMP_DOWNLOAD_DIRECTORY + "youtubedl/" if not os.path.isdir(out_folder): os.makedirs(out_folder) await v_url.edit("`Preparing to download...`") if type == "a": opts = { "format": "bestaudio", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "embedthumbnail": True, "audioquality": 0, "audioformat": "mp3", "prefer_ffmpeg": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "320", }], "outtmpl": out_folder + "%(title)s.mp3", "quiet": True, "logtostderr": False, } video = False song = True elif type == "v": opts = { "format": "best", "addmetadata": True, "key": "FFmpegMetadata", "writethumbnail": True, "write_all_thumbnails": True, "embedthumbnail": True, "prefer_ffmpeg": True, "hls_prefer_native": True, "geo_bypass": True, "nocheckcertificate": True, "postprocessors": [{ "key": "FFmpegVideoConvertor", "preferedformat": "mp4" }], "outtmpl": out_folder + "%(title)s.mp4", "logtostderr": False, "quiet": True, } song = False video = True try: await v_url.edit("`Fetching data, please wait...`") with YoutubeDL(opts) as ytdl: ytdl_data = ytdl.extract_info(url) sorted(get_lst_of_files(out_folder, [])) except DownloadError as DE: await v_url.edit(f"`{str(DE)}`") return except ContentTooShortError: await v_url.edit("`The download content was too short.`") return except GeoRestrictedError: await v_url.edit( "`Video is not available from your geographic location due to geographic restrictions imposed by a website.`" ) return except MaxDownloadsReached: await v_url.edit("`Max-downloads limit has been reached.`") return except PostProcessingError: await v_url.edit("`There was an error during post processing.`") return except UnavailableVideoError: await v_url.edit("`Media is not available in the requested format.`") return except XAttrMetadataError as XAME: await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") return except ExtractorError: await v_url.edit("`There was an error during info extraction.`") return except Exception as e: await v_url.edit(f"{str(type(e)): {str(e)}}") return c_time = time.time() # cover_url = f"https://img.youtube.com/vi/{ytdl_data['id']}/0.jpg" # thumb_path = wget.download(cover_url, out_folder + "cover.jpg") # relevant_path = "./DOWNLOADS/youtubedl" # included_extensions = ["mp4","mp3"] # file_names = [fn for fn in os.listdir(relevant_path) # if any(fn.endswith(ext) for ext in included_extensions)] if song: relevant_path = f"./{Config.TMP_DOWNLOAD_DIRECTORY}/youtubedl" included_extensions = ["mp3"] file_names = [ fn for fn in os.listdir(relevant_path) if any( fn.endswith(ext) for ext in included_extensions) ] img_extensions = ["webp", "jpg", "jpeg"] img_filenames = [ fn_img for fn_img in os.listdir(relevant_path) if any( fn_img.endswith(ext_img) for ext_img in img_extensions) ] thumb_image = out_folder + img_filenames[0] # thumb = out_folder + "cover.jpg" file_path = out_folder + file_names[0] song_size = file_size(file_path) j = await v_url.edit(f"`Preparing to upload song:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, file_path, caption=ytdl_data["title"] + "\n" + f"`{song_size}`", supports_streaming=True, thumb=thumb_image, attributes=[ DocumentAttributeAudio( duration=int(ytdl_data["duration"]), title=str(ytdl_data["title"]), performer=str(ytdl_data["uploader"]), ) ], progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{ytdl_data['title']}.mp3")), ) # os.remove(file_path) await asyncio.sleep(DELETE_TIMEOUT) os.remove(thumb_image) await j.delete() elif video: relevant_path = f"./{Config.TMP_DOWNLOAD_DIRECTORY}/youtubedl" included_extensions = ["mp4"] file_names = [ fn for fn in os.listdir(relevant_path) if any( fn.endswith(ext) for ext in included_extensions) ] img_extensions = ["webp", "jpg", "jpeg"] img_filenames = [ fn_img for fn_img in os.listdir(relevant_path) if any( fn_img.endswith(ext_img) for ext_img in img_extensions) ] thumb_image = out_folder + img_filenames[0] file_path = out_folder + file_names[0] video_size = file_size(file_path) # thumb = out_folder + "cover.jpg" j = await v_url.edit(f"`Preparing to upload video:`\ \n**{ytdl_data['title']}**\ \nby *{ytdl_data['uploader']}*") await v_url.client.send_file( v_url.chat_id, file_path, supports_streaming=True, caption=ytdl_data["title"] + "\n" + f"`{video_size}`", thumb=thumb_image, progress_callback=lambda d, t: asyncio.get_event_loop(). create_task( progress(d, t, v_url, c_time, "Uploading..", f"{ytdl_data['title']}.mp4")), ) os.remove(file_path) await asyncio.sleep(DELETE_TIMEOUT) os.remove(thumb_image) await v_url.delete() await j.delete() shutil.rmtree(out_folder)
async def download_video(v_url): """ For .rip command, download media from YouTube and many other sites. """ url = v_url.pattern_match.group(2) type = v_url.pattern_match.group(1).lower() await v_url.edit("`Preparing to download...`") if type == "audio": opts = { 'format': 'bestaudio', 'addmetadata': True, 'key': 'FFmpegMetadata', 'writethumbnail': True, 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '320', }], 'outtmpl': '%(id)s.mp3', 'quiet': True, 'logtostderr': False } video = False song = True elif type == "video": opts = { 'format': 'best', 'addmetadata': True, 'key': 'FFmpegMetadata', 'prefer_ffmpeg': True, 'geo_bypass': True, 'nocheckcertificate': True, 'postprocessors': [{ 'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4' }], 'outtmpl': '%(id)s.mp4', 'logtostderr': False, 'quiet': True } song = False video = True try: await v_url.edit("`Fetching data, please wait..`") with YoutubeDL(opts) as rip: rip_data = rip.extract_info(url) except DownloadError as DE: return await v_url.edit(f"`{str(DE)}`") except ContentTooShortError: return await v_url.edit("`The download content was too short.`") except GeoRestrictedError: return await v_url.edit( "`Video is not available from your geographic location " "due to geographic restrictions imposed by a website.`" ) except MaxDownloadsReached: return await v_url.edit("`Max-downloads limit has been reached.`") except PostProcessingError: return await v_url.edit("`There was an error during post processing.`") except UnavailableVideoError: return await v_url.edit("`Media is not available in the requested format.`") except XAttrMetadataError as XAME: return await v_url.edit(f"`{XAME.code}: {XAME.msg}\n{XAME.reason}`") except ExtractorError: return await v_url.edit("`There was an error during info extraction.`") except Exception as e: return await v_url.edit(f"{str(type(e)): {str(e)}}") c_time = time.time() if song: await v_url.edit( f"`Preparing to upload song:`\n**{rip_data['title']}**") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp3", supports_streaming=True, attributes=[ DocumentAttributeAudio(duration=int(rip_data['duration']), title=str(rip_data['title']), performer=str(rip_data['uploader'])) ], progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp3"))) os.remove(f"{rip_data['id']}.mp3") await v_url.delete() elif video: await v_url.edit( f"`Preparing to upload video:`\n**{rip_data['title']}**") await v_url.client.send_file( v_url.chat_id, f"{rip_data['id']}.mp4", supports_streaming=True, caption=rip_data['title'], progress_callback=lambda d, t: asyncio.get_event_loop( ).create_task( progress(d, t, v_url, c_time, "Uploading..", f"{rip_data['title']}.mp4"))) os.remove(f"{rip_data['id']}.mp4") await v_url.delete()
def download_video(url): with YoutubeDL({'format':'136'}) as ydl: return ydl.download([url])