Pytube part 2

Posted by Afsal on 12-Jan-2023

Hi Pythonistas!

First of all thanks for the excellent response to the last post. Today we are answering those questions. We can see those questions one by one.

1 How can we adjust the quality of the videos?

To get the video with a resolution there is a method called get_by_resolution. Also, we can get the highest or lowest resolution video by calling the method get_highest_resolution, get_lowest_resolution

Code

from pytube import YouTube

YouTube("https://www.youtube.com/watch?v=fyMj73zcn3Y&t=1s"
).streams.filter(file_extension='mp4').get_by_resolution('720p').download()

Here we can specify the resolution for the video.

If we need the highest or lower resolution there is another method 

Code

from pytube import YouTube

YouTube("https://www.youtube.com/watch?v=fyMj73zcn3Y&t=1s"
).streams.filter(file_extension='mp4').get_highest_resolution().download()
from pytube import YouTube

YouTube("https://www.youtube.com/watch?v=fyMj73zcn3Y&t=1s"
).streams.filter(file_extension='mp4').get_lowest_resolution().download()

2 Can we rename the downloaded file?

Yes, we can specify the name in the download method

Code

from pytube import YouTube

YouTube("https://www.youtube.com/watch?v=fyMj73zcn3Y&t=1s"
).streams.filter(file_extension='mp4').get_lowest_resolution().download(filename="strell_cbr_650.mp4")

3 Can we download audio only?

Yes, we can download audio only by providing an argument called only_audio

from pytube import YouTube

YouTube("https://www.youtube.com/watch?v=fyMj73zcn3Y&t=1s"
).streams.filter(only_audio=True).first().download(filename="strell_cbr_650.mp3")

4 Can we choose the selected time and download it?

No, pytube doesn’t provide such a feature. To know more click here

5 Can we download the entire playlist with this?

Yes, It provides an option to work with a playlist. 

Printing video title in a playlist

from pytube import Playlist
P = Playlist('https://www.youtube.com/watch?v=Li2y1-xwMfM&list=PL0dHyj2mC0eR67uR4mD8CUEzYhaMW-2ZK')

for video in p.videos:
    print(video.title)

Output

Kannathil Muthamittal | Sabareesh Prabhaker | A R Rahman | Medley violin cover
Pookal pookum flute by Vishnu Prabha Ft Tittoo C J | Vishnu Prabha Wind Music
A.R.Rahman's Nila Kaigirathu - Karthick Iyer and Ramprasad Sundar | Violin Fusion | Violin Cover
Vellai Pookal - Vatapi  Medley | A.R.Rahman |  Violin Fusion | IndoSoul | Violin Cover |
Ilayaraja  A.R.Rahman Mashup | IndoSoul | Violin Fusion | Violin Cover
"Ennavale" / "O Cheliya" / Sun Ri Sakhi -A.R Rahman cover- Abhijith P S Nair ft. Sumesh Anand
Anjali Anjali Violin Cover-Abhijith P S Nair ft. Sandeep Mohan (A.R Rahman Cover)
"Ennodu nee irundhaal" by The Fiddle and The Keys
"Yennai Maatrum Kadhalae" cover by The Fiddle and The Keys
Maari- Don'u Don'u Don'u | Veena cover
Yennai Maatrum Kadhale - Veena cover | Naanum Rowdy Dhaan
Narumugaiye Narumugaiye (Rajesh Vaidhya on Veena)
YouTube   Anjali Anjali   By Rajesh Vaidya
En Mel Vilundha - Veena Instrumental
Thendral vanthu rock  (tribute)#myistudio#mytamilrock
Munbe  Vaa ,[ flutu Cover ] By Dileep Babu .B
Rajhesh Vaidhya Athan Ennathan London
thendral vandhu theendum song by rajesh vaidhya
Po nee po (3 Moonu) - Instrumental Guitar and Piano Acoustic Cover - Anirudh Ravichander Tamil Song
Naanum Rowdy Dhaan | Flute Cover Mashup | Kuruji
Sway Anubavam mashup | Old Madras Sessions | Karthick Iyer Live feat. Dondieu Divin
Video Not Available
Pattu Pada Vaa et cetera | Old Madras Sessions | Karthick Iyer Live feat. Dondieu Divin
Mid Air | IndoSoul | IndoSoul by Karthick Iyer | Carnatic Fusion | Violin Fusion
CJ Germany - The Beginning (Music by M.Kowtham)
Ilayaraja Cover MashUp - M.Kowtham feat. CJ Germany
Mash Up Cover - CJ Germany feat. M. Kowtham
The Best of AR Rahman - 90s Rahman songs - Flute Medley cover - Vijay Kannan
"Kanne Kalaimane" Abhijith P S Nair on Violin Live With His fusion Band(Ilayaraja Violin Cover)
SUNDARI KANNAL ORU SETHI -FLUTE VIJAY
Sara Sara | Old Madras Sessions | IndoSoul feat. Dondieu Divin
All of Me (Indian Classical Version) - Mahesh Raghvan
Love Me Like You Do - Ellie Goulding / Veena Cover by Wageshan
Thalli Pogathey | Vellipomaakey | AYM | Flute by FLUTE SIVA
AR RAHMAN ROJA VIOLIN COVER - PUDHU VELLAI/YEH HASEEN Ft. BINESH BABU
RASATHI UNNA/JAABILLI KOSAM VIOLIN COVER ft. BINESH BABU
Vellai Pookkal - A.R. Rahman | STRINGS Cover - Keethan, Manoj, Ábel, Caroline
Sundari Kannal Oru | Violin | Roopa Revathi | Ilayaraja | Rajinikanth
A R Rahman/Ilayaraja Medley |  | Shravan Sridhar | Sabi Thankachan | Picturemakers
D3 D 4 Dance I Super Finale I Sabareesh - Violinist I Mazhavil Manorama
A R Rahman Medley Part-1 || Mashup Cover || Threeory
SUNDARI KANNAL ORU  FLUTE COVER BY JOHNSON
Enna Solla - Thangamagan | Roopa Revathi ft. Shivaradhya | Violin Cover | Dhanush
Ennai Thalatta Varuvala | Ilaiyaraja | Vijay | Flute Siva
Vennilave Violin Cover | Balabhaskar
Uyirum Neeye | Roopa Revathi | A. R. Rahman | Violin Cover
AR Rahman's Urvasi - Carnatic Mix
Girls Like You - Indian Classical Version (feat. Praveen Prathapan & Janan Sathiendran)
Yeh Haseen Vadiyan / Pudhu Vellai Mazhai - Mahesh Raghvan
REWIND RAHMAN - VENNILAVE | KAADHALENUM | MINNALE - RAJHESHVAIDHYA
Malargale (Flute Cover) - Sruthi Balamurali | A.R. Rahman
Naada Vindhu . . .beyond sound, time and space - Violin Cover - Pooven.M
Film mashup in Raga Kapi | Karthick Iyer | Kadhal Rojave | Kanne Kalaimane | Tumbi Vaa
Video Not Available

Downloading first 3 videos from the playlist

from pytube import Playlist

p = Playlist('https://www.youtube.com/watch?v=Li2y1-xwMfM&list=PL0dHyj2mC0eR67uR4mD8CUEzYhaMW-2ZK')

for video in p.videos[:3]:
    video.streams.first().download()

I hope this was helpful please share your valuable feedback with afsal@inovicstech.com