Auto-play video files from USB drive upon insertion

  • I'm using LibreElec on Raspberry Pi Zero (RP0). I have a hub attached to the RP0. I want to know how to automatically play video files from a USB drive upon insertion. I saw sample of an autoexec file that may potentially auto-run a playlist and contents residing in the micro-SD card upon booting. But, I need something that is more versatile and dynamic. I need a way to automatically read the mp4, mp3, avi, mkv contents from the USB drive upon insertion to the hub and start playing those files.


  • Apart from DVDs, Kodi has no autoplay support for music or videos from inserted USB sticks, external HDDs and the like.

    Thanks for your response.

    1. If KODI can dynamically auto-play DVDs when inserted, there must be a way to customize that set of codes and make it work for USB drives.

    2. Is there a way to use wild-card characters such as "*" to play all videos in a given folder on the SD-card? As an example, is the following possible:
    "PlayMedia(/home/pi/.xbmc/userdata/playlists/*.MP4)"
    or
    "PlayMedia(/home/pi/.xbmc/userdata/playlists/*.*)"

  • The correct approach will be to create a udev rule that triggers a bash or python script when removable media is mounted. The script needs to scan the device for playable files and create a Kodi playlist in .xsp format. You can then run a kodi-send "PlayMedia" command to instruct Kodi to play the playlist found at /path/to/playlist.xsp, and then when finished you can use kodi-send "ClearPlaylist" to remove it from Kodi.

  • I actually almost have a way to do this using udev, but it doesn't work because udev won't finish until the all udev rules have run, so it's not possible (with the current tools) to do this (that I have found yet).

    /storage/.config/udev.rules.d/99-usb-play.rules

    Code
    IMPORT{builtin}="blkid"
    ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*|mmc*", ENV{ID_FS_USAGE}=="filesystem", \
      RUN+="/storage/autoplay.sh $attr{ID_FS_LABEL}"

    /storage/autoplay.sh

    Bash
    #!/bin/sh
    
    
    sleep 5
    /usr/bin/kodi-send --action="PlayMedia(\"/media/${1}\", isdir)"

    I will try to get this to work using bash.

  • Thanks for your replies. It seems like there are no ready-made solutions for dynamically reading media content and auto-playing after USB drive insertion.

    I am trying to run Kodi/Libreelec on a RP0 using minimal extra hardware. I know I must have the SD-card which by default has 2 Linux ext3/4 partitions and cannot be easily read by Windows. So copying media files directly to that SD-card using Ubuntu etc. and creating a playlist that is auto-executed at startup is an alternate option. However, this option does not work for USB drives when inserted after booting.

    I can add a cheap micro USB OTG adapter on top of a FAT32 formatted USB drive and directly connect it to the micro USB slot on the RP0. Without a mouse, keyboard or Kore remote, I must rely on the Kodi/Libreelec system to automatically play all media upon insertion of the USB drive.

    Is there an easy way to auto-play media files from a FAT32 formatted USB drive while not having any mouse, keyboard or Kore remote to navigate, select and click on media files?


  • I actually almost have a way to do this using udev, but it doesn't work because udev won't finish until the all udev rules have run, so it's not possible (with the current tools) to do this (that I have found yet).

    It can be done with some modifications:

    /storage/.config/udev.rules.d/99-usb-play.rules

    Code
    IMPORT{builtin}="blkid"
    ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*|mmc*", ENV{ID_FS_USAGE}=="filesystem", \
    RUN+="/usr/bin/systemd-run /storage/autoplay.sh /dev/%k"

    /storage/autoplay.sh

    Bash
    #!/bin/sh
    sleep 5
    
    MP=$(printf '%b' $(grep "^${1} " /proc/self/mounts | cut -d ' ' -f 2))
    
    if [ -f "${MP}/kodi.playme" ]
    then
    /usr/bin/kodi-send --action="PlayMedia(\"${MP}\", isdir)"
    fi

    This limits auto play to USB drives with existing /kodi.playme file.

    Edited once, last by mglae: Support spaces in mount point (December 9, 2018 at 9:51 AM).

  • Below are the results:
    [hr]

    It can be done with some modifications:

    /storage/.config/udev.rules.d/99-usb-play.rules

    Code
    IMPORT{builtin}="blkid"
    ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*|mmc*", ENV{ID_FS_USAGE}=="filesystem", \
    RUN+="/usr/bin/systemd-run /storage/autoplay.sh /dev/%k"

    /storage/autoplay.sh

    I created the 99-usb-play.rules and autoplay.sh files as they did not exist before. I added the codes which you shared - exactly - for both files. Rebooted the system and inserted a USB drive. It did not auto-play the USB contents. The sub-window shows up as usual with "Browse videos" and "Browse music" etc. as options, but no auto-play of contents within the USB drive.
    Is there anything else that I need to do to make this code work?

    Edited once, last by kmihmisc (November 3, 2016 at 10:59 PM).

  • In addition we did forget to advice to make autoplay.sh executable:

    chmod +x /storage/autoplay.sh

    If it still fails please post the URL from

    journalctl | pastebinit

    after a failed try.

  • I created the files and copied the code and changed the permissions exactly as you wrote (copy-paste). Now, Kodi Libreelec goes into an infinite loop of booting and going all the way up to showing the Jarvis wallpaper and then crashing and automatically rebooting.
    Then I changed the IF - FI loop so it does not look for the "kodi.playme" file in the USB drive.
    It did not change anything. Still crashing and rebooting.
    Any suggestions?