Is it possible to make a list of title and thumbnail of videos of playlist? Then we can try to run macros or cronjob to automate it in regular basis.
So I'm a newbie here. I've found out today that more than 25 of my fav videos are gone from YouTube playlist which is irrecoverable as far as I know by now. Anyways that brings me an idea to try to make or perhaps if already exists such program, then trying to find out. Is it possible to make a list of title and thumbnail of videos of playlist? Then we can try to run macros or cronjob to automate it in regular basis. I know that it's possible to get title list from yt-dlp but how can I integrate thumbnail to the list? Or are there such site/program exist already? Any help is very much appreciated. For the information, I'm basically on Android.....so any python/ html/site/android app solution Will be very much helpful in this regard.
You are looking for YouTube Data API v3 PlaylistItems: list endpoint. You can use it the following way in Python:
import requests, json, csv
API_KEY = 'AIzaSy...'
PLAYLIST_ID = 'YOUR_PLAYLIST_ID'
with open('playlist.csv', 'w') as csvFile:
csvWriter = csv.writer(csvFile)
csvWriter.writerow(['videoId', 'title', 'thumbnails'])
pageToken = ''
while True:
url = f'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&playlistId={PLAYLIST_ID}&maxResults=50&pageToken={pageToken}&key={API_KEY}'
content = requests.get(url).text
data = json.loads(content)
for item in data['items']:
print(json.dumps(item, indent=4))
snippet = item['snippet']
csvWriter.writerow([snippet['resourceId']['videoId'], snippet['title'], snippet['thumbnails']])
if 'nextPageToken' in data:
pageToken = data['nextPageToken']
else:
break
I am looking to download a YouTube playlist using the PyTube library. Currently, I am able to download a single video at a time. I cannot download more than one video at once.
Currently, my implimentation is
import pytube
link = input('Please enter a url link\n')
yt = pytube.YouTube(link)
stream = yt.streams.first()
finished = stream.download()
print('Download is complete')
This results in the following output
>> Download is complete
And the YouTube file is downloaded. When I try this with a playlist link (An example) only the first video is downloaded. There is no error outputted.
I would like to be able to download an entire playlist without re-prompting the user.
You can import Playlist to achieve this. There is no reference to Playlist in the redoc, though there is a section in the GitHub repo found here. The source of the script is in the repo here.
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/watch?v=58PpYacL-VQ&list=UUd6MoB9NC6uYN2grvUNT-Zg')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()
NOTE: I've found the supporting method Playlist.video_urls does not work. The videos are still downloaded however, as evidenced here
The solutions above no longer work. Here's a code which downloads the sound stream of the videos referenced in a Youtube playlist. Pytube3 is used, not pytube. Note that the playlist must be public for the download to succeed. Also, if you want to download the full video instead of the sound track only, you have to modify the value of the Youtube tag constant. The empty Playlist.videos list fix was taken from this Stackoverflow post:PyTube3 Playlist returns empty list
import re
from pytube import Playlist
YOUTUBE_STREAM_AUDIO = '140' # modify the value to download a different stream
DOWNLOAD_DIR = 'D:\\Users\\Jean-Pierre\\Downloads'
playlist = Playlist('https://www.youtube.com/playlist?list=PLzwWSJNcZTMSW-v1x6MhHFKkwrGaEgQ-L')
# this fixes the empty playlist.videos list
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print(len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
# physically downloading the audio track
for video in playlist.videos:
audioStream = video.streams.get_by_itag(YOUTUBE_STREAM_AUDIO)
audioStream.download(output_path=DOWNLOAD_DIR)
if one needs to download the highest quality video of each item in a playlist
playlist = Playlist('https://www.youtube.com/watch?v=VZclsCzhzt4&list=PLk-w4cD8sJ6N6ffzp5A4PQaD76RvdpHLP')
for video in playlist.videos:
print('downloading : {} with url : {}'.format(video.title, video.watch_url))
video.streams.\
filter(type='video', progressive=True, file_extension='mp4').\
order_by('resolution').\
desc().\
first().\
download(cur_dir)
this code allows you to download a playlist to your assigned folder
import re
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=Pd5k1hvD2apA0DwI3XMiSDqp')
DOWNLOAD_DIR = 'D:\Video'
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print(len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
for video in playlist.videos:
print('downloading : {} with url : {}'.format(video.title, video.watch_url))
video.streams.\
filter(type='video', progressive=True, file_extension='mp4').\
order_by('resolution').\
desc().\
first().\
download(DOWNLOAD_DIR)
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()
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'm using pytube3 9.6.4, not pytube.
Now Playlist.video_urls works well.
And Playlist.populate_video_urls() function was deprecated.
from pytube import YouTube
from pytube import Playlist
SAVE_PATH = "E:/YouTube" #to_do
#link of the video to be downloaded
links= "https://youtube.com/playlist?list=PLblh5JKOoLUL3IJ4- yor0HzkqDQ3JmJkc"
playlist = Playlist(links)
PlayListLinks = playlist.video_urls
N = len(PlayListLinks)
#print('Number of videos in playlist: %s' % len(PlayListLinks))
print(f"This link found to be a Playlist Link with number of videos equal to {N} ")
print(f"\n Lets Download all {N} videos")
for i,link in enumerate(PlayListLinks):
yt = YouTube(link)
d_video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
d_video.download(SAVE_PATH)
print(i+1, ' Video is Downloaded.')
You can check this repository to download playlists and individual videos and keep them in different directories.
https://github.com/pushpendra050/Pytube-for-Playlist-download
this work for me in windows 11 or 10.5
Original code: Jean-Pierre Schnyder
import re
from pytube import Playlist
DOWNLOAD_DIR = input ("Download dir")
playlist = input ("Link:")
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print(len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
for video in playlist.videos:
audioStream = video.streams.get_highest_resolution()
audioStream.download(output_path=DOWNLOAD_DIR)
Though it seems like a solved problem, but here is one of my solutions which may help you to download the playlist specifically as video of 1080P 30 FPS or 720P 30 FPS (if 1080P not available).
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
# Loop through all videos in the playlist and download them
for video in playlist.videos:
try:
print(video.streams.filter(file_extension='mp4'))
stream = video.streams.get_by_itag(137) # 137 = 1080P30
stream.download()
except AttributeError:
stream = video.streams.get_by_itag(22) # 22, 136 = 720P30; if 22 still don't work, try 136
stream.download()
except:
print("Something went wrong.")
This works for me. Just takes the URLs in the playlist and downloads them one by one:
from pytube import Playlist
playlist = Playlist('URL')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
print(video_url)
urls.append(video_url)
for url in urls:
my_video = YouTube(url)
print("*****************DOWNLOAD VID*************")
print(my_video.title)
my_video = my_video.streams.get_highest_resolution()
path = "PATH"
my_video.download(path)
print("VIDEO DOWNLOAD DONNNNE")
import pytube
from pytube import Playlist
playlist = Playlist('plylist link')
num = 0
for v in playlist.videos:
print(v.watch_url)
one = pytube.YouTube(v.watch_url)
one_v = one.streams.get_highest_resolution()
name = f"{0}" + one_v.default_filename
one_v.download()
num = num + 1
Download all playlist
This works flawlessly to download complete playlist
check pytube github to install
pip install pytube
Now, go to this folder and open cipher.py
D:\ProgramData\Anaconda3\Lib\site-packages\pytube\
replace at line 273
function_patterns = [
r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*.*\|\|\s*(.*)\(',
r'\([a-z]\s*=\s*([a-zA-Z0-9$]{3})(\[\d+\])?\([a-z]\)',
]
main.py
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLwdnzlV3ogoXUifhvYB65lLJCZ74o_fAk')
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
print(len(playlist.video_urls))
for url in playlist.video_urls:
print(url)
for video in playlist.videos:
video.streams.get_highest_resolution().download()
this may not work, the code below works for every case
from pytube import Playlist
from pytube import YouTube
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/watch?v=UPFKAG9rYOE&list=PLknwEmKsW8OtK_n48UOuYGxJPbSFrICxm')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
print(video_url)
video=YouTube(video_url)
try:
#video.streams.first().download()
video.streams.filter(res="720p").first().download()
except:
continue
I have a lot of mp3 files that are titled in the following format:
"Artist - Song"
Where both the artist and song are in the 'title' field of the file. I would like to change them all to have the artist and song in their respective fields.
To do this, I am using the eyed3 python module. However, when I run my code I get an error message: "Lame tag CRC check failed", and none of the file properties are changed. Here is the code I currently have:
import os
import eyed3
for files in os.listdir("C:/Users/justi/Desktop/New Music - Copy"):
artist_and_song = files.split(".") # Gets rid of ".mp3" suffix
split_at_dash = artist_and_song[0].split("-") # Separates artist and song
artist = split_at_dash[0]
song = split_at_dash[1]
# Loads each and every MP3
mp3 = eyed3.load("C:/Users/justi/Desktop/New Music" + '/' + files)
mp3.initTag()
mp3.tag.artist = artist
mp3.tag.title = song
mp3.tag.save()
I have seen this question elsewhere on StackOverflow and none of the suggested solutions did the trick. Any help would be greatly appreciated.
I am trying to create a program in Python which automatically retrieves lyrics for a particular folder of MP3s. [I get the lyrics from azlyrics.com
]
So far, I have succeeded in doing everything except for actually embedding the lyrics into the "lyrics" tag.
You answered a question regarding reading the lyrics from it's tag over here.
I was wondering if you could help me with setting the lyrics. Here's my code.
import urllib2 # For downloading webpage
import time # For pausing
import eyed3 # For MP3s
import re # For replacing characters
import os # For reading folders
path = raw_input('Please enter folder of music') # TODO Must make GUI PATH SELECTION
files = os.listdir(path)
for x in files:
# Must make the program stop for a while to minimize server load
time.sleep(3)
# Opening MP3
mp3 = eyed3.load(path + '/' + x)
# Setting Values
artist = mp3.tag.artist.lower()
raw_song = str(mp3.tag.title).lower()
song = re.sub('[^0-9a-zA-Z]+', '', raw_song) #Stripping songs of anything other than alpha-numeric characters
# Generating A-Z Lyrics URL
url = "http://www.azlyrics.com/lyrics/" + artist + "/" + song + ".html"
# Getting Source and extracting lyrics
text = urllib2.urlopen(url).read()
where_start = text.find('<!-- start of lyrics -->')
start = where_start + 26
where_end = text.find('<!-- end of lyrics -->')
end = where_end - 2
lyrics = unicode(text[start:end].replace('<br />', ''), "UTF8")
# Setting Lyrics to the ID3 "lyrics" tag
mp3.tag.lyrics = lyrics ### RUNNING INTO PROBLEMS HERE
mp3.tag.save()
I am running into the following error after the 2nd-last line gets executed:-
Traceback (most recent call last):
File "<pyshell#62>", line 31, in <module>
mp3.tag.lyrics = lyrics
AttributeError: can't set attribute
I would also like you to know that I am a 15 year old who has been learning Python for about a year now. I searched everywhere and tried everything but I guess I need some help now.
Thanks in advance for all your help!
I don't pretend to understand why this is the way it is, but check out how lyrics are set in the handy example file:
from eyed3.id3 import Tag
t = Tag()
t.lyrics.set(u"""la la la""")
I believe this has to do with lyrics being placed into frames, but others may have to chime in with corrections on that. Note that this will fail unless you pass it unicode.
chcp 65001
eyeD3 --encoding utf8 --add-lyrics "001-001.txt" 001-001.mp3
I want to upload document, file to google docs using Google Apps Engine (python)
any code or link will be appreciated
See the documentation, but you might try something like:
ms = gdata.MediaSource(file_path='/path/to/your/test.doc', content_type=gdata.docs.service.SUPPORTED_FILETYPES['DOC'])
entry = gd_client.Upload(ms, 'MyDocTitle')
print 'Document now accessible online at:', entry.GetAlternateLink().href
Solution is with files Upload, You need to read data using below line in python:
function to read file size
def getSize(self,fileobject):
fileobject.seek(0,2) # move the cursor to the end of the file
size = fileobject.tell()
return size
f = self.request.POST.get('fname').file
media = gdata.data.MediaSource(file_handle=f.read(), content_type=gdata.docs.service.SUPPORTED_FILETYPES[ext], content_length=self.getSize(self.request.POST.get('fname').file))
And also need to modify the gdata python library of Google to achieve this:
client.py:
in
def upload_file
replace:
while not entry:
entry = self.upload_chunk(start_byte, self.file_handle.read(self.chunk_size))
start_byte += self.chunk_size
With:
while not entry:
entry = self.upload_chunk(start_byte, self.file_handle)
start_byte += self.chunk_size
And you can upload file directory to google doc