Exemple #1
0
 def init(self):
     self.session = Session()
     name = ytdl.get_extractor_name(self.url)
     self.print_('extractor: {}'.format(name))
     if name == 'generic':
         raise NotImplementedError()
Exemple #2
0
def _get_video(url, session, cw, ie_key=None, allow_m3u8=True):
    print_ = get_print(cw)
    print_('get_video: {}, {}'.format(allow_m3u8, url))
    options = {
        'noplaylist': True,
        #'extract_flat': True,
        'playlistend': 1,
    }
    ydl = ytdl.YoutubeDL(options, cw=cw)
    info = ydl.extract_info(url)
    if not ie_key:
        ie_key = ytdl.get_extractor_name(url)
    info['ie_key'] = ie_key
    url_new = info.get('url')
    print('url: {} -> {}'.format(url, url_new))
    formats = info.get('formats', [])
    print(info.keys())

    if not formats and (info.get('entries') or 'title' not in info):
        if 'entries' in info:
            entry = info['entries'][0]
            url_new = entry.get('url') or entry['webpage_url']
        if url_new != url:
            return get_video(url_new, session, cw, ie_key=get_ie_key(info))

    session.headers.update(info.get('http_headers', {}))
    #session.cookies.update(ydl.cookiejar)

    if not formats:
        print('no formats')
        if url_new:
            f = {'url': url_new, 'format': ''}
            formats.append(f)

    fs = []
    for i, f in enumerate(formats):
        f['_index'] = i
        f['_resolution'] = f.get('vbr') or int_or_none(
            re.find(
                '([0-9]+)p', f['format'],
                re.IGNORECASE)) or f.get('height') or f.get('width') or int(
                    f.get('vcodec', 'none') != 'none')
        f['_audio'] = f.get('abr') or f.get('asr') or int(
            f.get('acodec', 'none') != 'none')
        print_(format_(f))
        fs.append(f)

    if not fs:
        raise Exception('No videos')

    def filter_f(fs):
        for f in fs:
            if allow_m3u8:
                return f
            ext = get_ext_(f['url'], session, url)
            if ext.lower() != '.m3u8':
                return f
            print_('invalid url: {}'.format(f['url']))
        return list(fs)[0]  #

    f_video = filter_f(
        reversed(sorted(fs, key=lambda f: (f['_resolution'], f['_index']))))
    print_('video0: {}'.format(format_(f_video)))

    if f_video['_audio']:
        f_audio = None
    else:
        fs_audio = sorted([
            f_audio for f_audio in fs
            if (not f_audio['_resolution'] and f_audio['_audio'])
        ],
                          key=lambda f: (f['_audio'], f['_index']))
        if fs_audio:
            f_audio = fs_audio[-1]
        else:
            try:
                print_('trying to get f_video with audio')
                f_video = filter_f(
                    reversed(
                        sorted([f for f in fs if f['_audio']],
                               key=lambda f: (f['_resolution'], f['_index']))))
            except Exception as e:
                print_('failed to get f_video with audio: {}'.format(e))
            f_audio = None
    print_('video: {}'.format(format_(f_video)))
    print_('audio: {}'.format(format_(f_audio)))
    video = Video(f_video, f_audio, info, session, url, cw=cw)

    return video
Exemple #3
0
def get_video(url, session, cw, ie_key=None):
    print_ = get_print(cw)
    options = {
        'noplaylist': True,
        #'extract_flat': True,
        'playlistend': 1,
    }

    ydl = ytdl.YoutubeDL(options)
    info = ydl.extract_info(url)
    if not ie_key:
        ie_key = ytdl.get_extractor_name(url)
    info['ie_key'] = ie_key
    url_new = info.get('url')
    print('url: {} -> {}'.format(url, url_new))
    formats = info.get('formats', [])
    print(info.keys())

    if not formats and (info.get('entries') or 'title' not in info):
        if 'entries' in info:
            entry = info['entries'][0]
            url_new = entry.get('url') or entry['webpage_url']
        if url_new != url:
            return get_video(url_new, session, cw, ie_key=get_ie_key(info))

    session.headers.update(info.get('http_headers', {}))
    #session.cookies.update(ydl.cookiejar)

    if not formats:
        print('no formats')
        if url_new:
            f = {'url': url_new, 'format': ''}
            formats.append(f)

    fs = []
    for i, f in enumerate(formats):
        f['_index'] = i
        f['_resolution'] = f.get('vbr') or int_or_none(
            re.find(
                '([0-9]+)p', f['format'],
                re.IGNORECASE)) or f.get('height') or f.get('width') or int(
                    f.get('vcodec', 'none') != 'none')
        f['_audio'] = f.get('abr') or f.get('asr') or int(
            f.get('acodec', 'none') != 'none')
        print_(format_(f))
        fs.append(f)

    if not fs:
        raise Exception('No videos')

    f = sorted(fs, key=lambda f: (f['_resolution'], f['_index']))[-1]
    if f['_audio']:
        f_audio = None
    else:
        fs_audio = sorted([
            f_audio for f_audio in fs
            if (not f_audio['_resolution'] and f_audio['_audio'])
        ],
                          key=lambda f: (f['_audio'], f['_index']))
        if fs_audio:
            f_audio = fs_audio[-1]
        else:
            try:
                f = sorted([f for f in fs if f['_audio']],
                           key=lambda f: (f['_resolution'], f['_index']))[-1]
            except IndexError:
                pass
            f_audio = None
    print_('video: {}'.format(format_(f)))
    print_('audio: {}'.format(format_(f_audio)))
    video = Video(f, f_audio, info, session, url, cw=cw)

    return video