Let's use pytube

IPFactory Advent Calender 2019 Day 4

I don't have enough knowledge to write a large article, but I will write it on the 4th day. I'm pycys from IP Factory and ISC 1st year.

What is pytube

A Python library. You can easily download videos on YouTube.

environment

Let's install

pip install pytube

If you type the above in bash or command prompt, it will be installed.

Caution

Please see the channel name of the video poster before downloading. If there is no check mark next to the channel name If you download it, it violates the YouTube rules.

Let's download the video

Let's download it immediately.

download1.py


from pytube import YouTube

YouTube(input("URL of the video you want to download:")).streams.first().download()

Try copying and running the above code. When asked for the URL, paste the URL of the video and press Enter. If it works, you should have a video file in the same folder.

Specify the folder to save

download2.py


from pytube import YouTube

YouTube(input("URL of the video you want to download:")).streams.first().download(r"Folder path")

If you want to specify the folder to save, pass the path of the save destination folder to the argument of the download method. Don't forget to add r to disable escape sequences as you specify the folder path. If you want to rename the video file when downloading, set the name you want to give to the second argument of the download method.

Try to specify the resolution, format, etc.

download3.py


from pytube import YouTube

url = input("URL of the video you want to download:")
print(*YouTube(url).streams.all(), sep="\n")
itag = int(input("Tag of the video you want to download:"))
YouTube(url).streams.get_by_itag(itag)).download()

Now try running this code. You will be asked for the URL immediately after executing it, so enter it and press Enter.

Then a character string like this was returned, right? (It's okay if the formats are similar)

<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">
<Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">
<Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9">
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">
<Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9">
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f">
<Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">
<Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9">
<Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">
<Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">
<Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9">
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">
<Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">
<Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">
<Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">

These are combinations of downloadable formats, resolutions, etc. You can download it by specifying the value of itag of the combination you want to download from this.

Select a good combination from the returned combinations and enter the value of itag. If the values you entered are correct, the selected file should have been downloaded.

Finally

That's it. Please point out any incorrect information, typographical errors, or confusing points. Thank you for reading until the end.

Recommended Posts

Let's use pytube
Let's use def in python
Let's use python janome easily
Let's use python's wordcloud easily!
Let's use MemSQL Vol.14: Practice 7
Let's use MemSQL Vol.13: Practice 6
Let's use usercustomize.py instead of sitecustomize.py
Let's use tomotopy instead of gensim
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
Use DeepLabCut
[Introduction to Python] Let's use foreach with Python
Use pycscope
Use collections.Counter
Use: Django-MySQL
Use Pygments.rb
Let's make jupyter lab easy to use
Use Numpy
use pandas-ply
Use GitPython
Use Miniconda
Let's use different versions of SQLite3 from Python3!