0

Is there a way in python to download the video video (no matter if it is an mp4 or a youtube embed) of a website.

For example:

def download_video(url):
   #download the video

download_video("mywebiste.html")
Billi Alt
  • 51
  • 5
  • Just read the html file as a String, then search the string for a first occurrence of some `.mp4` link. – VC.One Jul 22 '22 at 14:22

1 Answers1

2

Sorry, I can't comment (only answer), so I'll show an example of program and give a link:

# importing the module 
from pytube import YouTube 

# where to save 
SAVE_PATH = "E:/" #to_do 

# link of the video to be downloaded 
link="https://www.youtube.com/watch?v=xWOoBJUqlbI"

try: 
    # object creation using YouTube
    # which was imported in the beginning 
    yt = YouTube(link) 
except: 
    print("Connection Error") #to handle exception 

# filters out all the files with "mp4" extension 
mp4files = yt.filter('mp4') 

#to set the name of the file
yt.set_filename('GeeksforGeeks Video')  

# get the video with the extension and
# resolution passed in the get() function 
d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution) 
try: 
    # downloading the video 
    d_video.download(SAVE_PATH) 
except: 
    print("Some Error!") 
print('Task Completed!') 
Beholder
  • 111
  • 8
  • Ok and is there a way to add an if statement for blob videos? – Billi Alt Jul 21 '22 at 11:16
  • https://stackoverflow.com/questions/48034696/python-how-to-download-a-blob-url-video - check this. Just "download" blob* is impossible there written. – Beholder Jul 21 '22 at 11:25
  • ok thank you for your answer. I tried your initial code snippet, but it returns the following error code: 'YouTube' object has no attribute 'filter' – Billi Alt Jul 21 '22 at 11:52
  • I haven't worked with this library since I didn't need it. There is much info in the Inretner about downloading files from YouTube with Python. Just search - you will find not only one library and not only one code. This code is copy-pasted from the linked site. Come in there for more information. You can also look up [here](https://geekyhumans.com/download-youtube-videos-using-python/) and [here where one more short code is](https://stackoverflow.com/questions/40713268/download-youtube-video-using-python-to-a-certain-directory). This attribute may no longer be revelant. – Beholder Jul 21 '22 at 12:16