Skip to content

Instantly share code, notes, and snippets.

@carnal0wnage
Last active May 27, 2025 23:45
Show Gist options
  • Save carnal0wnage/41ec4dc8b5ef93fa1ee10197509fae33 to your computer and use it in GitHub Desktop.
Save carnal0wnage/41ec4dc8b5ef93fa1ee10197509fae33 to your computer and use it in GitHub Desktop.
download a youtube file as 720, 1080, or best (you can pass whatever exists tbh)
import yt_dlp
def download_video(url, resolution="best"):
try:
# Set up yt-dlp options
if resolution in ["720", "1080"]:
# If the user specifies a resolution, prioritize that
ydl_opts = {
'format': f'bestvideo[height<={resolution}]+bestaudio/best', # Download best video with audio at requested resolution
'outtmpl': '%(title)s.%(ext)s', # Output filename template (keeps original file extension)
'noplaylist': True, # Skip playlists
'postprocessors': [], # Avoid any post-processing (i.e., no re-encoding)
'merge_output_format': 'mp4', # Ensure the output is in MP4 format if merging streams
}
else:
# If 'best' is specified, download the best available resolution
ydl_opts = {
'format': 'bestvideo+bestaudio/best', # Download the best video and audio quality available
'outtmpl': '%(title)s.%(ext)s', # Output filename template (keeps original file extension)
'noplaylist': True, # Skip playlists
'postprocessors': [], # Avoid any post-processing (i.e., no re-encoding)
'merge_output_format': 'mp4', # Ensure the output is in MP4 format if merging streams
}
# Use yt-dlp to download the video
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
print(f"Downloading video from {url} in {resolution}...")
ydl.download([url])
print("Download completed!")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
url = input("Enter the YouTube video URL: ")
resolution = input("Enter desired resolution (720, 1080, or best): ")
download_video(url, resolution)
# python3 py-youtube-dl.py
# Enter the YouTube video URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ
# Enter desired resolution (720, 1080, or best): 1080
# Downloading video from https://www.youtube.com/watch?v=dQw4w9WgXcQ in 1080...
# [youtube] Extracting URL: https://www.youtube.com/watch?v=dQw4w9WgXcQ
# [youtube] dQw4w9WgXcQ: Downloading webpage
# [youtube] dQw4w9WgXcQ: Downloading tv client config
# [youtube] dQw4w9WgXcQ: Downloading tv player API JSON
# [youtube] dQw4w9WgXcQ: Downloading ios player API JSON
# [youtube] dQw4w9WgXcQ: Downloading m3u8 information
# [info] dQw4w9WgXcQ: Downloading 1 format(s): 616+251
# [hlsnative] Downloading m3u8 manifest
# [hlsnative] Total fragments: 39
# [download] Destination: XXXXXX (Official Music Video).f616.mp4
# [download] 100% of 94.86MiB in 00:00:09 at 10.26MiB/s
# [download] Destination: XXXXXX (Official Music Video).f251.webm
# [download] 100% of 3.28MiB in 00:00:01 at 2.93MiB/s
# [Merger] Merging formats into "XXXXXX (Official Music Video).mp4"
# Deleting original file XXXXXX (Official Music Video).f616.mp4 (pass -k to keep)
# Deleting original file XXXXXX (Official Music Video).f251.webm (pass -k to keep)
# Download completed!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment