Showing progress in pytube - python

import pytube
def video_downloader():
vid_url=str(input("Enter Video URL: "))
print('Connecting, Please wait...')
video=pytube.YouTube(vid_url)
Streams=video.streams
File_name=input('File Name:')
Format=input('Audio Or Video :')
if Format=='Audio':
Filter=Streams.get_audio_only(subtype='mp4')
if Format=='Video':
Filter=Streams.get_highest_resolution()
print('Now downloading:',video.title)
sizer=round(Filter.filesize/1000000)
print('Size:',sizer,'MB')
Filter.download(filename=str(File_name))
print('Done!')
video_downloader()
This is a script I've made recently to download video and audio files from youtube using pytube, but I'm having a hard time trying to add a function or something that could show the user the progress of the download.
i.e: 1%complete 2%complete etc
Any help would be appreciated :)

This is my First Time Answering, apologizes for mistakes.
Reading Pytube Documentation, one may notice that pytube have this option already implemented as a Progress Bar, You will need to call on_progress_callback in your YouTube Object.
from pytube.cli import on_progress
from pytube import YouTube
yt = YouTube(video_url, on_progress_callback=on_progress)
yt.download()

pytubeenter code herefrom pytube.cli import on_progress
from pytube import YouTube as YT
yt =YT ("https://",on_progress_callback=on_progress)
yt.streams.get_highest_resolution().download(output_path="/Users/")
↳ |██████████████████ | 17.

Related

when I'm try to download video using pytube but its not woking

Unable to download video using pytube
import customtkinter
from pytube import YouTube
def startDownload():
try:
ytLink = link.get()
YouTube(ytLink).streams.get_highest_resolution().download()
except:
print("YouTube link is invalid")
print("Download Complete!")
#Link input
url_var = tkinter.StringVar()
link = customtkinter.CTkEntry(app, width=400, height=40, textvariable=url_var)
link.pack()
#Download button
download = customtkinter.CTkButton(app, text="Download", command=startDownload)
download.pack(padx=10, pady=10)
error in Line number 6. input take but download function not work
output - YouTube link is invalid
Download Complete!
from pytube import Youtube
That's because you are downloading the first one of the available streams which is usually 720p. To download a 360p resolution stream, you can do:
YouTube('https://youtu.be/2lAe1cqCOXo').streams.filter(res="360p").first().download()
Note: this is YouTube, not Youtube.
Short explanation: You need to use filter() to choose a specific resolution you want to download. For example, if you call:
yt = YouTube('https://youtu.be/2lAe1cqCOXo')
it returns the available stream to yt. You can view all the streams by typing:
yt.streams
You can filter which type of filter you want. To filter only 360p streams you can write:
yt.streams.filter(res="360p")
To filter only 360p streams and download the first one type this:
yt.streams.filter(res="360p").first().download()

I found a Key error of 'start' when using pytube.caption

I am trying to download Youtube captions by pytube.
Everything was fine and I managed to download the video and its caption by xml_captions.
However, when I tried to covert it into .srt format, I got a key error.
---> 83 start = float(child.attrib["start"])
KeyError: 'start'
I wonder what was wrong.
My code is
pip install pytube
from pytube import YouTube
# misc``
import os
import shutil
import math
import datetime
video=YouTube('https://www.youtube.com/watch?v=xxydY73V9bQ')
caption = video.captions['a.en']
caption.xml_captions
srt_format = caption.xml_caption_to_srt(caption.xml_captions)
If sill relevant: tt appears that YouTube has changed the way captions are handled. Here's a discussion with possible solutions.

How to keep google open using Python webbrowser?

I'm trying to open several google pages through python webbrowser, and I've gotten it to work, but after opening all the pages, google simply closes unless I manually click it. Why is this happening? Help is appreciated, and thank in advance.
import pyautogui as py
import keyboard as key
import time
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome',None, webbrowser.BackgroundBrowser(chrome_path))
def n_t():
py.hotkey('ctrl', 't')
def m_t():
py.hotkey('ctrl', 'tab')
def gogle():
webbrowser.get('chrome').open('gmail.com')
py.press('enter')
time.sleep(5)
n_t()
py.write('www.youtube.com')
py.press('enter')
n_t()
py.write('hangouts.google.com/authuser=2')
py.press('enter')
n_t()
py.write('stackoverflow.com')
py.press('enter')
m_t()
gogle()
Use this:
import webbrowser
webbrowser.open('https://google.com')
webbrowser.open('https://gmail.com')
webbrowser.open('https://youtube.com')
webbrowser.open('https://stackoverflow.com')
and you're good to go.

Pytube how to add a progress bar?

I don't know how to do it and whenever i try solutions from other topics about this question i get errors.
Mostly "TypeError: show_progress_bar() missing 1 required positional argument: 'bytes_remaining'".
from pytube import YouTube
#took this def from another topic
def progress_function(stream, chunk, file_handle, bytes_remaining):
percent = round((1-bytes_remaining/video.filesize)*100)
if( percent%10 == 0):
print(percent, 'done...')
url = "Any youtube url"
yt = YouTube(url, on_progress_callback=progress_function)
yt.streams[0].download()
for example when i run this exact code it gives me that error.
I really can't comprehend its logic. I also searched the docs from pytube3 website but i can't solve this. Pls help me. Thanks.
Remove the stream then it will work, recently I tried to develop similar logic facing a similar error.
Here is the code that worked for me:
def progress(chunk, file_handle, bytes_remaining):
global filesize
remaining = (100 * bytes_remaining) / filesize
step = 100 - int(remaining)
print("Completed:", step) # show the percentage of completed download
The filesize can be retrived, once you select which video or audio to download such as
yt = YouTube(str(link), on_progress_callback=progress) # Declare YouTube
yt1 = yt.streams.get_by_itag(int(itag)) # itag is given when you list all the streams of a youtube video
filesize = yt1.filesize
Hope this helps!

How to add tqdm to show progress bar when downloading you tube video with pytube?

I am learning pytube to download Youtube video and tried tqdm on top of it to show progress bar but it shows various error and also I could not understand what is happening when I download video with pytube and showing progress bar which is the reason for me to not able to add tqdm in it.
The code I have written with pytube runs well, this is the code:
from pytube import YouTube
url = str(input("Enter the video link: "))
yt = YouTube(url)
videos = yt.streams.filter(file_extension='mp4').all()
filename = yt.title
s = 1
for v in videos:
print(str(s)+". "+str(v))
s += 1
n = int(input("Enter the number of the video: "))
vid = videos[n-1]
vid.download("C:/Users/user/Downloads/")
print(yt.title,"\nHas been successfully downloaded")
I need tqdm to be added to the code to show a progress bar.
I don't know about tqdm, but there is a progress bar feature for pytube.
I use it like this:
from pytube.cli import on_progress
from pytube import YouTube as YT
...
yt = YT(video_url, on_progress_callback=on_progress)
yt.streams\
.filter(file_extension='mp4')\
.get_lowest_resolution()\
.download(video_path)
Looks like this:
PSY - GANGNAM STYLE(강남스타일) MV.mp4
↳ |███████████████████████████████████████| 100.0%
Hope it helps!

Categories