e-Zest members share technology ideas to foster digital transformation.

Working with Python for Streaming Music from YouTube

Written by Rajat Agarwal | Nov 29, 2018 10:27:00 AM

Python is a high-level interpreted language used for general purpose programming. It is not just limited for general purpose programming but it can also be used for web programming using web frameworks like Django, Flask, Google App Engine, Zope and much more. Additionally, it can also be used to develop desktop applications or for games (pygame). Python is a dynamic language and can help you write a code in a significantly less number of lines.Python provides you with a large number of libraries, which can be imported and used to simplify coding. Moreover, to install those libraries Python provides a package management system called PIP which is used to install and manage software packages written in Python. PIP is included by default if you have Python 3.4 or 2.7 and above.

Pafy– A Python library to stream YouTube and retrieve metadata

Pafy is a Python library that provides functionality to stream YouTube and retrieve metadata related to YouTube videos or metadata of playlists on YouTube.

Pafy.new(): It is a function that accepts the YouTube video URL as an argument and returns a pafy object. Pafy objects hold the metadata of videos hosted on YouTube such as title, author, videoid, author etc.

A pafy object provides you with various data attributes.

Pafy Methods: The pafy.getbest(), pafy.getbestvideo() and pafy.getbestaudio() methods a gives you access to the highest quality video streams available on YouTube.

Playlist retrieval using pafy: The pafy.set_playlist() accepting YouTube playlist URL/playlist ID as arguments is a method that returns a Python dictionary containing metadata and pafy objects as key of the dict.

For documentation refer: https://Pythonhosted.org/Pafy/

Python-VLC– A Python library to play music using VLC media player

It requires a VLC media player to be installed on the system. It allows you to play music on VLC media player using a Python script. You just have to create an instance of VLC player and pass the path of a media file to be played to a play() function provided by Python-VLC library.

It can also be used to stream online music. You can also create a playlist using functions provided by the library and play the playlist from Python script itself.

For documentation refer: https://www.olivieraubert.net/VLC/Python-ctypes/doc/

APScheduler– A Python library to schedule a job

This is a Python library that lets you schedule your Python code/job to be executed later, eitheronce or periodically. You can add new tasks or remove old ones as per your choice.

For documentation refer: https://apscheduler.readthedocs.io/en/latest/

APScheduler has three built-in scheduling options:

  • Cronstyle scheduling (with optional start/end times)
  • Interval-based execution (runs jobs on even intervals, with optional start/end times)
  • One-off delayed execution (runs jobs once, on a set date/time)
Functionality implementation steps:

Accomplishment:


This project gave me an opportunity to discover various Python libraries and the microframework named Flask which allows you to produce a web app in a much easier way and comes with a server to host your web app.

I used Raspberry Pi 3 to host my web app. It gave me a platform to interact with microcomputers that can unfold a world of possibilities for you. It was an amazing experience to deploy my web app on Raspberry Pi and ask my friends to upvote a song they liked and hear it play.