Playing Youtube Video using Python

Playing YouTube videos using Python is a powerful way to enhance your multimedia projects. Python's flexibility and extensive libraries provide developers with the tools to interact with YouTube's vast video collection programmatically. By leveraging the pytube library, developers can easily download YouTube videos and play them directly within their Python applications.

In this article, we will guide you through the process of playing YouTube videos using Python step by step. Whether you're looking to integrate videos into your software or simply explore the possibilities of YouTube data manipulation.

What is a pytube module?

The pytube module is a useful Python library that simplifies the interaction with YouTube. It offers a user-friendly and straightforward approach to downloading videos from YouTube and gathering various video-related information. By leveraging the pytube module, developers gain the ability to programmatically connect with YouTube's vast video collection.

Below is the code to import the pytube module −

pip install pytube

They can effortlessly retrieve details like video titles, thumbnail URLs, and video streams of different qualities. The module provides an efficient API that allows users to specify their preferred video resolution, format, and other parameters. Overall, the pytube module streamlines the process of working with YouTube videos in Python, empowering developers to seamlessly integrate YouTube functionality into their applications.

What is a webbrowser module in Python?

The `webbrowser` module in Python is a built-in library that enables developers to interact with web browsers from within their Python programs. It provides a simple and platform-independent way to open web pages, URLs, and files in the user's default web browser. With the `webbrowser` module, developers can automate the process of launching web content, such as displaying online documentation, opening external links, or playing YouTube videos.

Below is the code to import the webbrowser module −

pip install webbrowser

This module abstracts the complexities of different operating systems and web browsers, allowing for seamless integration and a consistent experience across platforms. The `webbrowser` module is a valuable tool for Python developers who want to incorporate web browsing capabilities into their applications or automate browser interactions.

How to play a youtube video using Python?

Below is the step-by-step implementation of the program that plays a YouTube video and displays its details −

  • Import the necessary modules:

    • pytube module is imported to interact with YouTube and download videos.

    • webbrowser module is imported to open the video in a web browser.

  • Prompt the user to enter the YouTube video URL using the input() function and store it in the variable video_url.

  • Create a YouTube object by passing the video_url to the YouTube class from the pytube module. This object represents the YouTube video that will be played.

  • Retrieve the video's title using the title attribute of the YouTube object and store it in the variable video_title. This provides the title of the YouTube video.

  • Retrieve the thumbnail URL of the video using the thumbnail_url attribute of the YouTube object and store it in the variable thumbnail_url. This URL points to the thumbnail image of the YouTube video.

  • Open the video in a web browser using the function and passing the video_url as the argument. This function opens the specified URL in the default web browser of your system, allowing you to watch the video.

  • Display the video details using the print() function. The program prints "Playing YouTube video:", followed by the video's title and thumbnail URL retrieved in steps 4 and 5.

Below is the sample program for Playing Youtube Video using Python −


from pytube import YouTube
import webbrowser

# Input the YouTube video URL
video_url = input("Enter the YouTube video URL: ")

# Create a YouTube object
yt = YouTube(video_url)

# Get the video title and thumbnail URL
video_title = yt.title
thumbnail_url = yt.thumbnail_url

# Open the video in a web browser

# Display the video details
print("Playing YouTube video:")
print("Title:", video_title)
print("Thumbnail URL:", thumbnail_url)


Enter the YouTube video URL:
Playing YouTube video:
Title: Rick Astley - Never Gonna Give You Up (Video)
Thumbnail URL:

The program then completes its execution, and you can watch the YouTube video in the web browser that opened. The video details, including the title and thumbnail URL, are also displayed on the console output.


In conclusion, playing YouTube videos using Python opens up a world of possibilities for enhancing multimedia projects. By utilizing the pytube library, developers can effortlessly download and integrate YouTube videos into their Python applications. The pytube module provides a seamless way to interact with YouTube's vast video collection, enabling developers to retrieve video details and customize the playback experience.

Additionally, we learned that leveraging the webbrowser module allows for convenient opening of YouTube videos in the user's default web browser.

Updated on: 24-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started