Home » Kodi » Python How to Play Video using URL Resolver – Kodi

Python How to Play Video using URL Resolver – Kodi

Last updated on August 14th, 2017 at 09:22 am EST

What is URL Resolver?

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. 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 which URL Resolver supports are :-

  • Youtube
  • Openload
  • Vimeo
  • Vidzi
  • Nowvideo
  • Novamov

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.

Python – How to Play a Video : Kodi

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

The purpose of this addon or plugin is to play a video in Kodi using URL Resolver. We have hardcoded three urls, one a 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

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 URL Resolver in your 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 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 till step 7.

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.

How to Install Noobsandnerds Repository

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 urlresolver

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 are get the addon name in the variable _addon, and 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 none the less.

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 addon in pre-requsites section. The only difference is that we have added a third menu item called Play Video 3.

URL Resolver menu

Lines 76 – 78

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

This is the piece of code which 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 url’s 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, code will go into the if loop, if a url was returned (meaning 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.

URL Resolver Error 001

 

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 pre-requisites section, except for a few lines which 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 False.

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 path for playitem 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 pre-requisites section), which couldn’t play Youtube videos because we didn’t have a url playable by Kodi inbuilt media player. Well, this addon can play those Youtube video, 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 play_video function, 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 it’s 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.

URL Resolver Error 001

Play Video 1

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

URL Resolver Play Video 1

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

Do you STREAM on Kodi WITHOUT a VPN?

We STRONGLY suggest you use a VPN Service like IPVanish.

Why VPN? 1. Stay Secure and Anonymous Online 2. Stream Content Anonymously 3. Access Geo Locked Content 4. Hide Your Activity from your ISP

Why IPVanish? 1. They Keep No Logs = No Tracking 2. Native apps for Android, Android TV, iOS, Mac, Linux 3. No Speed Slowdown 4. Highly Recommended 5. 7 Days Money Back Guarantee

Sign-Up for IPVanish

Follow Us

Close
Please support the site
By clicking any of these buttons you help our site to get better