Beispiel #1
0
def download_urls(urls,
                  title,
                  ext,
                  total_size,
                  output_dir='.',
                  refer=None,
                  merge=True,
                  faker=False,
                  headers={},
                  thread=0,
                  **kwargs):
    assert urls
    if json_output:
        json_output_.download_urls(urls=urls,
                                   title=title,
                                   ext=ext,
                                   total_size=total_size,
                                   refer=refer)
        return
    if dry_run:
        print('Real URLs:\n{}'.format('\n'.join(urls)))
        return

    if player:
        launch_player(player, urls, refer=refer)
        return

    if not total_size:
        try:
            total_size = urls_size(urls, faker=faker, headers=headers)
        except Exception:
            import traceback
            traceback.print_exc(file=sys.stdout)
            pass

    title = tr(get_filename(title))
    output_filename = get_output_filename(urls, title, ext, output_dir, merge)
    output_filepath = os.path.join(output_dir, output_filename)

    if total_size:
        if not force and os.path.exists(output_filepath) \
                and os.path.getsize(output_filepath) >= total_size * 0.9:
            print('Skipping {}: file already exists'.format(output_filepath))
            print()
            return
        bar = SimpleProgressBar(total_size, len(urls))
    else:
        bar = PiecesProgressBar(total_size, len(urls))

    if len(urls) == 1:
        url = urls[0]
        print('Downloading {} ...'.format(tr(output_filename)))
        bar.update()
        url_save(url,
                 output_filepath,
                 bar,
                 refer=refer,
                 faker=faker,
                 headers=headers,
                 **kwargs)
        bar.done()
    else:
        print('Downloading {}.{} ...'.format(tr(title), ext))
        parts = [''] * len(urls)
        bar.update()
        piece = 0

        def _download(url):
            # Closure
            nonlocal piece
            index = urls.index(url)
            filename = '{}[{:0>2d}].{}'.format(title, index, ext)
            filepath = os.path.join(output_dir, filename)
            parts[index] = filepath  # 防止多线程环境下文件顺序会乱
            piece += 1
            bar.update_piece(piece)
            url_save(url,
                     filepath,
                     bar,
                     refer=refer,
                     is_part=True,
                     faker=faker,
                     headers=headers,
                     **kwargs)

        if thread:
            with Pool(processes=thread) as pool:
                pool.map(_download, urls)
        else:
            for url in urls:
                _download(url)
        bar.done()

        if not merge:
            print()
            return

        if 'av' in kwargs and kwargs['av']:
            from .processor.ffmpeg import has_ffmpeg_installed
            if has_ffmpeg_installed():
                from .processor.ffmpeg import ffmpeg_concat_av
                ret = ffmpeg_concat_av(parts, output_filepath, ext)
                print('Merged into {}'.format(output_filename))
                if ret == 0:
                    for part in parts:
                        os.remove(part)

        elif ext in ['flv', 'f4v']:
            try:
                from .processor.ffmpeg import has_ffmpeg_installed
                if has_ffmpeg_installed():
                    from .processor.ffmpeg import ffmpeg_concat_flv_to_mp4
                    ffmpeg_concat_flv_to_mp4(parts, output_filepath)
                else:
                    from .processor.join_flv import concat_flv
                    concat_flv(parts, output_filepath)
                print('Merged into {}'.format(output_filename))
            except Exception:
                raise
            else:
                for part in parts:
                    os.remove(part)

        elif ext == 'mp4':
            try:
                from .processor.ffmpeg import has_ffmpeg_installed
                if has_ffmpeg_installed():
                    from .processor.ffmpeg import ffmpeg_concat_mp4_to_mp4
                    ffmpeg_concat_mp4_to_mp4(parts, output_filepath)
                else:
                    from .processor.join_mp4 import concat_mp4
                    concat_mp4(parts, output_filepath)
                print('Merged into {}'.format(output_filename))
            except Exception:
                raise
            else:
                for part in parts:
                    os.remove(part)

        elif ext == 'ts':
            try:
                from .processor.ffmpeg import has_ffmpeg_installed
                if has_ffmpeg_installed():
                    from .processor.ffmpeg import ffmpeg_concat_ts_to_mkv
                    ffmpeg_concat_ts_to_mkv(parts, output_filepath)
                else:
                    from .processor.join_ts import concat_ts
                    concat_ts(parts, output_filepath)
                print('Merged into {}'.format(output_filename))
            except Exception:
                raise
            else:
                for part in parts:
                    os.remove(part)

        else:
            print("Can't merge {} files".format(ext))

    print()
Beispiel #2
0
def download_urls(urls,
                  title,
                  ext,
                  total_size,
                  output_dir='.',
                  refer=None,
                  merge=True,
                  faker=False,
                  headers={},
                  **kwargs):
    assert urls
    if json_output:
        json_output_.download_urls(urls=urls,
                                   title=title,
                                   ext=ext,
                                   total_size=total_size,
                                   refer=refer)
        return
    if dry_run:
        print('Real URLs:\n%s' % '\n'.join(urls))
        return

    if player:
        launch_player(player, urls)
        return

    if not total_size:
        try:
            total_size = urls_size(urls, faker=faker, headers=headers)
        except Exception:
            import traceback
            traceback.print_exc(file=sys.stdout)
            pass

    title = tr(get_filename(title))
    output_filename = get_output_filename(urls, title, ext, output_dir, merge)
    output_filepath = os.path.join(output_dir, output_filename)

    if total_size:
        if not force and os.path.exists(output_filepath) \
                and os.path.getsize(output_filepath) >= total_size * 0.9:
            print('Skipping %s: file already exists' % output_filepath)
            print()
            return
        bar = SimpleProgressBar(total_size, len(urls))
    else:
        bar = PiecesProgressBar(total_size, len(urls))

    if len(urls) == 1:
        url = urls[0]
        print('Downloading %s ...' % tr(output_filename))
        bar.update()
        url_save(url,
                 output_filepath,
                 bar,
                 refer=refer,
                 faker=faker,
                 headers=headers,
                 **kwargs)
        bar.done()
    else:
        parts = []
        print('Downloading %s.%s ...' % (tr(title), ext))
        bar.update()
        for i, url in enumerate(urls):
            filename = '%s[%02d].%s' % (title, i, ext)
            filepath = os.path.join(output_dir, filename)
            parts.append(filepath)
            bar.update_piece(i + 1)
            url_save(url,
                     filepath,
                     bar,
                     refer=refer,
                     is_part=True,
                     faker=faker,
                     headers=headers,
                     **kwargs)
        bar.done()

        if not merge:
            print()
            return

        if 'av' in kwargs and kwargs['av']:
            from .processor.ffmpeg import has_ffmpeg_installed
            if has_ffmpeg_installed():
                from .processor.ffmpeg import ffmpeg_concat_av
                ret = ffmpeg_concat_av(parts, output_filepath, ext)
                print('Merged into %s' % output_filename)
                if ret == 0:
                    for part in parts:
                        os.remove(part)

        elif ext in ['flv', 'f4v']:
            try:
                from .processor.ffmpeg import has_ffmpeg_installed
                if has_ffmpeg_installed():
                    from .processor.ffmpeg import ffmpeg_concat_flv_to_mp4
                    ffmpeg_concat_flv_to_mp4(parts, output_filepath)
                else:
                    from .processor.join_flv import concat_flv
                    concat_flv(parts, output_filepath)
                print('Merged into %s' % output_filename)
            except Exception:
                raise
            else:
                for part in parts:
                    os.remove(part)

        elif ext == 'mp4':
            try:
                from .processor.ffmpeg import has_ffmpeg_installed
                if has_ffmpeg_installed():
                    from .processor.ffmpeg import ffmpeg_concat_mp4_to_mp4
                    ffmpeg_concat_mp4_to_mp4(parts, output_filepath)
                else:
                    from .processor.join_mp4 import concat_mp4
                    concat_mp4(parts, output_filepath)
                print('Merged into %s' % output_filename)
            except Exception:
                raise
            else:
                for part in parts:
                    os.remove(part)

        elif ext == 'ts':
            try:
                from .processor.ffmpeg import has_ffmpeg_installed
                if has_ffmpeg_installed():
                    from .processor.ffmpeg import ffmpeg_concat_ts_to_mkv
                    ffmpeg_concat_ts_to_mkv(parts, output_filepath)
                else:
                    from .processor.join_ts import concat_ts
                    concat_ts(parts, output_filepath)
                print('Merged into %s' % output_filename)
            except Exception:
                raise
            else:
                for part in parts:
                    os.remove(part)

        else:
            print("Can't merge %s files" % ext)

    print()