개발/문서 요약

pytube 설치

pytube 유튜브 동영상을 다운로드 하기 위한 파이썬 라이브러리입니다.

 

pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

 

조금 더 자세한 설명은 더보기를 누르셔서 확인하실 수 있습니다.

더보기

YouTube is the most popular video-sharing platform in the world and as a hacker you may encounter a situation where you want to script something to download videos. For this I present to you pytube.

pytube is a lightweight library written in Python. It has no third party dependencies and aims to be highly reliable.

 

pytube also makes pipelining easy, allowing you to specify callback functions for different download events, such as  on progress or on complete.

 

Finally pytube also includes a command-line utility, allowing you to quickly download videos right from terminal.

 

pytube를 검색하니 pytube와 pytube3이 나왔습니다. 각각 document 홈페이지를 가지고 있어서 무엇을 사용해야 할지 고민되었습니다.

연결된 GitHub를 확인하니 pytube3에서는 주소를 찾을 수 없는 반면 pytube는 운영 중인 git을 볼 수 있었습니다.

github.com/nficano/pytube

 

nficano/pytube

A lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos. - nficano/pytube

github.com

pytube3과의 차이는 천천히 알아보도록 하겠습니다.

pytube를 다음과 같이 설치합니다.

python -m pip install pytube

 

더보기
➜  ~ python -m pip install pytube
Collecting pytube
  Downloading pytube-10.0.0-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 1.1 MB/s 
Collecting typing-extensions
  Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Installing collected packages: typing-extensions, pytube
Successfully installed pytube-10.0.0 typing-extensions-3.7.4.3

설치가 간단하게 되었습니다.

 

제공하는 방법으로 간단히 사용해봅시다.

 

 

from pytube import YouTube
YouTube('https://youtube.com/watch?v=2lAe1cqCOXo').streams.first().download()

이 영상은 유튜브 공식 계정에서 올린 YouTube Rewind 2019: For the Record라는 영상입니다. 싫어요가 928만이네요.

 

다운로드한 유튜브 영상

import를 제외한 단 한 줄로 동영상을 받을 수 있습니다.

 

이 외에 다른 API를 사용해봅시다. API는 python-pytube.readthedocs.io/en/latest/index.html에서 확인하실 수 있습니다.

 

동영상의 자막 정보를 제공해 주는 API입니다. 영상뿐만 아니라 자막도 받을 수 있습니다.

yt.captions

 

더보기
{'ar': <Caption lang="Arabic" code="ar">, 'zh-HK': <Caption lang="Chinese (Hong Kong)" code="zh-HK">, 'zh-TW': <Caption lang="Chinese (Taiwan)" code="zh-TW">, 'hr': <Caption lang="Croatian" code="hr">, 'cs': <Caption lang="Czech" code="cs">, 'da': <Caption lang="Danish" code="da">, 'nl': <Caption lang="Dutch" code="nl">, 'en': <Caption lang="English" code="en">, 'en-GB': <Caption lang="English (United Kingdom)" code="en-GB">, 'et': <Caption lang="Estonian" code="et">, 'fil': <Caption lang="Filipino" code="fil">, 'fi': <Caption lang="Finnish" code="fi">, 'fr-CA': <Caption lang="French (Canada)" code="fr-CA">, 'fr-FR': <Caption lang="French (France)" code="fr-FR">, 'de': <Caption lang="German" code="de">, 'el': <Caption lang="Greek" code="el">, 'iw': <Caption lang="Hebrew" code="iw">, 'hu': <Caption lang="Hungarian" code="hu">, 'id': <Caption lang="Indonesian" code="id">, 'it': <Caption lang="Italian" code="it">, 'ja': <Caption lang="Japanese" code="ja">, 'ko': <Caption lang="Korean" code="ko">, 'lv': <Caption lang="Latvian" code="lv">, 'lt': <Caption lang="Lithuanian" code="lt">, 'ms': <Caption lang="Malay" code="ms">, 'no': <Caption lang="Norwegian" code="no">, 'pl': <Caption lang="Polish" code="pl">, 'pt-BR': <Caption lang="Portuguese (Brazil)" code="pt-BR">, 'pt-PT': <Caption lang="Portuguese (Portugal)" code="pt-PT">, 'ro': <Caption lang="Romanian" code="ro">, 'ru': <Caption lang="Russian" code="ru">, 'sk': <Caption lang="Slovak" code="sk">, 'es-419': <Caption lang="Spanish (Latin America)" code="es-419">, 'es-ES': <Caption lang="Spanish (Spain)" code="es-ES">, 'sv': <Caption lang="Swedish" code="sv">, 'th': <Caption lang="Thai" code="th">, 'tr': <Caption lang="Turkish" code="tr">, 'uk': <Caption lang="Ukrainian" code="uk">, 'ur': <Caption lang="Urdu" code="ur">, 'vi': <Caption lang="Vietnamese" code="vi">}

공식 계정이다 보니 많은 자막을 지원하네요. 좋은 샘플입니다.

 

한국어 자막을 확인해봅시다. get_by_language_code API를 이용하여 얻을 수 있습니다. 공식 문서에서 이를 간단하게 제공합니다.

yt.captions['ko']

<Caption lang="Korean" code="ko">	#출력값

대 괄호 안에 원하는 언어의 code를 입력하여 자막을 얻을 수 있습니다.

 

자막을 얻었으니 추출해봅시다. 자막은 xml 포맷과 srt 포맷으로 얻을 수 있습니다.

Korea_caption=yt.captions['ko']
Korea_caption.generate_srt_captions()	#srt 포맷
Korea_caption.xml_captions		#xml 포맷

선호하시는 모습에 따라 선택하면 되겠습니다.

 

자막 다운로드 API는 다음과 같이 제공합니다.

download(title: str, srt: bool = True, output_path: Optional[str] = None, filename_prefix: Optional[str] = None)
  • title(str) : 파일 이름입니다.
  • srt : True면 srt포맷으로, False면 xml 포맷으로 출력합니다. default 값은 True입니다.
  • output_path : 파일이 저장되는 경로를 나타냅니다.
  • filename_prefix : 파일 이름 앞에 붙는 prefix입니다.
Korea_caption.download('title', True, '.', 'sample_')

자막 파일 sample_title (ko).srt이 현재 경로에 생성되는 것을 확인할 수 있습니다.

 

 

pytube 라이브러리를 설치하고 영상과 자막을 다운로드 해보았습니다. 이뿐만 아니라 영상에 대한 정보를 얻을 수 있습니다.

 

 

추후에 YouTube API와 비교해봐야겠습니다.