2019年4月23日 星期二

omxplayer loop bash on Raspbian + RPi3B

#!/usr/bin/env bash
#
# Plays videos from playlist
#
# Copyright  2013 Janne Enberg http://lietu.net/
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#

# setup loop times: -1/0: infinite; 1~999: repeat loop
# loop=3
loop=$1
echo "input loop number is: " $loop

# create a playlist in ./ folder
find "$PWD" -name "*.mp4" > playlist.m3u

# Where is the playlist
# PLAYLIST_FILE="${HOME}/.playlist"
PLAYLIST_FILE="playlist.m3u"

# make a copy of the original playlist
cp ${PLAYLIST_FILE} ${PLAYLIST_FILE}.tmp

# If you want to switch omxplayer to something else, or add parameters, use these
PLAYER="omxplayer"
PLAYER_OPTIONS=" -b "

# Process playlist contents
while [ true ]; do
        # Sleep a bit so it's possible to kill this
        sleep 1

        # Do nothing if the playlist doesn't exist
        if [ ! -f "${PLAYLIST_FILE}" ]; then
                echo "Playlist file ${PLAYLIST_FILE} not found"
                continue
        fi
       
        # Get the top of the playlist
        file=$(cat "${PLAYLIST_FILE}.tmp" | head -n1)

        # And strip it off the playlist file
        cat "${PLAYLIST_FILE}.tmp" | tail -n+2 > "${PLAYLIST_FILE}.rest"
        mv "${PLAYLIST_FILE}.rest" "${PLAYLIST_FILE}.tmp"

        # Skip if this is empty
        if [ -z "${file}" ]; then       
                if [ $loop -eq -1 ]; then
                        #cp ${PLAYLIST_FILE} ${PLAYLIST_FILE}.tmp
                        cp ${PLAYLIST_FILE} ${PLAYLIST_FILE}.tmp
                elif [ $loop -gt 1 ]; then               
                        cp ${PLAYLIST_FILE} ${PLAYLIST_FILE}.tmp
                        loop=$(($loop - 1))
                        echo "rest loop number is: " $loop
                else       
                        echo "Playlist empty or bumped into an empty entry for some reason"
                        continue
                fi
        fi

        # Check that the file exists
        if [ ! -f "${file}" ]; then
                echo "Playlist entry ${file} not found"
                continue
        fi

        echo
        echo "Playing ${file} ..."
        echo

        "${PLAYER}" ${PLAYER_OPTIONS} "${file}"

        echo
        echo "Playback complete, continuing to next item on playlist."
        echo    
done

沒有留言:

張貼留言