How to Autoplay Videos Upon Boot on Kodi

Here’s a quick tip for today.

If you’ve got a few spares Raspberry Pis lying around (like I do) then sometimes they can come in handy as a portable media player. However, given that I don’t know the capabilities of the television with which the Pi will be used (at the bare minimum it has HDMI) then I am left guessing whether or not things like using the remote control will work.

To save messing around, I decided to configure OSMC on the Pi to autoplay a single video located on a USB stick. This is how you do it:

  1. Locate or create /home/osmc/.kodi/userdata/autoexec.py
  2. Add the following lines to the file:

import xbmc

xbmc.executebuiltin( “PlayMedia(/media/[labelofusbstick]/[path]/[videofilename])” )

You’ll need to replace [labelofusbstick] with the volume label of the USB stick, [path] with the folder structure where the video is located, and [videofilename] with the filename of the video.

After you save the file, reboot your Raspberry Pi and when it boots up again it should launch straight into the video!

16 comments

Skip to comment form

  1. Ok I could google this…. But…. Can you get it to play a folder on boot and shuffle the contents? Think jukebox style.

  2. Yeah, you could do something like the last post @ http://forum.kodi.tv/showthread.php?tid=69797

    # Autoplay videodirectory
    import os, xbmc

    # set path to dir you want to play
    path=”/media/pathtovideodir”

    dirList=os.listdir(path)

    dirList.sort()

    videoList = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
    videoList.clear()

    for fname in dirList:
    videoList.add(path + “\” + fname)

    # shuffle playlist
    #videoList.unshuffle()

    # put playlist on repeat
    xbmc.executebuiltin(“xbmc.playercontrol(RepeatAll)”)

    # play playlist
    xbmc.Player().play(videoList)

  3. Google is so over rated 🙂

    • Nicolai Hansen on October 2, 2015 at 20:33
    • Reply

    Hi Boyd.

    Total noob here..

    Some questions for you.

    Are you running OSMC alone, not on Ubuntu or other desktops?

    How, where, and with what do you write Linux commands to OSMC?
    Taking the SD Card out og my Pi2, and putting it in my Win pc, i cant find the files that you write about.

    Regards from noob.

    1. Ah, fair question 🙂

      My Pi2 is running OSMC by itself.

      To update the file, I connected to the Pi2 using SSH using Putty which will give you the remote text mode interface to the unit. You’ll need the IP address or hostname of the Pi2 to connect as well as the username and passsword (by default these should be “osmc” without quotation marks).

      Once you are connected, type the following:

      nano /home/osmc/.kodi/userdata/autoexec.py

      This will open a text editor where you can make the required updates. Once completed, press Ctrl+S to save and then Ctrl+X to exit.

      Reboot the Pi2 and you should be off and running 🙂

        • Nicolai Hansen on October 3, 2015 at 01:51
        • Reply

        Thx for the quick reply.

        I need it to boot up and start playing videos from a folder.
        For a clothing store.

        Last question (mayby).
        How do i locate my usb drive path?

          • Nicolai Hansen on October 3, 2015 at 05:22
          • Reply

          Hi Again..

          Found the Linux command line df.

          But i get a error from autoexec.py when OSMC starts.
          Read log file for error

          Is it because its called KODI now, and not XBMC?
          So the lines in your link may have changed.

          Can you help.

          Thanks in advance.

            • Boydo on October 3, 2015 at 09:00
              Author

            No, particularly given that the path where the file lives already has “kodi” included as the folder name.

            The fact that you are getting an error though means that the file is probably being read.

            What text did you insert into autoexec.py? Maybe there is a minor error causing it to throw the error.

    • Nicolai Hansen on October 3, 2015 at 22:36
    • Reply

    Its most likely somthing i havent done correctly.

    I copied the text from your post, and changed path of video dir to TOSHIBA. (name of my usb stick)

    By the way, how do i find the autoexec.py log file?

    # Autoplay videodirectory
    import os, xbmc

    # set path to dir you want to play
    path=”/media/TOSHIBA”

    dirList=os.listdir(path)

    dirList.sort()

    videoList = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
    videoList.clear()

    for fname in dirList:
    videoList.add(path + “\” + fname)

    # shuffle playlist
    #videoList.unshuffle()

    # put playlist on repeat
    xbmc.executebuiltin(“xbmc.playercontrol(RepeatAll)”)

    # play playlist
    xbmc.Player().play(videoList)

    1. There should be a general log file for Kodi at /home/.kodi/temp/kodi.log – depending on the level of logging enabled you may see a varying amount of information in there.

      Have you tried manually browser to /media/TOSHIBA via Putty to see if it is a valid folder?

        • Nicolai Hansen on October 4, 2015 at 23:01
        • Reply

        cd /media/TOSHIBA and the dir, lists all of my videos.

        Heres my df.

        osmc@osmc:~$ df
        Filesystem 1K-blocks Used Available Use% Mounted on
        /dev/root 30299164 564872 28172132 2% /
        devtmpfs 371332 0 371332 0% /dev
        tmpfs 375636 0 375636 0% /dev/shm
        tmpfs 375636 5104 370532 2% /run
        tmpfs 5120 0 5120 0% /run/lock
        tmpfs 375636 0 375636 0% /sys/fs/cgroup
        /dev/mmcblk0p1 244988 17311 227677 8% /boot
        /dev/sda1 15220160 6762944 8457216 45% /media/TOSHIBA
        tmpfs 75128 0 75128 0% /run/user/1000
        osmc@osmc:~$

        Trying to find the elusive log file is beeing a problem.

        osmc@osmc:~$ cd /home/.kodi/temp/kodi.log
        -bash: cd: /home/.kodi/temp/kodi.log: No such file or directory
        osmc@osmc:~$
        osmc@osmc:~$ cd /home
        osmc@osmc:/home$ cd /.kodi
        -bash: cd: /.kodi: No such file or directory
        osmc@osmc:/home$
        osmc@osmc:/home$ dir
        osmc
        osmc@osmc:/home$

        am i even logged in correctly?

        login as: osmc
        OSMC: Open Source Media Center
        osmc@192.168.1.61‘s password:osmc

        The programs included with the Debian GNU/Linux system are free software;
        the exact distribution terms for each program are described in the
        individual files in /usr/share/doc/*/copyright.

        Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
        permitted by applicable law.
        Last login: Sun Oct 4 13:11:54 2015 from 192.168.1.32
        osmc@osmc:~$

        1. Ah, okay. My bad.

          Try /home/osmc/.kodi/temp/kodi.log

    • J_Sanche on June 23, 2016 at 10:55
    • Reply

    hey
    i am having exactly the same problem as Nicolai Hansen.

    osmc@osmc:~$ cd /home/osmc/.kodi/temp/kodi.log
    -bash: cd: /home/osmc/.kodi/temp/kodi.log: Not a directory

    I was wondering if there was ever a solution found for this?
    I used:

    import xbmc
    xbmc.executebuiltin( “PlayMedia(/media/Lexar/A.mp4)” )

    to reach a file on my Lexar usb stick which is called A.mp4 – perhaps there is a problem with the path i have written?

    Any help much appreciated!

    Thank you!

    • j_sanchez on June 23, 2016 at 11:00
    • Reply

    Hey
    I am getting exactly the same issue as Nicolai Hansen. When i tried /home/osmc/.kodi/temp/kodi.log
    i recieved this:

    osmc@osmc:~$ cd /home/osmc/.kodi/temp/kodi.log
    -bash: cd: /home/osmc/.kodi/temp/kodi.log: Not a directory

    Here is what i typed into autoexec.py

    import xbmc
    xbmc.executebuiltin( “PlayMedia(/media/Lexar/A.mp4)” )

    I am wondering whether the file path is definitely /media/Lexar/A.mp4 ???

    1. Not sure if the path has been changed in subsequent builds used for fresh installation but this solution still works for me.

      Are you certain that Lexar is the label assigned to the SD card? Can you browse to that location from the command line?

    • DWoot on August 23, 2017 at 04:51
    • Reply

    I know this is an old thread but since it is one of the more common results when googling an auto playing and repeating playlist on boot for Kodi I figured I would chime in on the fixes that were needed.

    The first is just simply verifying that you have the path set correctly for your project. I have done a number of projects for customer showrooms and have found, if the files are not too large, simply using a 32 or 64gb sd card and then mapping the /Movies directory through FTP on the clients computer allows them to add and change files at their leisure without even needing to access the Pi which may be stuck to back of the television high on a wall.

    The second most common problem is the lost syntax when people copy and paste the code for the autoexec.py script. The portion of code that adds the files from the folder to the playlist must be indented and contain a “/” rather than “\” as follows;

    for fname in dirList:
    videoList.add(path + “/” + fname)

    I hope this is helpful to some of you who have been banging your head against the wall trying to figure out what was going wrong as I did for awhile.

Leave a Reply

Your email address will not be published.