Enchanting screensaver for Kodi

image alt






Assigning a Screen Saver to Kodi



The project is designed to create an "Enchanting" screen saver with a minimal amount of source code in Python. The project is the simplest plugin for multimedia center Kodi.



The project shows how you can create a very beautiful screen saver based entirely on the work of the "OpenSource" community. The integration project is an example of writing two independent components, each of which takes about 80 lines of code. The first component is a content generator, shell script, the second component is a Kodi multimedia center plugin, which is responsible for displaying content.



And finally, if you are a programmer and use the Git version control system, you can visualize your work, write it to a video file and enjoy the result on a TV or computer screen, leaning back in your chair with a cup of coffee. And on cool autumn evenings, you can sleep soothingly under your screensaver, without forgetting to set the device off timer in Kodi.



Preamble



You can endlessly look at three things:

how fire burns, how water flows and how other people work.



Since the plugin is written for the β€œKodi Multimedia Center”, then I will visualize the work of the super team β€œKodi Programmers”.



Using plugins in Kodi



The Kodi Multimedia Center is a very powerful and flexible program, working in conjunction with the ffmpeg external library, for decoding audio and video files.



To create a third-party application, Kodi uses the β€œaddons” extension mechanism or simply plug-ins. To create my own plugin, I need some skills and a little knowledge of the Python programming language.



The Kodi plugin mechanism is extremely flexible and convenient. Python is an interpreted language, which means that I don’t need to compile anything, compile it into a separate software package, suffer with build files like β€œmakefile”, etc.



For the final distribution of the Kodi plugin, just pack it in the Zip archive, observing some rules on the directory structure. Having the final Zip archive on hand, it can be installed on any device that Kodi runs on: a computer, tablet, and finally a TV (meaning a bunch of TV + single-plate), specifying the archive as a plug-in source.



Kodi plugin structure



 └── screensaver.kodi.universe
     β”œβ”€β”€ README.md
     β”œβ”€β”€ addon.xml
     β”œβ”€β”€ changelog.txt
     β”œβ”€β”€ create.sh
     β”œβ”€β”€ fanart.jpg
     β”œβ”€β”€ icon.png
     β”œβ”€β”€ resources
     β”‚ β”œβ”€β”€ language
     β”‚ β”‚ β”œβ”€β”€ English
     β”‚ β”‚ β”‚ └── strings.po
     β”‚ β”‚ └── Russian
     β”‚ β”‚ └── strings.po
     β”‚ β”œβ”€β”€ settings.xml
     β”‚ └── skins
     β”‚ └── default
     β”‚ β”œβ”€β”€ 720p
     β”‚ β”‚ └── kodi-universe.xml
     β”‚ β”œβ”€β”€ 1080i
     β”‚ β”‚ └── kodi-universe.xml
     β”‚ └── media
     β”‚ β”œβ”€β”€ black.jpg
     β”‚ β”œβ”€β”€ buran.jpg
     β”‚ └── kodi-universe.mkv
     └── screensaver.py






Description of plugin resources



The resources directory contains the following files:





contents of Russian / strings.po
# Kodi Media Center language file # Addon Name: Screensaver Kodi Universe # Addon id: screensaver.kodi.universe # Addon Provider: berserktv msgid "" msgstr "" "Project-Id-Version: Kodi Addons\n" "Report-Msgid-Bugs-To: alanwww1@kodi.org\n" "POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Kodi Translation Team\n" "Language-Team: English \ (http://www.transifex.com/projects/p/xbmc-addons/language/en/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: en\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" msgctxt "#32001" msgid "Screensaver" msgstr " " msgctxt "#32002" msgid "Video file" msgstr "" msgctxt "#32003" msgid "Not Video" msgstr "  "
      
      









contents of settings.xml
  <?xml version="1.0" encoding="utf-8" standalone="yes"?> <settings height="800"> <category label="32001"> <setting id="videofile" label="32002" type="video"/> <setting id="not-video" type="bool" \ label="32003" default="false"/> </category> </settings>
      
      







Media Resources Plugin:





Plugin root configuration file - addon.xml



Addon.xml - is the main configuration file for the plugin, from which Kodi takes all the necessary information to launch the plugin and integrate it into the multimedia center.



addon.xml content
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <addon id="screensaver.kodi.universe" name="Kodi Universe" \ version="0.1.2" provider-name="berserktv"> <requires> <import addon="xbmc.python" version="2.7"/> </requires> <extension point="xbmc.ui.screensaver" library="screensaver.py" /> <extension point="xbmc.addon.metadata"> <platform>all</platform> <source>https://github.com/berserktv/screensaver.kodi.universe </source> <summary lang="en">Kodi Universe</summary> <summary lang="ru"> </summary> <description lang="en">Screensaver - Kodi Universe </description> <description lang="ru">  -  </description> <license>GNU GENERAL PUBLIC LICENSE. Version 2, June 1991</license> </extension> </addon>
      
      







The main parameters of the plugin are:





Plugin implementation



Since the plugin should be extremely simple and straightforward, in its implementation I will limit myself to one main source file screensaver.py located in the root directory of screensaver (a)



In order for the python script to be called, I registered it in the addon.xml file, see the section above



The Python programming language is quite flexible, it allows you to perform the same action in several ways, and for clarity, I will use the object-oriented approach with classes.



screensaver.py content
  # -*- coding: utf-8 -*- # Plugin for Kodi mediacenter # Kodi Universe - Very Simple Screensaver # GNU GENERAL PUBLIC LICENSE. Version 2, June 1991 import os import sys import xbmc import urllib import xbmcgui import xbmcaddon __id__ = 'screensaver.kodi.universe' __addon__ = xbmcaddon.Addon(id=__id__) __path__ = __addon__.getAddonInfo('path') def_video_url = __path__+'/resources/skins/default/media/kodi-universe.mkv' class BsPlaylist: def __init__(self,): pass def getPlaylist(self,): try: xbmc.PlayList(1).clear() except: pass self.playlist = xbmc.PlayList(1) item = xbmcgui.ListItem("item1") self.playlist.add(__addon__.getSetting("videofile"),item) return self.playlist class BsPlayer(xbmc.Player): def __init__(self,): pass def onPlayBackStarted(self): xbmc.executebuiltin("PlayerControl(RepeatAll)") def onPlayBackStopped(self): return class Screensaver(xbmcgui.WindowXMLDialog): def __init__( self, *args, **kwargs ): pass def onInit(self): video_url = __addon__.getSetting("videofile") if (video_url == ""): video_url = def_video_url __addon__.setSetting("videofile", video_url) if (__addon__.getSetting("not-video") == "true" \ or not os.path.isfile(video_url) ): return li = BsPlaylist() self.vpl = li.getPlaylist() if self.vpl: xbmc.sleep(2000) self.getControl(1).setImage("black.jpg") self.player = BsPlayer() if not xbmc.getCondVisibility("Player.HasMedia"): self.player.play(self.vpl,windowed=True) def onAction(self,action): try: xbmc.PlayList(1).clear() except: pass try: xbmc.Player().stop() except: pass try: self.close() except: pass if __name__ == '__main__': scr = Screensaver( 'kodi-universe.xml', __path__, 'default', '', ) scr.doModal() del scr
      
      







class Screensaver (xbmcgui.WindowXMLDialog)



The main class of the screen saver



The Python Kodi API includes several core modules: xbmc, xbmcgui, xbmcplugin, xbmcaddon, and xbmcvfs . To work with the Kodi GUI, I will use the xbmcgui module. This module contains classes responsible for different interface elements.



To create a plugin dialog box with its interface described in the xml configuration file, the xbmcgui class is used.WindowXMLDialog



kodi-universe.xml content
  <?xml version="1.0" encoding="utf-8" standalone="yes"?> <window type="window"> <controls> <control type="image" id="1"> <description>Background image</description> <posx>0</posx> <posy>0</posy> <colordiffuse>FF555555</colordiffuse> <aspectratio scalediffuse="false" align="center" \ aligny="center">scale</aspectratio> <width>1920</width> <height>1080</height> <texture>buran.jpg</texture> </control> <control type="videowindow" id="2"> <description>VideoWindow</description> <posx>0</posx> <posy>0</posy> <width>1920</width> <height>1080</height> <visible>true</visible> </control> </controls> </window>
      
      







The configuration XML file describes two control elements:



The first one with identifier - 1 , is a background image - β€œSnowstorm, on the wings of a Dream”, of certain sizes, aligned to the center of the screen.



The second element with identifier - 2 , is a window for playing the Video, with a sign of visibility and certain geometric dimensions.



The Screensaver class inherits from the WindowXMLDialog class, and has three methods:





The Screensaver class uses two helper classes:





The general algorithm of Screensaver (a) is as follows:



  1. When the Screensaver call event occurs (a) - the user’s inactivity for the specified number of minutes and the absence of active video / audio playback, Kodi transfers control to the screensaver.py script
  2. Based on the xml configuration file, the main graphic window of the plugin is created. When the graphical window is initialized, the string resources of the plugin settings are loaded (the menu is β€œsettings”).


If the video file is not specified

__addon __. getSetting ("videofile")



it is populated with a default parameter

video_url = def_video_url

__addon __. setSetting ("videofile", video_url)



if there is no flag - β€œdisable video playback”

__addon __. getSetting ("not-video")



the futuristic image β€œSnowstorm on the wings of the Dream” is shown for two seconds

xbmc.sleep (2000)



further by the identifier of control 1, a black background image is set

self.getControl (1) .setImage ("black.jpg")



and then the XBMC video player starts with a single-file playlist

self.player.play (self.vpl, windowed = True)



The video file is played in a circle until the moment it comes

waking event i.e. any active user action



Creating visualizations for the Kodi plugin in Ubuntu



Note:

All the instructions described below I will run under the Linux operating system, namely the Ubuntu distribution



It is also possible to perform the following steps in a Debian compatible operating system - the main condition for launching will be the presence of the Apt package manager in the system, i.e. a manager that allows you to install Deb software in the system. Of course, you can perform the following steps on any Linux system, but this will require additional steps from you and possibly changing the sequence of some commands (as an example: installing RPM packages instead of Deb, etc.)



Gource is a very interesting and fascinating project. Gource is named after source i.e. source code + G (Graphics). This application allows you to visualize the history of changes in the version control system. Gource natively understands Git, for other systems such as SVN, Mercurial there are converters that allow you to convert the storage base into Git Format.



Gource - renders incredibly beautiful with OpenGL, and has a large number of parameters for its work. This incredibly powerful tool for generating "Enchanting" visualization I will use.



To create a visualization, I need some sequence of commands described in a bash script



To generate the video, I need two main programs:

Gource - to create the source video file for the Git history of any specified project

FFmpeg - a library for encoding and decoding video and audio



Script to generate a plugin video file



create.sh contents
  #!/bin/bash # This is script of the generation video from "Gource". # # project: Screensaver Kodi Universe (https://berserk.tv) # This script creates a ZIP archive of a Kodi screensaver. # GNU GENERAL PUBLIC LICENSE. Version 2, June 1991 # #     , #        # git zip ffmpeg gource    sudo OUT_DIR="output" OUT="kodi-universe.mkv" NAME_PROJ="screensaver.kodi.universe" MEDIA_PATH="${NAME_PROJ}/resources/skins/default/media" NAME_REP="https://github.com/berserktv/${NAME_PROJ}.git" GSFILE="output.ppm" SECONDS_PER_DAY="1" GOURCE_FRAME_RATE="30" RESOLUTION="-1920x1080" CODEC_OUT_FRAME_RATE="25" # -vcodec -     , # libx264         (h.264) # -profile -    (baseline, main, high, # high10, high422, high444) # -pix_fmt -    (yuv420p, yuv422p, yuv444p) FFPARAM="-vcodec libx264 -profile:v high422 -pix_fmt yuv420p" GSPARAM1="--camera-mode track ${RESOLUTION} --stop-position 1.0 \ --seconds-per-day ${SECONDS_PER_DAY}" GSPARAM2="--git-branch origin/master --multi-sampling \ --stop-at-end --hide-filenames" GSPARAM3="--highlight-users --file-idle-time 13 --max-files 0 --hide date" GSPARAM4="--title Kodi --bloom-multiplier 1.0 --bloom-intensity 1.0" VIS="visualize" # GIT         GIT_REP="https://github.com/xbmc/xbmc.git" # arg1 -   git , #         # example: ./create.sh "https://github.com/facebook/react.git" if [ -n "$1" ]; then GIT_REP="$1"; fi #   git zip ffmpeg  gource packages="git zip ffmpeg gource" for i in $packages; do if ! dpkg -s $i | grep -q "install ok installed"; then sudo apt-get install -y $i; fi done #    test -d ${OUT_DIR} && rm -rf ${OUT_DIR} test -d ${OUT_DIR} || mkdir -p ${OUT_DIR} cd ${OUT_DIR} #  Screensaver  GIT ,   if ! git clone ${NAME_REP} ${NAME_PROJ}; then echo "Error, not load ${NAME_REP}, exit ..."; exit 1; fi if ! git clone ${GIT_REP} ${VIS}; then echo "Error, not load ${GIT_REP}, exit ..."; exit 2; fi #    Screensaver(a) gource ${VIS} ${GSPARAM1} ${GSPARAM2} ${GSPARAM3} ${GSPARAM4} \ --output-framerate ${GOURCE_FRAME_RATE} --output-ppm-stream ${GSFILE} ffmpeg -y -r ${GOURCE_FRAME_RATE} -f image2pipe -vcodec ppm \ -i ${GSFILE} ${FFPARAM} -r ${CODEC_OUT_FRAME_RATE} ${OUT} && sync mv -f ${OUT} ${MEDIA_PATH} rm -f ${GSFILE} #     #   GIT    screensaver() test -d ${NAME_PROJ}/.git && rm -fr ${NAME_PROJ}/.git zip -r ${NAME_PROJ}.zip ${NAME_PROJ}
      
      







The script should be run as a normal user, but during startup the script requires the installation of the following git zip ffmpeg gource programs



If they are absent, the script will try to install them using the privilege escalation command - sudo .



In short, the script does the following:





Gource Options





note: some parameters may be mutually exclusive,

A complete list of parameters can be found here .



Short video tutorial for Screensaver (a)





1) Download the project from github:
  git clone https://github.com/berserktv/screensaver.kodi.universe.git
      
      







2) Generate a video and create a Zip archive of the plugin:
  cd screensaver.kodi.universe chmod u+x create.sh ./create.sh
      
      







3) Video generation time and free disk space usage:
       .           -    20      Gource (PPM)   MKV      FFmpeg. (  h.264)  ,   Git  XBMC (Kodi)   700            .       PPM   10     ( FullHD, 30   ).
      
      







4) Install screensaver in Kodi
     ""   Kodi (        )   Kodi - "" => " " => " Zip "   ,     Kodi Universe    Screensaver(),     .  Settings ( ) => " " => ""
      
      








All Articles