def youtubedl(message):
    args = extract_args(message).split(' ', 2)

    if len(args) != 2:
        edit(message, f'`{get_translation("wrongCommand")}`')
        return

    util = args[0].lower()
    url = args[1]

    try:
        video_info = YoutubeDL().extract_info(url, False)
    except DownloadError as e:
        return edit(message, get_translation('banError', ['`', '**', e]))

    title = video_info.get('title')
    uploader = video_info.get('uploader')

    if util == 'mp4':
        edit(message, get_translation('downloadYTVideo', ['**', title, '`']))
        ydl_opts = {
            'outtmpl': f'{title}.%(ext)s',
            'format':
            'bestvideo[ext=mp4][height<=?1080]+bestaudio[ext=mp3]/best',
        }
        with YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
        edit(message, f'`{get_translation("uploadMedia")}`')
        reply_video(
            message,
            f'{title}.mp4',
            caption=
            f"**{get_translation('videoTitle')}** `{title}`\n**{get_translation('videoUploader')}** `{uploader}`",
            delete_orig=True,
            delete_file=True,
        )

    elif util == 'mp3':
        edit(message, get_translation('downloadYTAudio', ['**', title, '`']))
        ydl_opts = {
            'outtmpl':
            f'{title}.%(ext)s',
            'format':
            'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '320',
            }],
        }
        with YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
        edit(message, f'`{get_translation("uploadMedia")}`')
        reply_audio(
            message,
            f'{title}.mp3',
            caption=f"**{get_translation('videoUploader')}** `{uploader}`",
            delete_orig=True,
            delete_file=True,
        )
def earrape(message):
    args = extract_args(message).split(' ', 1)
    reply = message.reply_to_message

    util = args[0].lower()
    if util == 'mp4':
        if not (reply.video or reply.video_note or
                (reply.document and 'video' in reply.document.mime_type)):
            edit(message, f'`{get_translation("wrongMedia")}`')
        else:
            edit(message, f'`{get_translation("applyEarrape")}`')
            media = download_media_wc(reply)
            process = Popen([
                'ffmpeg',
                '-i',
                f'{media}',
                '-af',
                'acrusher=.1:1:64:0:log',
                f'{media}.mp4',
            ])
            process.communicate()
            edit(message, f'`{get_translation("uploadMedia")}`')
            reply_video(
                reply if reply else message,
                f'{media}.mp4',
                delete_file=True,
            )
            remove(media)
            message.delete()
    elif util == 'mp3':
        if not (reply.video or reply.video_note or
                (reply.audio or reply.voice or
                 (reply.document and 'video' in reply.document.mime_type))):
            edit(message, f'`{get_translation("wrongMedia")}`')
        else:
            edit(message, f'`{get_translation("applyEarrape")}`')
            media = download_media_wc(reply)
            process = Popen([
                'ffmpeg',
                '-i',
                f'{media}',
                '-af',
                'acrusher=.1:1:64:0:log',
                f'{media}.mp3',
            ])
            process.communicate()
            edit(message, f'`{get_translation("uploadMedia")}`')
            reply_voice(
                reply if reply else message,
                f'{media}.mp3',
                delete_file=True,
            )
            remove(media)
            message.delete()
    else:
        edit(message, f'`{get_translation("wrongCommand")}`')
        return
Beispiel #3
0
def youtubedl(message):
    args = extract_args(message).split(' ', 2)

    if len(args) != 2:
        edit(message, f'`{get_translation("wrongCommand")}`')
        return

    util = args[0].lower()
    url = args[1]

    if util == 'mp4':
        ydl_opts = {
            'outtmpl': f'%(id)s.%(ext)s',
            'format': 'bestvideo[ext=mp4][height<=?1080]+bestaudio[ext=m4a]/best',
            'addmetadata': True,
            'prefer_ffmpeg': True,
            'geo_bypass': True,
            'nocheckcertificate': True,
            'postprocessors': [
                {'key': 'FFmpegMetadata'},
                {'key': 'FFmpegVideoConvertor', 'preferedformat': 'mp4'},
            ],
            'quiet': True,
            'logtostderr': False,
        }
        thumb_path = None
        with YoutubeDL(ydl_opts) as ydl:
            try:
                video_info = ydl.extract_info(url, False)
                title = video_info['title']
                uploader = video_info['uploader']
                duration = video_info['duration']
            except KeyError:
                uploader = get_translation('notFound')
                duration = None
            except BaseException as e:
                return edit(message, get_translation('banError', ['`', '**', e]))

            edit(message, get_translation('downloadYTVideo', ['**', title, '`']))

            try:
                temp = BytesIO()
                with get(video_info['thumbnail']) as req:
                    temp.name = 'file.webp'
                    temp.write(req.content)
                    temp.seek(0)
                im = Image.open(temp)
                imc = im.convert('RGB')
                imc.save(thumb_path := f'{get_download_dir()}/{video_info["id"]}.jpg')
            except BaseException:
                thumb_path = None

            ydl.download([url])

        edit(message, f'`{get_translation("uploadMedia")}`')
        reply_video(
            message,
            f'{video_info["id"]}.mp4',
            thumb=thumb_path,
            caption=f"**{get_translation('videoTitle')}** `{title}`\n**{get_translation('videoUploader')}** `{uploader}`",
            duration=duration,
            delete_orig=True,
            delete_file=True,
        )
        try:
            remove(thumb_path)
        except BaseException:
            pass

    elif util == 'mp3':
        ydl_opts = {
            'outtmpl': f'%(title)s.%(ext)s',
            'format': 'bestaudio/best',
            'addmetadata': True,
            'writethumbnail': True,
            'prefer_ffmpeg': True,
            "extractaudio": True,
            'geo_bypass': True,
            'nocheckcertificate': True,
            'postprocessors': [
                {
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '320',
                },
                {'key': 'EmbedThumbnail'},
                {'key': 'FFmpegMetadata'},
            ],
            'quiet': True,
            'logtostderr': False,
        }
        with YoutubeDL(ydl_opts) as ydl:
            try:
                video_info = ydl.extract_info(url, False)
                title = video_info['title']
                uploader = video_info['uploader']
                duration = int(video_info['duration'])
            except KeyError:
                uploader = get_translation('notFound')
                duration = None
            except BaseException as e:
                return edit(message, get_translation('banError', ['`', '**', e]))

            edit(message, get_translation('downloadYTAudio', ['**', title, '`']))

            ydl.download([url])
        edit(message, f'`{get_translation("uploadMedia")}`')
        reply_audio(
            message,
            f'{title}.mp3',
            caption=f"**{get_translation('videoUploader')}** `{uploader}`",
            duration=duration,
            delete_orig=True,
            delete_file=True,
        )
Beispiel #4
0
    def progress(current, total):
        if (curr_posix := time()) - TEMP_SETTINGS[f'upload_{posix}'] > 5:
            TEMP_SETTINGS[f'upload_{posix}'] = curr_posix
            edit(
                message,
                get_translation(
                    'updownUpload',
                    ['`', '(½{:.2f})'.format(current * 100 / total), args],
                ),
            )

    if isfile(args):
        try:
            edit(message, get_translation('updownUpload', ['`', '', args]))
            if args.endswith('.mp4'):
                reply_video(message, args, progress=progress)
            else:
                reply_doc(message, args, progress=progress)
            edit(message, f'`{get_translation("uploadFinish")}`')
        except Exception as e:
            edit(message, f'`{get_translation("uploadError")}`')
            raise e

        del TEMP_SETTINGS[f'upload_{posix}']
        return

    edit(message, f'`{get_translation("uploadFileError")}`')
    del TEMP_SETTINGS[f'upload_{posix}']


HELP.update({'download': get_translation('uploadInfo')})