I had made a youtube video downloader using pytube in python. It previously works fine but during sometime it gives an error of "assets". I had tried a lot but I can't solve it. I had also tried to search error solution but I can't found anything like that.
Here is my code
def startDownload(url):
global file_size
path_to_save = askdirectory()
if path_to_save is None:
return
try:
global MaxFileSize, fileSizeInBytes
choice = youtubeChoicesLabel.get()
url=urlField.get()
yt = YouTube(url)
nome = yt.title
if (choice == download_choices[1]):
print("720p video is downloading")
selectVideo = yt.streams.filter(progressive=True, file_extension='mp4').first()
elif (choice == download_choices[2]):
print("144 video is downloading")
selectVideo = yt.streams.filter(progressive=True, file_extension='mp4').last()
selectVideo = yt.streams.filter(only_audio=True).first()
elif (choice == download_choices[0]):
return
fileSizeInBytes = selectVideo.filesize
MaxFileSize = fileSizeInBytes/1024000
MB =str(MaxFileSize)+ "MB"
print("File Size = : {:00.000f}".format (MaxFileSize))
st= selectVideo
yt.register_on_complete_callback(complete_download)
yt.register_on_progress_callback(progress_download)
file_size=st.filesize
st.download(output_path=path_to_save)
except Exception as e:
print(e)
download_choices = ["----Download Type----",
"Mp4 720p",
"Mp4 144p",
# "Video 3gp",
"Audio Mp3"]
def btnClicked():
try:
downloadBtn['text'] = "Please wait....."
downloadBtn["bg"] = "red"
downloadBtn["fg"] ="white"
downloadBtn['state']= 'disabled'
url=urlField.get()
if url == '':
showinfo("Message","Please input a url of the video.....")
return
print(url)
thread= Thread(target=startDownload,args=(url,))
thread.start()
except Exception as e:
print(e)
Here is my error
soham#LAPTOP-FJLV55TK MINGW64 ~
y"python "e:/python projects/YOUTUBE VIDEO DOWNLOADER/YOUTUBE_VIDEO_DOWNLOADER.py
e:/python projects/YOUTUBE VIDEO DOWNLOADER/YOUTUBE_VIDEO_DOWNLOADER.py
https://www.youtube.com/watch?v=rTuxUAuJRyY
'assets'
I had the same problem and updating pytube to the latest version available currently the problem disappeared.
pip install pytube==10.0.0
or
pip install --upgrade pytube
Similar question.
Related
I have been creating a program to grab links from a YouTube playlist and download the individual links but I noticed if you try to download a video that has a "Viewer Discretion is Advised" warning label attached to the video I get an error that reads "Streaming Error"
from pytube import YouTube
from pytube import Playlist
import os
#---------------------------------------------
youtube = True
is_playlist = False
convert_to_mp3 = True
youtube_playlist_name = "Folder Name"
youtube_playlist_videos = [""]
#---------------------------------------------
spotify = False
spotify_playlist_name = ""
spotify_playlist = [""]
#---------------------------------------------
def download_and_convert_video(link, playlist_name, x, urls):
video = link.streams.get_audio_only()
out_file = video.download(output_path=f"audio/{playlist_name}")
base = os.path.splitext(out_file)
new_file = f"{base[0]}.mp3"
print(f"Downloading \"{new_file[new_file.find(playlist_name) + len(playlist_name) + 1:len(new_file)]}\" - {x+1}/{len(urls)}")
os.rename(out_file, new_file)
def obtain_urls(playlists):
urls, x = [], 0
print("Locating videos in playlist")
for playlist in playlists:
playlist_urls = Playlist(playlist)
for url in playlist_urls:
x += 1
urls.append(url)
print(f"{x} video(s) located")
return urls
def download_from_youtube(playlist, playlist_name, convert_to_mp3, is_playlist):
if is_playlist == True:
urls = obtain_urls(playlist)
if is_playlist == False:
urls = playlist
for x in range(len(urls)):
if convert_to_mp3 == True:
download_and_convert_video(YouTube(urls[x]), playlist_name, x, urls)
if convert_to_mp3 == False:
download_video(YouTube(urls[x]), playlist_name)
print(f"Successfully downloaded {len(urls)} video(s)")
def download_video(video, playlist_name):
video = video.streams.get_highest_resolution()
video.download(f"video/{playlist_name}")
if youtube == True:
download_from_youtube(youtube_playlist_videos, youtube_playlist_name, convert_to_mp3, is_playlist)
I found this video k8jw3JVfK0w with the The following content may contain suicide or self-harm topics.
Viewer discretion is advised. Learn more you mentioned, with youtube-dl downloading this video works fine, please give it a try.
I wrote myself a little youtube downloader for practice, that can download video and audio. So the problem is, that the formats Full HD <= mostly don't contain any audio. So my solution for that is to just download the audio seperatly and combine it together afterwards with moviepy. My problem is that when I watch the final output the audio is about .5 seconds behind the video, which looks really weird. The code (its bad, but its just an exercise):
from pytube import YouTube
from moviepy.editor import VideoFileClip
from time import sleep
import os
def convert_to_mp3(filename):#method to convert to mp3 since i havent found a way to download mp3
clip = VideoFileClip(filename)
clip.audio.write_audiofile(filename[:-4] + ".mp3")
clip.close()
def input_link():#get the yt link
print("Videolink:")
link = input()
return link
while True:#check the link until its correct
try:
link = input_link()
video = YouTube(link)
break
except Exception:
print("Fehler im Link!\n")
print("\n1) Video\n2) Audio")
def download_video(itag, name=video.title):#download the video and return the type
# print(video.streams.filter(file_extension="mp4").all)
name = video.streams.get_by_itag(itag).download(filename=name)
if name.__contains__(".webm"):
return "webm"
elif name.__contains__(".mp4"):
return "mp4"
else:
print("Unknown format... Name of the file: " + name)
return "unknown"
def combine_audio(vidname, audname, outname, fps=25):#combine the audio and the video with moviepy
import moviepy.editor as mpe
my_clip = mpe.VideoFileClip(vidname)
audio_background = mpe.AudioFileClip(audname)
final_clip = my_clip.set_audio(audio_background)
final_clip.write_videofile(outname)
print("removing " + vidname)
os.remove(vidname)
print("removing " + audname)
os.remove(audname)
video_audio = input()
def download_video_only(download=True):#function to download the video
global fps
formats = str(video.streams.filter().all)
formatsFormattet = formats[formats.index("[") + 1:formats.index("]")]
formatsList = []
while formatsFormattet != "":#prepares the list of the available formats, ill make a method to sort out doubles later
try:
formatsList.append(formatsFormattet[formatsFormattet.index(":") + 1:formatsFormattet.index(",") + 1])
except ValueError:
formatsList.append(formatsFormattet[formatsFormattet.index(":") + 1:len(formatsFormattet)])
try:
formatsFormattet = formatsFormattet[formatsFormattet.index(",") + 1: len(formatsFormattet)]
except ValueError:
formatsFormattet = ""
counter = 1
itagList = []
for element in formatsList:
try:
element = element[element.index("itag=") + 6:len(element)]
if element[2] == '"':
itagList.append(element[0:2])
else:
itagList.append(element[0:3])
# print("\nItag: " + element[0:3])
element = element[element.index("res=") + 5:len(element)]
print("\n" + str(counter) + ")\nAuflösung: " + element[0:element.index("\"")])
element = element[element.index("fps=") + 5:len(element)]
fps = element[0:element.index("\"")]
print("Fps: " + element[0:element.index("\"")])
counter += 1
except ValueError:
break
counter -= 1
while True:
print("\nWelches Format hätten sie gerne(1-" + str(counter)+ ")")#print all the formats for the user to select one
selection = input()
if int(selection) - 1 < len(formatsList):
# print(formatsList[int(selection) - 1])
if download:
if video.streams.get_by_itag(itagList[int(selection) - 1]).is_progressive:#if the video has audio, just download it
download_video(itagList[int(selection) - 1])
else:#if the video has no audio, download audio and video and combine it after finding out the format
download_audio_only(name="audio")
type = download_video(itagList[int(selection) - 1], name="video")
if(type == "webm"):
combine_audio("video.webm", "audio.mp3", video.title + ".mp4", fps)
elif type == "mp4":
combine_audio("video.mp4", "audio.mp3", video.title + ".mp4", fps)
elif type == "unknown":
return
else:
return itagList[int(selection) - 1]
break
print("\nUngültige Eingabe, bitte richtige Eingabe tätigen")
def download_audio_only(name=video.title):#download the audio and make it to mp3
file = video.streams.filter(only_audio=True).first().download(filename=name)
sleep(1)
print(file)
os.rename("audio.mp4", "audio.mp3")
if int(video_audio) == 1:#get selection for video or audio
download_video_only()
else:
download_audio_only()
Any ideas on how to fix that?
Thanks for any help
Hello i get following error:
pytube.exceptions.RegexMatchError: get_ytplayer_config: could not find match for config_patterns
My python version: 3.9.2
Can somebody please help me?
My Code:
from tkinter import ttk
from tkinter import filedialog
from pytube import YouTube #pip install pytube3
Folder_Name = ""
#file location
def openLocation():
global Folder_Name
Folder_Name = filedialog.askdirectory()
if(len(Folder_Name) > 1):
locationError.config(text=Folder_Name,fg="green")
else:
locationError.config(text="Please Choose Folder!!",fg="red")
#donwload video
def DownloadVideo():
choice = ytdchoices.get()
url = ytdEntry.get()
if(len(url)>1):
ytdError.config(text="")
yt = YouTube(url)
if(choice == choices[0]):
select = yt.streams.filter(progressive=True).first()
elif(choice == choices[1]):
select = yt.streams.filter(progressive=True,file_extension='mp4').last()
elif(choice == choices[2]):
select = yt.streams.filter(only_audio=True).first()
else:
ytdError.config(text="Paste Link again!!",fg="red")
#download function
select.download(Folder_Name)
ytdError.config(text="Download Completed!!")
root = Tk()
root.title("YTD Downloader")
root.geometry("350x400") #set window
root.columnconfigure(0,weight=1)#set all content in center.
#Ytd Link Label
ytdLabel = Label(root,text="Enter the URL of the Video",font=("jost",15))
ytdLabel.grid()
#Entry Box
ytdEntryVar = StringVar()
ytdEntry = Entry(root,width=50,textvariable=ytdEntryVar)
ytdEntry.grid()
#Error Msg
ytdError = Label(root,text="Error Msg",fg="red",font=("jost",10))
ytdError.grid()
#Asking save file label
saveLabel = Label(root,text="Save the Video File",font=("jost",15,"bold"))
saveLabel.grid()
#btn of save file
saveEntry = Button(root,width=10,bg="red",fg="white",text="Choose Path",command=openLocation)
saveEntry.grid()
#Error Msg location
locationError = Label(root,text="Error Msg of Path",fg="red",font=("jost",10))
locationError.grid()
#Download Quality
ytdQuality = Label(root,text="Select Quality",font=("jost",15))
ytdQuality.grid()
#combobox
choices = ["720p","144p","Only Audio"]
ytdchoices = ttk.Combobox(root,values=choices)
ytdchoices.grid()
#donwload btn
downloadbtn = Button(root,text="Donwload",width=10,bg="red",fg="white",command=DownloadVideo)
downloadbtn.grid()
#developer Label
developerlabel = Label(root,text="Dream Developers",font=("jost",15))
developerlabel.grid()
root.mainloop()
The error pytube.exceptions.RegexMatchError: get_ytplayer_config: could not find match for config_patterns has been a known bug in pytube. You can either update the request.py file. Or the best way is to update pytube:
python -m pip uninstall pytube pytube3 pytubex
python -m pip install git+https://github.com/nficano/pytube
I am working to extract issues data from a repo on Github using Github3.py.
The following is a part of my code to extract issues from a repo:
I used these libraries in the main code:
from github3 import login
from mysql.connector import IntegrityError
import config as cfg
import project_list
from github3.exceptions import NotFoundError
from github3.exceptions import GitHubException
import datetime
from database import Database
import sys
import re
import time
Then the main code is:
DEBUG = False
def process(url, start):
re_pattern = re.compile(u'[^\u0000-\uD7FF\uE000-\uFFFF]', re.UNICODE)
splitted = url.split("/")
org_name = splitted[3]
repo_name = splitted[4]
while True:
try:
gh = login(token = cfg.TOKEN)
repo = gh.repository(org_name, repo_name)
print("{} =====================".format(repo))
if start is None:
i = 1
else:
i = int(start)
if start is None:
j = 1
else:
j = int(start)
Database.connect()
while True:
try:
issue = repo.issue(i)
issue_id = issue.id
issue_number = issue.number
status_issue = str(issue.state)
close_author = str(issue.closed_by)
com_count = issue.comments_count
title = re_pattern.sub(u'\uFFFD', issue.title)
created_at = issue.created_at
closed_at = issue.closed_at
now = datetime.datetime.now()
reporter = str(issue.user)
body_text = issue.body_text
body_html = issue.body_html
if body_text is None:
body_text = ""
if body_html is None:
body_html = ""
body_text = re_pattern.sub(u'\uFFFD', body_text)
body_html = re_pattern.sub(u'\uFFFD', body_html)
Database.insert_issues(issue_id, issue_number, repo_name,status_issue , close_author, com_count, title, reporter, created_at, closed_at, now, body_text, body_html)
print("{} inserted.".format(issue_id))
if DEBUG == True:
break;
except NotFoundError as e:
print("Exception # {}: {}".format(i, str(e)))
except IntegrityError as e:
print("Data was there # {}".format(str(e)))
i += 1
j += 1
except GitHubException as e:
print("Exception: {}".format(str(e)))
time.sleep(1000)
i -= 1
j -= 1
if __name__ == "__main__":
if len(sys.argv) == 1:
sys.exit("Please specify project name: python issue-github3.py <project name>")
if len(sys.argv) == 2:
start = None
print("Start from the beginning")
else:
start = sys.argv[2]
project = sys.argv[1]
url = project_list.get_project(project)
process(url, start)
With the above code, everything is ok for me and I can extract issues from a repo on GitHub.
Problem: Exception: 410 Issues are disabled for this repo occurs after 100 successful issues extraction from a repo.
How could I solve this problem?
As mentioned in the main code, I fixed the exception 404 (i.e., Not found issues) with the library of from github3.exceptions import NotFoundError and the below code:
except NotFoundError as e:
print("Exception # {}: {}".format(i, str(e)))
Given the main code, what library and code should I use to fix exception 410?
I found an easy way to fix it but it doesn't solve the problem completely.
As I mentioned before, the exception occurs after 100 successful issue number (i.e., issue number of 101 is a problem), and as #LhasaDad said above, there is no issue number of 101 in the repo (I checked it manually). So we just need to put 102 instead of None where start = None, then execute the code again.
I was wondering if anyone could help me out a bit with this problem I cannot solve. I am using Pafy to search Youtube from a text file that has a song name written in it and that gets a new song every few minutes.
I am using Watchdog to watch for file modification and when i first run the script, watchdog catches the file modification and runs the pafy and opencv script, but it won't do the same when the following modification occurs.
#watchdog file change monitoring
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print ("Received modified event - %s." % event.src_path)
cv2.destroyAllWindows()
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='//PLAYING', recursive=False)
observer.start()
try:
while True:
#read PLAYING.txt
PLAYING = open('//PLAYING.txt').readline()
PLAYING = PLAYING[7:]
print (PLAYING)
#search youtube based on NowOnAir.txt
query_string = urllib.parse.urlencode({"search_query" : PLAYING})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode())
link = ('http://www.youtube.com/watch?v=' + search_results[0])
videoPafy = pafy.new(link)
best = videoPafy.getbestvideo()
videompv = best.url
#opencv youtube video output
video = cv2.VideoCapture(videompv)
while(video.isOpened()):
ret, frame = video.read()
resize = cv2.resize(frame, (1680, 1050))
gray = cv2.cvtColor(resize, cv2.COLOR_BGR2GRAY)
result = cv2.addWeighted(image, 0.2, resize, 0.8, 0)
cv2.namedWindow('frame', 0)
cv2.resizeWindow('frame', 1680, 1050)
cv2.imshow('frame', result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
So, I what I want to happen is, when file gets modified, I want openCV to close the window and open a new one with the new youtube query result.
Any suggestions would be quite welcome, thank You in advance.
If the file is just updated once per track change, then you could check the timestamp of the file for modification and use that to trigger your search.
import os.path
import time
last_modified = time.ctime(os.path.getmtime(file))
while True:
time.sleep(1)
if last_modified != time.ctime(os.path.getmtime(file)):
# search for the track on youtube
last_modified = time.ctime(os.path.getmtime(file))