Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Add a progress bar for the downloads #9

Closed
Flowrey opened this issue Aug 19, 2023 · 5 comments
Closed

Add a progress bar for the downloads #9

Flowrey opened this issue Aug 19, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Flowrey
Copy link
Owner

Flowrey commented Aug 19, 2023

Problem to solve

Currently, there is no indication of the progression of a download

Proposal

Add a progress bar to see the progression

@Flowrey Flowrey added enhancement New feature or request good first issue Good for newcomers labels Aug 19, 2023
@yr1404
Copy link
Contributor

yr1404 commented Aug 20, 2023

I would like to give it a try. Can you assign this issue to me?

@Flowrey
Copy link
Owner Author

Flowrey commented Aug 20, 2023

Yeah, sure !

@yr1404
Copy link
Contributor

yr1404 commented Sep 9, 2023

Code Details

I have used tqdm for adding progress bar

from tqdm import tqdm

Added a pbar global variable

global pbar
    pbar = tqdm(total=len(results))
    
    # Run download in thread pool to avoid blocking IO
    for result in results:
        if result:
            loop = asyncio.get_running_loop()
            loop.run_in_executor(
                None, download_video_audio, result["title"], result["id"], destination
            )

    pbar.close()

And, updating the pbar here:

def download_video_audio(title: str, video_id: str, destination: Optional[str] = None):
    """Download audio of a YouTube video."""
    print("[Downloading] {} : {}".format(title, video_id))
    if stream := pytube.YouTube(
        f"http://youtube.com/watch?v={video_id}"
    ).streams.get_audio_only():
        stream.download(output_path=destination)
    print("[Downloaded] {}".format(title))
    pbar.update(1)

I'm encountering an issue where multiple progress bars are being displayed.
Can you give me a heads up at where I might be approaching wrong?

@Flowrey
Copy link
Owner Author

Flowrey commented Sep 9, 2023

Hey, i think the global here is what make you have multiple progress bars, we don't need one.
The idea is to update the progress bar when a Future completed.

You would need to use the asyncio module of tqdm, tqdm.asyncio.
And with that you should be able to do something like this:

from tqdm.asyncio import tqdm
   loop = asyncio.get_running_loop()

    # Run download in thread pool to avoid blocking IO
    tasks = []
    for result in results:
        if result:
            tasks.append(loop.run_in_executor(
                None, download_video_audio, result["title"], result["id"], destination
            ))

    for f in tqdm.as_completed(tasks):
        await f

@Flowrey
Copy link
Owner Author

Flowrey commented Sep 15, 2023

Fixed by #26

@Flowrey Flowrey closed this as completed Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants