You tube video downloader using python
In this post I am going to tell you how you can make Python Scripts for downloading the youtube
videos.
Requirements:-
- Make sure Python is installed in your Computer
- Good Internet Speed
- understanding of Python basics
- Libraries :- beautifulsoup4 , urllib ,requests
- IDE for coding (I am using jupyter notebook) you can use any IDE of your choice
Installations :-Well urllib are built in libraries so we only need to install the beautifulsoup4 library and requests.
- open the Command Prompt i.e cmd and enter the below code.
- pip install beautifulsoup4
- pip install requests
Write Program :-Lets Write the program.
Step 1:- Now import the below libraries in the program.
import bs4
import urllib.request
import requests
Step 2:- we will write a code to take video or playlist url as an input
Note:- this program cannot download the music/song videos.
playlist_url=input("Enter Playlist or Youtube URL")
Step 3:- Now we have the URL of the video or playlist then we will send the "get" request for the url and we get the html page as a response.
requested_page=requests.get(playlist_url)
Now if we try to print the "requested_page" it will print the status of the request
for example:-
For successful request status code is 200 that means our request is successful
print(requested_page)
output:-<Response [200]>
if you want to see the html content of the requested page then use the below code
print(requested_page.text)
step 3:- Now we will parse the content of the requested_page we received. for parsing we will use soup library.
soup=bs4.BeautifulSoup(requested_page.text,"html.parser")
soup = soup.prettify()
print(soup)
step 4:- we know that youtube playlist contains many videos and we want the links of each and every video for the further steps. for url extraction use the below code.
all_video_url=[]
for links in soup.find_all('a'):
links=links.get('href')
if str(links).find('/watch')>=0 and "https://www.youtube.com"+str(links) not in all_video_url and str(links).find('index')>=0:
all_video_url.append("https://www.youtube.com"+str(links))
else:
if len(all_video_url) is 0:
all_video_url.append(playlist_url)
print("Total Videos in the Playlist are ",len(all_video_url))
for e in all_video_url:
print(e)
step 5:- Now we have all the link of youtube video that need to be downloaded in the 'all_video_url' list. we need an API for finding the downloading url.
downloader_api="http://www.youtube-video-downloader.xyz/download?video="
def find_download_url(request_video_page):
soup=bs4.BeautifulSoup(request_video_page.text,"html.parser")
for each_tr_tag in soup.find_all('a'):
video_link=each_tr_tag.get('href')
if str(video_link).find('r')==8:
return str(video_link)
step 6:- Downloading of video ,In the below code replace the "videos/deep_learning/" in the full_path="videos/deep_learning/"+f"video{video_counter}"+".mp4" with your folder path where you wanna save the video.
video_counter=1
for each_url in all_video_url:
request_video_page=requests.get(downloader_api+each_url)
print(f"\nFinding the url of Video={video_counter}.....\n")
mp4_link=find_download_url(request_video_page)
video_title=mp4_link[mp4_link.find('title'):]
video_title=video_title[6:].replace("+"," ")
print("video title="+video_title)
print(f"{mp4_link}")
try:
print(" Downloading video="+str(video_counter)+"\n")
print("\n")
#path where u want to save the videos
#here enter your path
full_path="videos/deep_learning/"+f"video{video_counter}"+".mp4"
downloading=urllib.request.urlretrieve(str(mp4_link)[:mp4_link.find('&title')+1],full_path)
print(f"Downloading of Video{video_counter} is complete\n ")
except Exception as e:
print(e)
input("press anything to proceed")
print(f"Unable to Download video={video_counter}")
video_counter+=1
else:
print("All videos are downloaded")
Step 7:- Thats it
updated:- the above Script has been updated now on Github. plz use the latest Script
See the Demonstration of this Project
👍🏻
ReplyDeletenice trick will surely try this
ReplyDelete