Skip to content

[Python] Using package pytube to download YouTube videos

Last Updated on 2020-09-06 by Clay


Introduction

Various electronic entertainments of modern people are inseparable from YouTube, whether it is to follow dramas, query tutorial, watch funny videos ... we will watch it on YouTube platform. Saying watching YouTube is the most important thing for modern people.

Sometimes, we will need to download videos from YouTube. Of course, this matter itself is likely to violate intellectual property rights, so our use is limited to teaching and non-commercial use. The downloaded video must be deleted within a certain period of time.

For example, elementary school teachers want to teach students through videos, but sometimes the Internet signal is not good, and waiting for the video streaming in class is a waste of time. So, in addition to using the existing network download services on the Internet, you can also consider using Python to simply make the job of downloading videos.

There is a package known as pytube in Python. You can check the developer's Github for detailed information: https://github.com/nficano/pytube

The experience teaching of this article is divided into two stages: first download the video file, and convert the file format as required.


Use pytube to download YouTube videos

First we need to install pytube package.

sudo pip3 install pytube3

The latest version of pytube is 9.5.3. But when I tried before, I failed: I couldn't use the pytube v9.5.2 to download the video files on the YouTube website smoothly.

This is a very important point. YouTube has always changed the way of using crawler technology to download videos. It must update the version of the package we use at any time.

Below we take a video to demo: https://www.youtube.com/watch?v=JwBXgJeqeOs

This is the theme song of a game:

# -*- coding: utf-8 -*-
from pytube import YouTube


url = 'https://www.youtube.com/watch?v=JwBXgJeqeOs'
YouTube(url).streams.first().download()


After a while, we will see the video stored in our current folder.


At this time we must be very confused, huh? There is no progress bar, so if you need to download a lot of things at once, don’t you know the download progress now?

Don't worry, in the YouTube class of pytube, there is "on_progress_callback" that allows us to write our own progress bar.

# -*- coding: utf-8 -*-
from pytube import YouTube


def progress(stream, chunk, file_handle, bytes_remaining):
    contentSize = video.filesize
    size = contentSize - bytes_remaining

    print('\r' + '[Download progress]:[%s%s]%.2f%%;' % (
    '█' * int(size*20/contentSize), ' '*(20-int(size*20/contentSize)), float(size/contentSize*100)), end='')


url = 'https://www.youtube.com/watch?v=JwBXgJeqeOs'
yt = YouTube(url, on_progress_callback=progress)
video = yt.streams.first()
video.download()


Output:


Use moviepy to convert MP4 to MP3

There are many programs and packages for converting videos into audio files. Here I chose moviepy directly because the code is very short.

First of all, we still have to download moviepy.

sudo pip3 install moviepy

After the download is complete, let's convert the downloaded file into test.mp3.

# -*- coding: utf-8 -*-
from moviepy.editor import *

video = VideoFileClip('Sora no Kiseki the 3rd Evolution [BGM RIP] - Cry for your Eternity.mp4')
video.audio.write_audiofile('test.mp3')


Output:

MoviePy - Writing audio in test.mp3
MoviePy - Done.

In this way, our conversion is complete.

1 thought on “[Python] Using package pytube to download YouTube videos”

  1. У кого функция не работает, то пробуем ее объявить так:
    def progress(stream=None, chunk=None, bytes_remaining=0):

Leave a ReplyCancel reply

Exit mobile version