Skip to content

Instantly share code, notes, and snippets.

@ArmaanMcleod
Last active December 17, 2023 13:16
Show Gist options
  • Save ArmaanMcleod/8b891c4ec2cb480261f85b5b55438395 to your computer and use it in GitHub Desktop.
Save ArmaanMcleod/8b891c4ec2cb480261f85b5b55438395 to your computer and use it in GitHub Desktop.
Download 720/1080p YouTube Video Script
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()
@ArmaanMcleod
Copy link
Author

Usage:

python download-youtube-video.py --resolution 1080p --url https://www.youtube.com/watch?v=Sagg08DrO5U

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment