Open In App

Automate Youtube with Python

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to automate Youtube using Python.

Module used

  • Selenium: It controlling a web browser through the program
  • pyttsx3: It is a text-to-speech conversion library in Python
  • speech_recognition: Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc
  • pyaudio: It is used to play and record audio on a variety of platforms

Make sure that you have noted the location where the chromedriver has been downloaded (as it is used in our python script). Now After downloading extract the zip file and please note the file location of the extracted file as we have needed it later in python code. (You can find the location by clicking on properties and then details).

Approach

  • Import the library which we have installed.
  • Then we have to take the input search query using the speech_recognition library. we can do this by making an instance of speech_recognition library as sr.Recognizer() .
  • After this adjusting the threshold frequency and convert the voice input into a string.
  • Then, the main part comes into the picture, we have created a function automateYoutube() which plays the required video from Youtube.
  • In this function create we driver instance using function webdriver.Chrome() which takes the path of chromedriver as the parameter.
  • Finally, find the name or id or class or CSS selector of the search bar and search button by right-clicking inspect search bar and .search button.
  • Now, just call the automateYoutube() function to see the output.
  • Now we have to automate play/pause buttons for that we again find the CSS selector of play/pause button using selenium.

Below is the Full Implementation

Python3




from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import speech_recognition as sr
import pyttsx3
import time
 
 
def automateYoutube(searchtext):
   
    # giving the path of chromedriver to selenium webdriver
    path = "C:\\Users\\hp\\Downloads\\chromedriver"
 
    url = "https://www.youtube.com/"
     
    # opening the youtube in chromedriver
    driver = webdriver.Chrome(path)
    driver.get(url)
 
    # find the search bar using selenium find_element function
    driver.find_element_by_name("search_query").send_keys(searchtext)
 
    # clicking on the search button
    driver.find_element_by_css_selector(
        "#search-icon-legacy.ytd-searchbox").click()
 
    # For finding the right match search
    WebDriverWait(driver, 0).until(expected_conditions.title_contains(MyText))
 
    # clicking on the match search having same as in searched query
    WebDriverWait(driver, 30).until(
        expected_conditions.element_to_be_clickable((By.ID, "img"))).click()
 
    # while(True):
    #     pass
 
 
speak = sr.Recognizer()
try:
    with sr.Microphone() as speaky:
       
        # adjust the energy threshold based on
        # the surrounding noise level
        speak.adjust_for_ambient_noise(speaky, duration=0.2)
        print("listening...")
         
        # listens for the user's input
        searchquery = speak.listen(speaky)
         
        # Using google to recognize audio
        MyText = speak.recognize_google(searchquery)
        MyText = MyText.lower()
 
except sr.RequestError as e:
    print("Could not request results; {0}".format(e))
 
except sr.UnknownValueError:
    print("unknown error occurred")
 
# Calling the function
automateYoutube(MyText)


Output:



Previous Article
Next Article

Similar Reads

Automate YouTube Music to Spotify with Python
Sometimes we listen to a song on YouTube music, and we really found that song interesting, and you want to add that to your Spotify playlist. Then you manually search that song, add it to the Spotify playlist. But we can automate that process of searching YouTube songs and add them into Spotify playlist using python programming. In this article, we
7 min read
How to Extract YouTube Comments Using Youtube API - Python
Prerequisite: YouTube API Google provides a large set of API’s for the developer to choose from. Each and every service provided by Google has an associated API. Being one of them, YouTube Data API is very simple to use provides features like – Search for videosHandle videos like retrieve information about a video, insert a video, delete a video et
2 min read
GUI to get views, likes, and title of a YouTube video using YouTube API in Python
Prerequisite: YouTube API, Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this art
2 min read
Python | Automate Google Search using Selenium
Google search can be automated using Python script in just 2 minutes. This can be done using selenium (a browser automation tool). Selenium is a portable framework for testing web applications. It can automatically perform the same interactions that any you need to perform manually and this is a small example of it. Mastering Selenium will help you
3 min read
Automate linkedin connections using Python
LinkedIn connections are a very important thing for an IT professional, so we need to send connection requests to a lot of people who can be useful to us. But sometimes sending connection requests one at a time can be a little annoying and hectic. It would be nice to automate this work but How? Python to rescue!In this article, we will learn how to
3 min read
Python Script to Automate Refreshing an Excel Spreadsheet
In this article, we will learn how we can automate refreshing an Excel Spreadsheet using Python. So to open the Excel application and workbooks we are going to use the pywin32 module. You can install the module using the below code: pip install pywin32 Then we are going to open the Excel application using the win32com.client.Dispatch() method and w
1 min read
Automate getter-setter generator for Java using Python
Encapsulation is defined as the wrapping up of data under a single unit. Encapsulation can be achieved by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. These public methods are called getters and setters. In practice, it is quite a time consuming and repetitive pr
3 min read
How to Automate VPN to change IP location on Ubuntu using Python?
To Protect Our system from unauthorized users Access you can spoof our system's IP Address using VPN service provided by different organizations. You can set up a VPN on your system for free. After you set up and log in to the VPN over the Ubuntu system you need to manually connect with different VPN servers after some duration. We can automate it
3 min read
How to Automate an Excel Sheet in Python?
Before you read this article and learn automation in Python....let's watch a video of Christian Genco (a talented programmer and an entrepreneur) explaining the importance of coding by taking the example of automation. You might have laughed loudly after watching this video and you surely, you might have understood the importance of automation in r
8 min read
How to Automate the Storage using Dropbox API in Python ?
In this Data Era Storing, Managing, and organizing the Data is an important factor considered by every business. Dropbox is one of the most popular cloud storage systems on the market, and it continues to improve its features. In this article, we will be demonstrating how one can connect to dropbox API using Python and perform Storage Automation ef
2 min read
three90RightbarBannerImg