Created
May 16, 2018 09:34
-
-
Save Koasing/03f2f42d16a12c7938dbee6b6845f8a4 to your computer and use it in GitHub Desktop.
pytube download script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
from pytube import YouTube | |
def download(url): | |
def progress_callback(stream, chunk, file_handle, bytes_remaining): | |
# this function is called by download process... do not call directly! | |
total_size = stream.filesize | |
downloaded_size = total_size - bytes_remaining | |
progress = downloaded_size / total_size * 100 | |
print('{:.3f}% downloaded'.format(progress), end='\r') | |
def complete_callback(stream, file_handle): | |
# this function is called by download process... do not call directly! | |
# do nothing but print newline | |
print() | |
yt = YouTube(url, on_progress_callback=progress_callback, on_complete_callback=complete_callback) | |
print(yt.title) | |
# video | |
# pytube's `order_by('resolution')` sorts streams by lexicographical order. do not use it. | |
video_progressive = max(yt.streams.filter(progressive=True).all(), key=lambda x: int(x.resolution[:-1])) | |
video_adaptive = max(yt.streams.filter(only_video=True).all(), key=lambda x: int(x.resolution[:-1])) | |
if video_progressive and video_adaptive: | |
# compare two resolution | |
progressive_res = int(video_progressive.resolution[:-1]) | |
adaptive_res = int(video_adaptive.resolution[:-1]) | |
if progressive_res >= adaptive_res: | |
print('Download progressive video codec={vid.video_codec} res={vid.resolution}, audio codec={vid.audio_codec}'.format(vid=video_progressive)) | |
video_progressive.download(filename=yt.title+'_progressive') | |
if video_adaptive: | |
print('Download video codec={vid.video_codec} res={vid.resolution}'.format(vid=video_adaptive)) | |
video_adaptive.download(filename=yt.title+'_video') | |
# audio | |
audio_adaptive = None | |
if video_adaptive and video_adaptive.subtype == 'mp4': | |
audio_adaptive = yt.streams.filter(only_audio=True, file_extension='mp4').order_by('bitrate').desc().first() | |
else: | |
audio_adaptive = yt.streams.filter(only_audio=True, audio_codec='vorbis').order_by('bitrate').desc().first() | |
if audio_adaptive is None: | |
# grab any audio tracks | |
audio_adaptive = yt.streams.filter(only_audio=True).order_by('bitrate').desc().first() | |
if audio_adaptive: | |
print('Download audio codec={aud.audio_codec} bitrate={aud.bitrate}'.format(aud=audio_adaptive)) | |
audio_adaptive.download(filename=yt.title+'_audio') | |
if audio_adaptive and audio_adaptive.subtype == 'mp4': | |
audio_alternative = yt.streams.filter(only_audio=True, audio_codec='vorbis').order_by('bitrate').desc().first() | |
if audio_alternative: | |
print('Download audio codec={aud.audio_codec} bitrate={aud.bitrate}'.format(aud=audio_alternative)) | |
audio_alternative.download(filename=yt.title+'_vorbis') | |
def main(): | |
# argument parser | |
parser = argparse.ArgumentParser(description='YouTube Download') | |
parser.add_argument('urls', nargs='+', metavar='URL') | |
args = parser.parse_args() | |
print(args.urls) | |
for url in args.urls: | |
download(url) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment