Skip to content

Instantly share code, notes, and snippets.

@ozgurozkok
Created January 6, 2023 20:14
Show Gist options
  • Save ozgurozkok/b43eab15d6a7bef10397d1eed0dc0b49 to your computer and use it in GitHub Desktop.
Save ozgurozkok/b43eab15d6a7bef10397d1eed0dc0b49 to your computer and use it in GitHub Desktop.
How to Build a Python Program to Download YouTube Videos
# Import the necessary libraries
from pytube import YouTube
import ffmpeg
import urllib
# Set the URL of the YouTube video you want to download
url = 'https://www.youtube.com/watch?v=VIDEO_ID'
# Use the pytube library to access the YouTube video and retrieve its streaming data
yt = YouTube(url)
stream = yt.streams.first()
# Use the ffmpeg library to convert the streaming data into a video file
video_file = ffmpeg.input(stream.url)
video_file = ffmpeg.output(video_file, 'output.mp4')
# Use Python's built-in urllib library to download the video file
urllib.request.urlretrieve(stream.url, 'output.mp4')
# Save the video file to your desired location
with open('output.mp4', 'wb') as f:
f.write(video_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment