Download mp4 video from YouTube sung python

I was trying to download videos from YouTube the below code. But it gives me .3gpp file format which is very low resolution. How can I make it mp4 or the best resolution?

Code:

from pytube import Playlist
p = Playlist('https://www.youtube.com/watch?v=cSKGa_7XJkg&list=PLoGtT1rYE-El8eTpyXUzeFHbRUDUJN1Ag')
print(f'Downloading: {p.title}')
for video in p.videos:
     video.streams.first().download()

Upvotes: 1

Views: 5713

Answers (2)

Gitau Harrison
Gitau Harrison

Reputation: 3517

To download the highest resolution file you can run:

>>> from pytube import YouTube
>>> yt = YouTube("URL").streams.get_highest_resolution()
>>> yt.download()

Should you want a list of resolutions to choose from, consider:

resolution = yt.streams.filter(adaptive=True, file_extension='mp4').order_by('resolution').desc()

This gets you all the mp4 resolutions in descending order (1080 > 720 > 360). You can then loop through this to display individual resolutions that you may want to pick:

{% for i in resolution %}
    <option value="itag"> {{ i.resolution }} </option>
{% endfor %}

You will have a drop-down menu in an HTML template

There are several filters you can apply to be specific on what type of video you want to download. Refer to the documention.

Upvotes: 0

Smaurya
Smaurya

Reputation: 187

Try this

from pytube import YouTube

link = "https://www.youtube.com/watch?v=cSKGa_7XJkg&list=PLoGtT1rYE-El8eTpyXUzeFHbRUDUJN1Ag"

yt = YouTube(link)  

try:
    yt.streams.filter(progressive = True, 
file_extension = "mp4").first().download(output_path = <your output path here>, 
filename = "downloaded_video.mp4")

except:
    print("Some Error!")
print('Task Completed!')

Upvotes: 2

Related Questions