I am using Python 3.8.5.
#pip freeze
beautifulsoup4==4.9.1
bs4==0.0.1
packaging==20.4
pyparsing==2.4.7
PyQt5==5.15.0
PyQt5-sip==12.8.0
PyQtWebEngine==5.15.0
pytube3==9.6.4
sip==5.3.0
six==1.15.0
soupsieve==2.0.1
toml==0.10.1
typing-extensions==3.7.4.2
CODE
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
print(video_url)
playlist.download_all()
WARNING
Number of videos in playlist: 0 playlist.py:24: DeprecationWarning:
Call to deprecated function download_all (This function will be
removed in the future. Please iterate through .videos).
playlist.download_all() /media/hophoet/Nouveau
nom/Projects/python/workspace/automates/autovideo/venv/lib/python3.8/site-packages/pytube/contrib/playlist.py:216:
DeprecationWarning: Call to deprecated function
_path_num_prefix_generator (This function will be removed in the future.).
Based on some research it seems that there is problem between pytube3==9.6.4 and YouTube's HTML code. The issue is related to how pytube3 reads a YouTube URL. The code below solves this issue by using a regex, which matches YouTube's updated HTML code.
import re
from pytube import Playlist
playlist = Playlist("https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
###############################
OUTPUT
###############################
Number of videos in playlist: 23
https://www.youtube.com/watch?v=HjuHHI60s44
https://www.youtube.com/watch?v=Z40N7b9NHTE
https://www.youtube.com/watch?v=FvziRqkLrEU
https://www.youtube.com/watch?v=XN2-87haa8k
https://www.youtube.com/watch?v=VgI4UKyL0Lc
https://www.youtube.com/watch?v=BvPIgm2SMG8
https://www.youtube.com/watch?v=DpdmUmglPBA
https://www.youtube.com/watch?v=BmVmJi5dR9c
https://www.youtube.com/watch?v=pYNuKXjcriM
https://www.youtube.com/watch?v=EWONqLqSxYc
https://www.youtube.com/watch?v=EKmLXiA4zaQ
https://www.youtube.com/watch?v=-DHCm9AlXvo
https://www.youtube.com/watch?v=7cRaGaIZQlo
https://www.youtube.com/watch?v=ZkcEB96iMFk
https://www.youtube.com/watch?v=5Fcf-8LPvws
https://www.youtube.com/watch?v=xWLgdSgsBFo
https://www.youtube.com/watch?v=QcKYFEgfV-I
https://www.youtube.com/watch?v=BtSQIxDPnLc
https://www.youtube.com/watch?v=O5kh_-6e4kk
https://www.youtube.com/watch?v=RuWVDz-48-o
https://www.youtube.com/watch?v=-yjc5Y7Wbmw
https://www.youtube.com/watch?v=C5T59WsrNCU
https://www.youtube.com/watch?v=MWldNGdX9zE
I did note that pytube3 is currently not being supported and that someone forked it to pytubeX. I'm trying to figure out the download, because I cannot get that piece to work with pytube3 or pytubex. I will keep looking at this issue.
Related
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 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.
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.
I am following the tutorial found here https://www.geeksforgeeks.org/youtube-data-api-set-1/. After I run the below code, I am getting a "No module named 'apiclient'" error. I also tried using "from googleapiclient import discovery" but that gave an error as well. Does anyone have alternatives I can try out?
I have already imported pip install --upgrade google-api-python-client
Would appreciate any help/suggestions!
Here is the code:
from apiclient.discovery import build
# Arguments that need to passed to the build function
DEVELOPER_KEY = "your_API_Key"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
# creating Youtube Resource Object
youtube_object = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey = DEVELOPER_KEY)
def youtube_search_keyword(query, max_results):
# calling the search.list method to
# retrieve youtube search results
search_keyword = youtube_object.search().list(q = query, part = "id, snippet",
maxResults = max_results).execute()
# extracting the results from search response
results = search_keyword.get("items", [])
# empty list to store video,
# channel, playlist metadata
videos = []
playlists = []
channels = []
# extracting required info from each result object
for result in results:
# video result object
if result['id']['kind'] == "youtube# video":
videos.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"],
result["id"]["videoId"], result['snippet']['description'],
result['snippet']['thumbnails']['default']['url']))
# playlist result object
elif result['id']['kind'] == "youtube# playlist":
playlists.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"],
result["id"]["playlistId"],
result['snippet']['description'],
result['snippet']['thumbnails']['default']['url']))
# channel result object
elif result['id']['kind'] == "youtube# channel":
channels.append("% s (% s) (% s) (% s)" % (result["snippet"]["title"],
result["id"]["channelId"],
result['snippet']['description'],
result['snippet']['thumbnails']['default']['url']))
print("Videos:\n", "\n".join(videos), "\n")
print("Channels:\n", "\n".join(channels), "\n")
print("Playlists:\n", "\n".join(playlists), "\n")
if __name__ == "__main__":
youtube_search_keyword('Geeksforgeeks', max_results = 10)
With this information it's hard to say what is the problem. But sometimes I've been banging my head to wall when installing something with pip (Python2) and then trying to import module in Python3 or vice versa.
So if you are running your script with Python3, try install package by using pip3 install --upgrade google-api-python-client
Try the YouTube docs here:
https://developers.google.com/youtube/v3/code_samples
They worked for me on a recently updated Slackware_64 14.2
I use them with Python 3.8. Since there may also be a version 2 of Python installed, I make sure to use this in the Interpreter line:
!/usr/bin/python3.8
Likewise with pip, I use pip3.8 to install dependencies
I installed Python from source. python3.8 --version Python 3.8.2
You can also look at this video here:
https://www.youtube.com/watch?v=qDWtB2q_09g
It sort of explains how to use YouTube's API explorer. You can copy code samples directly from there. The video above covers Android, but the same concept applies to Python regarding using YouTube's API Explorer.
I concur with the previous answer regarding version control.
I am writing a program in python using pytube, and I want to indicate progress when downloading a playlist. When downloading a single video I can do:
YouTube(url, on_progress_callback=progressFunction)
but that doesn't work when downloading a playlist:
Playlist(url, on_progress_callback=progressFunction)
I get the following error:
TypeError: __init__() got an unexpected keyword argument 'on_progress_callback'
Is there any way to get the progress when downloading a playlist?
Hey you can get all the urls from the Playlist and then call download one by one.
this works for me all the best.
def getAllLinks(playList):
'''
: This function take a link of playlist and return the link of each videos
:param playList:
:return: A list of all Url links
'''
allLinks = []
youtubeLink = 'https://www.youtube.com'
pl = Playlist(playList)
for linkprefix in pl.parse_links():
allLinks.append(youtubeLink + linkprefix)
return allLinks
from this you will get all the urls and then
def downloadPlaylist(playlistLink):
linkArray = getAllLinks(playlistLink)
for link in linkArray:
downloadVideo(link)
According to the source code, the Playlist class doesn't need on_progress_callback keyword argument, but only the url one.
You can use the register_on_progress_callback function to register a download progress callback function post initialization.
An example of this would be:
p = Playlist('https://www.youtube.com/playlist?list=PLetg744TF10BrdPjaEXf4EsJ1wz6fyf95')
for v in p.videos:
v.register_on_progress_callback(progressFunction)
# proceed to downloading...
from pytube import Playlist
from pytube.cli import on_progress
yt_playlist = Playlist(url)
for video in yt_playlist.videos:
video.register_on_progress_callback(on_progress)
video.streams.first().download()