How to add Youtube video player in JARVIS - Python

Hello, and welcome to our Developer Service


Youtube video player in JARVIS 











Today we will give you the code of Youtube video player in JARVIS 

Follow these steps to add Youtube video player in JARVIS

  • First, make a .py file name - youtubeD, and paste these codes in that file
import request
from bs4 import BeautifulSoup
import webbrowser


def youtube(video):
    try:
        yt_url = "https://www.youtube.com/"
        url = "https://www.youtube.com/results?search?_query"
        tmp = video.replace(' ','+')
        url = url+tmp
        source_code = request.get(url)
        plain_text = source_code.text 
        soup = BeautifulSoup(plain_text,'html.praser')
        url_list=[]
        for link in soup.findAll('a',{'dir':'ltr'}):
            herf = link.get('herf')
            if '/watch?' in herf:
                url_list.append(herf)
                ping = yt_url+url_list[0]

        
 webbrowser.open(ping)
 except:
     print ("ERROR...Internet connection lost") 
  • Save the file and keep this .py file in JARVIS folder
  • Now open JARVIS file and paste all following codes in the file 
  • Paste this code under - import os -
import youtubeD
  • Paste this code in #for main function
This is the command which will open Youtube if a user tells to JARVIS

elif'play video from Youtube' in query:
            query = query.replace("youtube",")
            youtubeD.youtube(query)
            speak("Opening video on Youtube") 

Final Code with Youtube video player in JARVIS of .py file of JARVIS -

#Project jarvis
import speech_recognition as sr 
import datetime
import wikipedia
import pyttsx3
import webbrowser
import random
import os
import videoTester
import youtubeD

#Text To Speech

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice',voices[0].id)

def speak(audio):  #here audio is var which contain text
    engine.say(audio)
    engine.runAndWait()

def wish():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour<12:
        speak("good morning sir i am virtual assistent MB")
    elif hour>=12 and hour<18:
        speak("good afternoon sir i am virtual assistent MB") 
    else:
        speak("good night sir i am virtual assistent MB")  

#now convert audio to text
def takecom():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listning....")
        audio = r.listen(source)
    try:
        print("Recognising.") 
        text = r.recognize_google(audio,language='en-in')
        print(text)
    except Exception:                #For Error handling
        speak("error...")
        print("Network connection error") 
        return "none"
    return text

#for main function                               
if __name__ == "__main__":
    if videoTester.live != 1:
        exit(0)
    wish()
    while True:
        query = takecom().lower()

        if "wikipedia" in query:
            speak("searching details....Wait")
            query.replace("wikipedia","")
            results = wikipedia.summary(query,sentences=2)
            print(results)
            speak(results)
        elif 'open youtube' in query or "open video online" in query:
            webbrowser.open("www.youtube.com")
            speak("opening youtube")
        elif 'open github' in query:
            webbrowser.open("https://www.github.com")
            speak("opening github")  
        elif 'open facebook' in query:
            webbrowser.open("https://www.facebook.com")
            speak("opening facebook")      
        elif 'open instagram' in query:
            webbrowser.open("https://www.instagram.com")
            speak("opening instagram")    
        elif 'open google' in query:
            webbrowser.open("https://www.google.com")
            speak("opening google")

        elif 'open tontuf' in query:
            webbrowser.open("https://tontuf.blogspot.com/")
            speak("opening tontuf")    
            
        elif 'open yahoo' in query:
            webbrowser.open("https://www.yahoo.com")
            speak("opening yahoo")
            
        elif 'open gmail' in query:
            webbrowser.open("https://mail.google.com")
            speak("opening google mail") 
            
        elif 'open snapdeal' in query:
            webbrowser.open("https://www.snapdeal.com") 
            speak("opening snapdeal")  
             
        elif 'open amazon' in query or 'shop online' in query:
            webbrowser.open("https://www.amazon.com")
            speak("opening amazon")
        elif 'open flipkart' in query:
            webbrowser.open("https://www.flipkart.com")
            speak("opening flipkart")   
        elif 'open ebay' in query:
            webbrowser.open("https://www.ebay.com")
            speak("opening ebay")
        elif 'music from pc' in query or "music" in query:
            speak("ok i am playing music")
            music_dir = './music'
            musics = os.listdir(music_dir)
            os.startfile(os.path.join(music_dir,musics[0]))
        elif 'video from pc' in query or "video" in query:
            speak("ok i am playing videos")
            video_dir = './video'
            videos = os.listdir(music_dir)
            os.startfile(os.path.join(video_dir,videos[0]))  
        elif 'good bye' in query:
            speak("good bye")
            exit()
        elif "shutdown" in query:
            speak("shutting down")
            os.system('shutdown -s') 
        elif "what\'s up" in query or 'how are you' in query:
            stMsgs = ['Just doing my thing!', 'I am fine!', 'Nice!', 'I am nice and full of energy','i am okey ! How are you']
            ans_q = random.choice(stMsgs)
            speak(ans_q)  
            ans_take_from_user_how_are_you = takecomm()
            if 'fine' in ans_take_from_user_how_are_you or 'happy' in ans_take_from_user_how_are_you or 'okey' in ans_take_from_user_how_are_you:
                speak('okey..')  
            elif 'not' in ans_take_from_user_how_are_you or 'sad' in ans_take_from_user_how_are_you or 'upset' in ans_take_from_user_how_are_you:
                speak('oh sorry..')  
        elif 'make you' in query or 'created you' in query or 'develop you' in query:
            ans_m = " For your information I am a AI created by Tontuf.in to help peoples like you "
            print(ans_m)
            speak(ans_m)
        elif "who are you" in query or "about you" in query or "your details" in query:
            about = "I am MB an A I based computer program but i can help you lot like a your close friend ! i promise you ! Simple try me to give simple command ! like playing music or video from your directory i also play video and song from web or online ! i can also entain you i so think you Understand me ! ok Lets Start "
            print(about)
            speak(about)
        elif "hello" in query or "hello MB" in query:
            hel = "Hello Sir ! How May i Help you.."
            print(hel)
            speak(hel)
        elif "your name" in query or "sweat name" in query:
            na_me = "Thanks for Asking my name my self ! MB"  
            print(na_me)
            speak(na_me)
        elif "you feeling" in query:
            print("feeling Very sweet after meeting with you")
            speak("feeling Very sweet after meeting with you") 
        elif query == 'none':
            continue 
        elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query :
            ex_exit = 'I feeling very sweet after meeting with you but you are going! i am very sad'
            speak(ex_exit)
            exit()
        elif'play video from Youtube' in query:
            query = query.replace("youtube",")
            youtubeD.youtube(query)
            speak("Opening video on Youtube")
        else:
            temp = query.replace(' ','+')
            g_url="https://www.google.com/search?q="    
            res_g = 'sorry! i cant understand but i search from internet to give your answer ! okay'
            print(res_g)
            speak(res_g)
            webbrowser.open(g_url+temp)                                

  You can also watch this video to do this


Credits - Himesh Kumar Arya

Post a Comment

Post a Comment (0)

Previous Post Next Post