ticrandall
Posts: 19
Joined: Sat Oct 19, 2013 2:39 pm

Steve's Computer Vision Raspberry Pi Automatic Video Looper

Thu Oct 31, 2013 5:45 pm

http://stevenhickson.blogspot.com/2013/ ... ooper.html

Anyone know how to change how often it updates the play list? I am adding files and it doesn't seem to pick them up..

If I stop/restart the script then the videos play, I would like the list to update every time it plays through, or every other time..

Any thoughts?

Thanks

JustThisGuy
Posts: 114
Joined: Thu Jan 05, 2012 11:22 pm

Re: Steve's Computer Vision Raspberry Pi Automatic Video Loo

Thu Oct 31, 2013 7:55 pm

This code in startvideo.sh creates a list of files to play once when the script starts.

Code: Select all

FILES=/home/pi/videos/
current=0
for f in `ls $FILES | grep ".mp4$\|.avi$\|.mkv$\|.mp3$\|.mov$\|.mpg$\|.flv$\|.m4v$"`
do
        vids[$current]="$f"
        let current+=1
	echo "$f"
done
max=$current
The script runs the loop with this code

Code: Select all

while true; do
if pgrep omxplayer > /dev/null
then
	echo 'running'
else
	let current+=1
	if [ $current -ge $max ]
	then
		current=0
	fi

	/usr/bin/omxplayer -r -o hdmi "$FILES${vids[$current]}"
fi
done
This chunk of code never changes the vid[] list so any new files aren't even seen by this loop.

Let's say for now that we just want to check for and add the new files to the vid[] list at the top of the play loop so that when a file completes the list is updated.

The code could look like this

Code: Select all

#!/bin/bash

declare -A vids

#Make a newline a delimiter instead of a space
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

FILES=/home/pi/videos/

current=0

#Reset the IFS
IFS=$SAVEIFS

while true; do

tempindx=0
for f in `ls $FILES | grep ".mp4$\|.avi$\|.mkv$\|.mp3$\|.mov$\|.mpg$\|.flv$\|.m4v$"`
do
        vids[$tempindx]="$f"
        let tempindx+=1
	echo "$f"
done
max=$tempindx

if pgrep omxplayer > /dev/null
then
	echo 'running'
else
	let current+=1
	if [ $current -ge $max ]
	then
		current=0
	fi

	/usr/bin/omxplayer -r -o hdmi "$FILES${vids[$current]}"
fi
done
Please notice that the 'current' variable in the original script is reused, once at the top during the building of the list and again in the play loop. In the original script that was fine because the list builder runs only once and the 'current' variable was reset (let current=0) before entering the play loop. Since the list building loop is now running in the play loop the 'current' variable the two uses are incompatible. To fix that I changed the variable to 'tempindx' for the list build.

While this gives you what you asked for it may not behave like you believe. For one thing, while every vid in the directory will get put in the list and probably played it doesn't cover the situation where a file is deleted from the directory. And I haven't looked to see if growing and reducing the list causes any problem besides the skipping of a file once because it's index changing.

CAUTION: Unfortunately I'm not at home and can't test this code. And it doesn't cover the situation where a file is deleted from the directory.
Any conversation about a sufficiently complex subject is indistinguishable from babble.

ticrandall
Posts: 19
Joined: Sat Oct 19, 2013 2:39 pm

Re: Steve's Computer Vision Raspberry Pi Automatic Video Loo

Thu Oct 31, 2013 9:21 pm

WOW.. Just wow.. that goes above and beyond what I expected to get back. Thank you very much.. I will try this out and see what happens.. Why do you think that it might not operate in the manor I am looking for?

I had one other thought If I couldn't get this figured out, I am setting up an automatic video display that will have podcasts and some promotional videos intermixed with it. What If I set it up to download the podcasts at x time with a cron input, and then after that used another cron job to stop, then start the videoloop script. This really would only have to happen once per day.

JustThisGuy
Posts: 114
Joined: Thu Jan 05, 2012 11:22 pm

Re: Steve's Computer Vision Raspberry Pi Automatic Video Loo

Thu Oct 31, 2013 10:30 pm

You're welcome.

Well I wasn't sure if the order of the vids was important. You are using an index into the vid[] list which increments every time through the play loop. But each time through the play loop the vid[] list may change drastically. Say a.mp4 and b.mp4 are in the directory at startup. In the vid[] list they would have indexes 0 (a.mp4) & 1 (b.mp4) since that is the order that the 'ls' command will return them. Say then we have just completed showing a.mp4, we go to the top of the loop and find a new file 42.mp4. The vid[] list will end up

vid[0] => 42.mp4
vid[1] => a.mp4
vid[2] => b.mp4

and vid[1] will play which means a.mp4 will get played twice. The order that the script plays the vids in may change depending on what you put in and take out of the directory and when. Oh, and there are parameters you can pass to ls to affect the order.

As for the cron stuff, yeah that might work. But this whole thing is actually run as a service, which is a type of background process. Generally you want to write them such that you start them up at boot and let them run untill shutdown. If my mods work then there really isn't any reason to shut the videoloop service down and start it up again (this is called bouncing the server) every day. Let it run and just have the cron job download the new vids whenever you want and the service should just keep playing them in a loop.

But, I don't know about you but playing around and trying to do the same thing in different ways is a lot of fun. Try the two cron method. Or go over to the one of the OS groups and ask for help getting these scripts set as a service using the rc.d system (i never was really good at that).

Almost countless ways of doing what you want.
Any conversation about a sufficiently complex subject is indistinguishable from babble.

ticrandall
Posts: 19
Joined: Sat Oct 19, 2013 2:39 pm

Re: Steve's Computer Vision Raspberry Pi Automatic Video Loo

Mon Nov 04, 2013 9:47 pm

Definitely fun.. I agree it is pretty neat the number of ways you can accomplish one goal.. I've learned quite a bit getting this put together for my brother.. I have not had a chance to try out your code, have been moving the past few days... But am looking forward to digging into it later this week.. For right now I have the pi set up to just reboot after it downloads the videos, not ideal by any means, but until I can mess with it further.. it'll have to do!

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 6 guests