Is there any way to download a Youtube video using urllib?

Asked

Viewed 1,855 times

4

Is there any way to download a video on youtube using urllib in Python?

I did a test, but it doesn’t work (just an example):

import urllib.request


youtube = ""

pagina = urllib.request.urlopen(youtube)

urllib.request.urlretrieve("", "video.mp4")
  • So, you would be opening the video page, and not the file itself, so I understood.

  • Good evening Jefferson, is there a problem with the answer? Something is missing?

  • No, thanks for your attention. I managed to solve the problem.

  • @Jefferson_andr then if one of the answers solved the problem could mark the one that fit your case as correct? Clicking the green checked sign next to the answer

2 answers

9

Note: Remember that Youtube has terms and conditions, if the videos are not yours I do not recommend doing this, as the Wallace said

There is a dependency-free lib called pytube, install using PIP, type in the terminal or cmd:

pip install pytube

Script example:

from pytube import YouTube

# Esta parte não é necessária é apenas usada para entender o exemplo
from pprint import pprint

yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y")

# Uma vez selecionado você pode ver todos formatos e resoluções disponíveis do video que deseja acessar

print(yt.get_videos())

# Exemplos de saídas possíveis

# [<Video: MPEG-4 Visual (.3gp) - 144p>,
#  <Video: MPEG-4 Visual (.3gp) - 240p>,
#  <Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>,
#  <Video: H.264 (.mp4) - 360p>,
#  <Video: H.264 (.mp4) - 720p>,
#  <Video: VP8 (.webm) - 360p>,
#  <Video: VP8 (.webm) - 480p>]

# O nome do video é automaticamente gerado a partir do titulo, mas você pode sobreescrever

# view the auto generated filename:
print(yt.filename)

# Pulp Fiction - Dancing Scene [HD]

# Renomeia:
yt.set_filename('Dancing Scene from Pulp Fiction')

# Você pode ficar a lista por tipo
print(yt.filter('flv'))

# [<Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>]

# Note que a lista é ordenada da menor resolução para maior
# Se esta procurando a maior resolução pelo formato faça isso:

print(yt.filter('mp4')[-1])

#Saída:
# <Video: H.264 (.mp4) - 720p>

# Você pode ver todos formatos por resolução
print(yt.filter(resolution='480p'))

# [<Video: H.264 (.flv) - 480p>,
#  <Video: VP8 (.webm) - 480p>]

# E pode pegar um video por resolução e formato

video = yt.get('mp4', '720p')

# NOTE: get() só irá reconhecer os formatos e resoluções disponiveis

print(yt.videos)

#[<Video: MPEG-4 Visual (.3gp) - 144p>,
# <Video: MPEG-4 Visual (.3gp) - 240p>,
# <Video: Sorenson H.263 (.flv) - 240p>,
# <Video: H.264 (.flv) - 360p>,
# <Video: H.264 (.flv) - 480p>,
# <Video: H.264 (.mp4) - 360p>,
# <Video: H.264 (.mp4) - 720p>,
# <Video: VP8 (.webm) - 360p>,
# <Video: VP8 (.webm) - 480p>]

# Since we have two H.264 (.mp4) available to us... now if we try to call get()
# on mp4...

video = yt.get('mp4')
# MultipleObjectsReturned: 2 videos met criteria.

# In this case, we'll need to specify both the codec (mp4) and resolution
# (either 360p or 720p).

# Okay, let's download it! (a destination directory is required)
video.download('/tmp/')

Per command line:

Specifies the resolution:

$ pytube -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y

Specifies where you want to save:

$ pytube -e mp4 -p ~/Downloads/ http://www.youtube.com/watch?v=Ik-RsDGPI5Y

Specifies name to be saved:

$ pytube -e mp4 -f Dancing Scene from Pulp Fiction http://www.youtube.com/watch?v=Ik-RsDGPI5Y

Specifies the desired resolution and format:

$ pytube -e mp4 -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y

4

It seems that according to the Term of Use, it’s not nice to download videos from Youtube.

Read item 5 with the title: "Use of content", item B.

There probably won’t be any (at least politically correct) API that supports this.

In that response from the SOEN, also talks about it.

  • I was going to quote this :), but since you did I agree +1

  • I know this does not answer the question directly, but it is important to highlight.

  • I will take a test and see how it works. The video used will be mine.

  • 1

    @Jefferson_andr for me is fine. I just wanted to leave this little reminder. After all, your question will be seen by others interested in the same subject. So I chose to do the "boring role", so that they are aware that "has how to do", but is not legalized.

  • 1

    @Jefferson_andr consider this a note for future visitors ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.