Python: How to Play Video using URL Resolver on Kodi

Ever seen video websites asking you to click on “I am not a robot”? Once you click on that checkbox, the video starts streaming. Well, if you stream a video in Kodi from the same website, URL Resolver can do all that for you and play the video. Kodi Play URL Resolver works in the background and knows how to navigate through certain hosts, get the video link from the URL, and then you can play it in Kodi.

URL Resolver is used by a lot of third-party Kodi addons and is installed as a dependency. In this post, we are going to take a look at how to play video using URL Resolver when developing a Kodi video addon. Some of the major video streaming websites that URL Resolver supports are:-

  • Youtube
  • Openload
  • Vimeo
  • Vidzi
  • Nowvideo
  • Novamov
How to Play Video using URL Resolver

Python How to Play Video using URL Resolver – Kodi

Pre-Requisites

Make sure you have read our previous article on how to play a video in Kodi using Python.

Also, install the Youtube Kodi addon from the official Kodi team. Kodi Home screen -> Addons -> Addon Manager -> Install from Repository -> Kodi Repository -> Video Addons -> Youtube.

Purpose of Python play video from URL

The purpose of this addon or plugin is to play a video in Kodi using URL Resolver. We have hardcoded three URLs, one is an mp4 file, the other one is a youtube video, and a random web URL (which is not a video). The mp4 file can be played using the inbuilt media player in Kodi, but the youtube video will be played using URL Resolver, and the third one cannot be played as it is not a video.

Addon name – plugin.video.v1d30play

Alert: Firestick and Kodi User

ISPs and the Government are constantly monitoring your online activities, If you are streaming copyrighted content through Firestick, Kodi or any other unauthorised streaming service ,It could lead you into trouble. Your IP Address:40.77.167.7 is publicly visible to everyone.

TheFirestickTV suggests to use VPN Service For Safe Streaming. We recommend you to use ExpressVPN, It's one of the safest and fast VPN available in the Market. You wil get 3 Months For Free if you buy 12 month plan.

3 Month Free on 1 year Plan at Just $6.67/Month

30-Days Money Back Guarantee

The code for this addon can be found here, and you can grab the addon zip from here.

Python How to Play Video using URL Resolver – Kodi

Add URL Resolver Dependency

The first step towards using the URL Resolver Kodi addon is to add it as a required module in the addon.xml file (line 5 of the source)

<import addon=”script.module.urlresolver” version=”3.0.0″/> <import addon=”script.module.urlresolver” version=”3.0.0″/>

Add Kodi URL Resolver Source

URL Resolver is automatically installed by a lot of third-party Kodi. To check if you have URL Resolver installed or not, follow our Real Debrid setup guide: How to setup Real Debrid

If you do not already have URL Resolver, you can add and install the noobsandnerds repository zip, and once you install our addon URL Resolver should be installed automatically.

Let’s Dive Into the Code

All the code for this addon is present in the file playvideo.py. We will only go through the differences of this addon when compared to the addon in the pre-requisites section.

Line 4

import URL resolver

We are importing URL Resolver classes and functions to be used in our code.

Line 13 – 14

_addon = xbmcaddon.Addon()_icon = _addon.getAddonInfo(‘icon’)

Here we get the addon name in the variable _addon and the addon icon on the variable _icon. We won’t be using these variables anywhere in the code as we changed the code, but it’s something new nonetheless.

Lines 21-45 will be covered later.

Lines 52-70

video_play_url = “http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4” url = build_url({‘mode’ :’play’, ‘playlink’ : video_play_url}) li = xbmcgui.ListItem(‘Play Video 1′, iconImage=’DefaultVideo.png’) li.setProperty(‘IsPlayable’ , ‘true’) xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)video_play_url = “https://www.youtube.com/watch?v=J9d9UrK0Jsw” url = build_url({‘mode’ :’play’, ‘playlink’ : video_play_url}) li = xbmcgui.ListItem(‘Play Video 2′, iconImage=’DefaultVideo.png’) li.setProperty(‘IsPlayable’ , ‘true’) xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)video_play_url = “www.reddit.com” url = build_url({‘mode’ :’play’, ‘playlink’ : video_play_url}) li = xbmcgui.ListItem(‘Play Video 3′, iconImage=’DefaultVideo.png’) li.setProperty(‘IsPlayable’ , ‘true’) xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li) xbmcplugin.endOfDirectory(addon_handle)

Here we create a menu similar to the one used in the addon in the pre-requisites section. The only difference is that we have added a third menu item called Play Video 3.

Lines 76 – 78

elif mode[0] == ‘play’: final_link = args[‘playlink’][0] play_video(final_link)

This is the piece of code that is executed when you select any of the menu items.

Lines 21 – 31

def resolve_url(url): duration=7500 #in milliseconds message = “Cannot Play URL” stream_url = urlresolver.HostedMediaFile(url=url).resolve() # If urlresolver returns false then the video url was not resolved. if not stream_url: dialog = xbmcgui.Dialog() dialog.notification(“URL Resolver Error”, message, xbmcgui.NOTIFICATION_INFO, duration) return False else: return stream_url

This is the piece of code for URL Resolver which we have added to this addon.

def resolve_url(url):

We just define a function called resolve_url which takes a URL as input. In our case, it will be the three hard-coded URLs when we click on the respective menu item.

duration=7500 #in milliseconds

We define a variable called duration, which we will use later in the code.

message = “Cannot Play URL”

We define a variable called message, which will be used later in the code.

stream_url = urlresolver.HostedMediaFile(url=url).resolve()

We are calling a function in URL Resolver to see if the URL we supplied as input can be resolved by URL Resolver or not. If URL Resolver can resolve or process that link, a playable link is returned. If it cannot resolve or process that link False is returned. We store the result in a variable called stream_url.

if not stream_url:

This is the statement, where we check what was returned. If False was returned, the code will go into the if loop, if a URL was returned (meaning the link was processed by URL Resolver) the code will go into else and return that URL.

dialog = xbmcgui.Dialog() dialog.notification(“URL Resolver Error”, message, xbmcgui.NOTIFICATION_INFO, duration) return False

Here we are creating a notification in Kodi to be displayed if URL Resolver returns False. Notice the use of message and duration variables.

Lines 33 – 45

def play_video(path): “”” Play a video by the provided path. :param path: str “”” # Create a playable item with a path to play. play_item = xbmcgui.ListItem(path=path) vid_url = play_item.getfilename() stream_url = resolve_url(vid_url) if stream_url: play_item.setPath(stream_url) # Pass the item to the Kodi player. xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)

The code is almost the same as for the addon in the pre-requisites section, except for a few lines that were added for URL Resolver.

vid_url = play_item.getfilename() stream_url = resolve_url(vid_url) if stream_url: play_item.setPath(stream_url)vid_url = play_item.getfilename()

Here we get the video URL from play_item.

stream_url = resolve_url(vid_url)

This is where we call the function we added for URL Resolver in lines 21-31. We will either get a resolved URL or a False one.

if stream_url: play_item.setPath(stream_url)

If we get False, we do not do anything. If we get a resolved URL we set the path for play item to the URL we got back.

Python How to Play Video using URL Resolver – Kodi

How the Code Executes

Let us go ahead and see what happens during runtime.

If you remember our last addon (the one in the pre-requisites section), which couldn’t play Youtube videos because we didn’t have a URL playable by Kodi inbuilt media player. Well, this add-on can play those Youtube videos, all credits to URL Resolver.

Play Video 2

Let’s say you click on Play Video 2. The play_video function is called as below.

play_video(https://www.youtube.com/watch?v=J9d9UrK0Jsw)

Once in the play_video function, the resolve_url function is called. In this case, it’s called as.

resolve_url(https://www.youtube.com/watch?v=J9d9UrK0Jsw)

URL Resolver will now work its magic and return us a playable link for the youtube link we just passed and the video will be played.

Play Video 3

When you click on Play Video 3, you will get an error notification because we know that link (www.reddit.com) is not a playable video file.

Play Video 1

This is an interesting one. When you click Play Video 1, you will get a notification that URL Resolver cannot Play the URL. This is because it’s already a playable link (direct mp4) or maybe the URL resolver cannot process that URL. But, the video will also play. We are yet to figure out a workaround for this but if you do let us know via comments below.

I hope you have got from this article “Python How to Play Video using URL Resolver – Kodi”

References

http://t0mm0.github.io/xbmc-urlresolver/modules/urlresolver/urlresolver.html

http://mirrors.kodi.tv/docs/python-docs/13.0-gotham/xbmcgui.html

Warning

TheFirestickTV.com Does Not Promote Or Encourage Any Illegal Use Of Kodi, FireStick Or any streaming services. Users Are Responsible For Their Actions.

Razi Haider

Hi! I'm Razi Haider, a Professional Electronic Engineer and part-time blogger. Being an avid cinephile and other content streamers myself, I started TheFirestickTV.com in 2020 to help others to access and stream good content on any platform. Here I do share all the tutorials and compilations of best services, write how-to articles, and cover all the major and minor updates regarding streaming, firestick, Kodi, Roku and other devices and platforms.