Last active
December 17, 2023 13:16
-
-
Save ArmaanMcleod/8b891c4ec2cb480261f85b5b55438395 to your computer and use it in GitHub Desktop.
Download 720/1080p YouTube Video 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
import pytube | |
import ssl | |
import sys | |
import ffmpeg | |
import argparse | |
import os | |
ssl._create_default_https_context = ssl._create_stdlib_context | |
parser = argparse.ArgumentParser(description = 'Youtube Video Downloader') | |
parser.add_argument('-u', '--url', type = str, required = True, help = 'Video URL') | |
parser.add_argument('-r', '--resolution', type = str, required = True, choices = ['720p', '1080p'], help = 'Video Resolution') | |
args = parser.parse_args() | |
def show_progress_bar(stream, chunk, bytes_remaining): | |
current = (stream.filesize - bytes_remaining) / stream.filesize | |
percent = '{0:.1f}'.format(current * 100) | |
progress = int(50 * current) | |
status = '█' * progress + '-' * (50 - progress) | |
sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar = status, percent = percent)) | |
sys.stdout.flush() | |
yt = pytube.YouTube(args.url) | |
yt.register_on_progress_callback(show_progress_bar) | |
if args.resolution == '720p': | |
video = yt.streams.filter(res = args.resolution, file_extension = "mp4", progressive = True).first() | |
video.download(max_retries = 15, output_path = args.resolution) | |
elif args.resolution == '1080p': | |
video = yt.streams.filter(only_video = True, res = args.resolution, file_extension = "mp4").first() | |
audio = yt.streams.filter(only_audio = True, file_extension = "mp4").first() | |
video.download(filename_prefix = 'Video ', max_retries = 15, output_path = args.resolution) | |
audio.download(filename_prefix = 'Audio ', max_retries = 15, output_path = args.resolution) | |
video_input = ffmpeg.input(os.path.join(args.resolution, f"Video {yt.title}.mp4")) | |
audio_input = ffmpeg.input(os.path.join(args.resolution, f"Audio {yt.title}.mp4")) | |
merged_path = os.path.join(args.resolution, f"{yt.title}.mp4") | |
ffmpeg.output(audio_input, video_input, merged_path).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: