Home » Kodi » Python How To Play a Video – Kodi

Python How To Play a Video – Kodi

Python How To Play a Video – Kodi

python how to play a video

Pre-Requisites

Make sure you have read our previous article on sys.argv explained which also tells you how to build menus. In case you haven’t hit the link below.

Kodi Addon Development : sys.argv Explained, Building Menus

Purpose

The purpose of this addon or plugin is to play a video in Kodi. We have hardcoded two videos, one a mp4 file and the other one is a youtube video. The mp4 file can be played using the inbuilt media player in Kodi, but the youtube video cannot be. We will figure out how to play the Youtube video using URL Resolver in another tutorial.

Addon name – plugin.video.playv1d30

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

Python How to Play A Video – Kodi

Let’s Dive Into the Code

All the code for this addon is present in the file playvideo.py. We will start with code at lines 30 – 48 and come back to function play_video (lines 15-24). Lines 1-14 are covered in the how to link in pre-requisites section.

mode = args.get('mode', None)

check what the mode is set to. This is covered in more detail in the how to link in pre-requisites section.

Create a Menu With two Links

if mode is None:
 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)

xbmcplugin.endOfDirectory(addon_handle)

We just created a menu with options as Play Video 1 and Play Video 2. Play Video 1 links to the mp4 video. Play Video 2 links to the youtube video.  The important piece of code to note here is:-

li.setProperty('IsPlayable' , 'true')

This is a mandatory property which you need to set for playable media. This is the piece of code which is executed when the addon is first started.

create menu kodi addon

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

When you select either Play Video 1 or Play Video 2 this is the piece of code which is executed.

When you select Play Video 1, the value of sys.argv[2] is

plugin://plugin.video.demo1/?mode=play&playlink=http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4

final_link = args['playlink'][0]

makes final_link as http://www.vidsplay.com/wp-content/uploads/2017/04/alligator.mp4

Similarly when you select Play Video 2, the value of sys.argv[2] is

plugin://plugin.video.demo1/?mode=play&playlink=https://www.youtube.com/watch?v=J9d9UrK0Jsw

makes final_link as https://www.youtube.com/watch?v=J9d9UrK0Jsw

 play_video(final_link)

Calls the function play_video and final_url is passed as a parameter.

play_video function

Let us now take a look at the play_video function.

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)
# Pass the item to the Kodi player.
 xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)
def play_video(path):

defines a function name play_video, which takes path as input. We name path, so that when we remember that it takes as input the path aka url of the video.

"""
 Play a video by the provided path.
 :param path: str
 """

These are comments, and ignored when the code is executed.

play_item = xbmcgui.ListItem(path=path)

Here we just create a playable item, using the path we sent as input.

xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)

This is the code which plays the path which we sent as input to the function.

If you select Play Video 2, nothing will happen as this code cannot play youtube videos. We will be discussing how to play that in another post.

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