[19. April 2013] A-400 Advanced Network Setup now available | [11 Jan. 2013] Customize your A-400 Home Screen

Firmware: A-400 [13 May 2013] | C-300 [30 Nov. 2012] | A-300 [30 Nov. 2012] | C-200 [21 Jan. 2013] | A-200/A-210 [10 Aug. 2012]

Just got your NMT | WIKI has the answers | Search the forum | Forum Rules/Policy | Firmware & Official NMT News | Popcornhour manuals



User(s) browsing this thread: 1 Guest(s)
Thread Closed 
Script for Random playlist
12-08-2008, 09:30 AM (This post was last modified: 12-15-2008 02:39 AM by grahams.)
Post: #1
Script for Random playlist
Hi I created the following script to generate Random jsp playlist. It works when a ssh into my pch and run "# sh random.cgi". However I no cgi expert and I can not get it run when I call it from an html file. Could someone let me know what I'm missing, thanks in advance.

Most of the script is borrowed from the "Sleek" theme and it needs some of the commands that sleek installs. I'm writing this as a simple shuffle tool.

html file
Code:
<a href="http://localhost.drives:8883/HARD_DISK/random.cgi">Shuffle Playlist</a><br>

The script
Code:
#!/bin/sh
#

find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -name "*.mp3" | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/rl | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#' > my_random.jsp

cat my_random.jsp | grep -v "/<br>/" >my_random.jsp.tmp

cat my_random.jsp.tmp | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/tr '\n' '|' > /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp

If I can get the above to run from the pch screen I plan to add the following to start playback.

Code:
<center>
<H1>
<a href="http://localhost.drives:8883/HARD_DISK/Music/random.jsp" aod="playlist">Play</a>
<H1>
<center>
Find all posts by this user
12-08-2008, 01:43 PM
Post: #2
RE: Script for Random playlist - need CGI help
Just to double check...did you give the cgi file 777 file attributes?
Find all posts by this user
12-09-2008, 03:10 AM
Post: #3
RE: Script for Random playlist - need CGI help
Thansk for the quick reply.

Yeah, tried setting chmod 777 via a direct ssh connection. I did the same with a cgi for the sleek theme and that still worked, so I don't think it's a permissions thing.

Should what I did work? i.e. write a shell script and save it as an cgi.
Find all posts by this user
12-09-2008, 03:58 AM
Post: #4
RE: Script for Random playlist - need CGI help
Does it run using

./random.cgi

instead of

sh random.cgi

Oversight: Jukebox | FeedTime: Automatic nzbs
Find all posts by this user
12-09-2008, 05:57 AM
Post: #5
RE: Script for Random playlist - need CGI help
For CGI scripts, you have to follow a certain format. First, output all HTML headers, one per line. Then, output a blank line. Then output your content. Most of the headers are taken care of by the web server, but one header -- the Content-type header -- MUST be written by your script. So, for example, my a simple script to play one file might look like:

#! /bin/sh

echo "Content-type: text/plain";
echo "";

file="`echo $1 | sed s/%20/\ /g`"
file="`echo $file | sed 's|file:/opt|file:///opt|'`"
echo "Playing $file..."

killall -HUP mono
mono -single "$file" 2>&1 -dram 1

exit 0;
Find all posts by this user
12-09-2008, 07:36 AM (This post was last modified: 12-09-2008 07:47 AM by grahams.)
Post: #6
RE: Script for Random playlist - need CGI help
(12-09-2008 03:58 AM)ydrol Wrote:  Does it run using

./random.cgi

instead of

sh random.cgi

yes ./random.cgi also works when I'm ssh'd in to the pch

just found the script actually works via the remote on the pch, the "can not process" error is a false negetive.
(12-09-2008 05:57 AM)curien Wrote:  For CGI scripts, you have to follow a certain format. First, output all HTML headers, one per line. Then, output a blank line. Then output your content. Most of the headers are taken care of by the web server, but one header -- the Content-type header -- MUST be written by your script. So, for example, my a simple script to play one file might look like:

#! /bin/sh

echo "Content-type: text/plain";
echo "";

file="`echo $1 | sed s/%20/\ /g`"
file="`echo $file | sed 's|file:/opt|file:///opt|'`"
echo "Playing $file..."

killall -HUP mono
mono -single "$file" 2>&1 -dram 1

exit 0;

Thanks. I found that my cgi script actually does create the a random playlist when invoked via the pch browser. However the pch reports that it couldn't be processed, when it clearly was.

I cleaned up the script (?), but it still falsely reports it couldn't be processed.

Code:
#!/bin/sh
#

echo "Content-type: text/plain";
echo "";

find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -name "*.mp3" | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/rl | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#' > my_random.jsp

cat my_random.jsp | grep -v "/<br>/" >my_random.jsp.tmp

cat my_random.jsp.tmp | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/tr '\n' '|' > /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp

exit 0;

Is there a trick to make this clean?
Find all posts by this user
12-10-2008, 03:20 AM (This post was last modified: 12-10-2008 03:22 AM by curien.)
Post: #7
RE: Script for Random playlist - need CGI help
OK, I got it working. I took out the randomization part because I don't have rl compiled for my NMT, but it should be easy enough to stick in. A few things to note:

1. You were storing temporary files where you didn't need them. Just keep piping!
2. Sed's really cool. You can give it multiple commands in one go. If you notice, I'm doing two replacements with one invokation of the sed command. Way better than a sed + tr (plus, your tr was throwing away the newline characters, which makes a mess of the playlist file).
3. You have to craft the link to the playlist specially -- the NMT browser uses non-standard HTML attributes to tell it that the file is a playlist.

OK, so you need to files. One static HTML file to link to the playlist generator, and one to generate the playlist. There are NO temporary file!

random.html is simple enough:
&lt;a href="http://localhost.drives:8883/HARD_DISK/bin/random.cgi" pod="2"&gt;random.cgi&lt;/a&gt;

OK, now for random.cgi:
#! /bin/sh

echo Content-type: text/plain
echo ""

find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -name "*.mp3" | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#; s/$/|/' | rl
exit 0;

That's it! Happy randoming.
Find all posts by this user
12-10-2008, 08:37 AM
Post: #8
RE: Script for Random playlist - need CGI help
(12-10-2008 03:20 AM)curien Wrote:  OK, I got it working. I took out the randomization part because I don't have rl compiled for my NMT, but it should be easy enough to stick in. A few things to note:

1. You were storing temporary files where you didn't need them. Just keep piping!
2. Sed's really cool. You can give it multiple commands in one go. If you notice, I'm doing two replacements with one invokation of the sed command. Way better than a sed + tr (plus, your tr was throwing away the newline characters, which makes a mess of the playlist file).
3. You have to craft the link to the playlist specially -- the NMT browser uses non-standard HTML attributes to tell it that the file is a playlist.

OK, so you need to files. One static HTML file to link to the playlist generator, and one to generate the playlist. There are NO temporary file!

random.html is simple enough:
&lt;a href="http://localhost.drives:8883/HARD_DISK/bin/random.cgi" pod="2"&gt;random.cgi&lt;/a&gt;

OK, now for random.cgi:
#! /bin/sh

echo Content-type: text/plain
echo ""

find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -name "*.mp3" | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#; s/$/|/' | rl
exit 0;

That's it! Happy randoming.

Thanks Curien

Tried your script. It starts up the player, it buffers then fails to load.

Good tip on the sed options. Thought I need to delete newlines for jsp playlists, but I don't. Now no longer using the tmp files. My script runs, but still has the error.

Combined the randiomize and play into 1 html and added other audio files to the shuffling.

Code:
<center>
<H1>
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi">Shuffle</a><br>
<H1>
<center>
<a href="http://localhost.drives:8883/HARD_DISK/Music/random.jsp" aod="playlist">Play</a>
<H1>
<center>

Code:
#!/bin/sh
#

echo "Content-type: text/plain";
echo "";

/opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f \( -iname "*.mp3" -o -iname "*.wma" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.mpc" \) -print | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/rl | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#; s/$/|/' > /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp

#mono -audio -bgimg -playlist /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp

exit 0;

Note the commented out line for mono. This worked well, shuffled the songs and then started playing and showed the embedded album covers. However when I stop playing I get a blank screen and only power cycling cures it. Is there a way to gracefully return to the last page or homepage?
Find all posts by this user
12-10-2008, 01:15 PM
Post: #9
RE: Script for Random playlist - need CGI help
(12-10-2008 08:37 AM)grahams Wrote:  Tried your script. It starts up the player, it buffers then fails to load.

The syntax for your link is wrong. aod doesn't mean anything. You use vod="playlist" ONLY for video; for audio, use pod="2". See <a href="http://www.networkedmediatank.com/download/docs/Syabas_browser_tag_2007_v1.2.html">here</a> for details.

I guess you can't have audio and video files in the same playlist.

(12-10-2008 08:37 AM)grahams Wrote:  My script runs, but still has the error.

The NMT's browser shows that "page failed to load" (or whatever it is) error if the script doesn't return any content. In theory, the cgi program is supposed to actually print an HTML (or other type of) page.

That's why I had my script just print the playlist instead of saving it to a file. If you do that, and link to the cgi correctly for an audio playlist, you just need to run one link (the playlist) to generate it and start playing.

(12-10-2008 08:37 AM)grahams Wrote:  
Code:
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi">Shuffle</a><br>

Change that to:
Code:
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi" pod="2">Shuffle</a>

You won't need the other link (to the jsp file) at all anymore.

(12-10-2008 08:37 AM)grahams Wrote:  
Code:
/opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f \( -iname "*.mp3" -o -iname "*.wma" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.mpc" \) -print | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/rl | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#; s/$/|/' > /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp

Take out the redirection (ie, remove the "> /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp" part). By printing the results to stdout, you're actually sending it straight to the NMT's browser (as if you saved to a file and then clicked on a link to that file).

(12-10-2008 08:37 AM)grahams Wrote:  Note the commented out line for mono. This worked well, shuffled the songs and then started playing and showed the embedded album covers. However when I stop playing I get a blank screen and only power cycling cures it. Is there a way to gracefully return to the last page or homepage?

I'm currently trying to figure that out myself.
Find all posts by this user
12-10-2008, 10:39 PM
Post: #10
RE: Script for Random playlist - need CGI help
Do you think that this script could be used to play music videos as well?
I have a single folder with almost 2,000 music videos (mkv, mov, avi, mpg, etc.) that this would be great for.

Cheers
Find all posts by this user
12-10-2008, 11:24 PM
Post: #11
RE: Script for Random playlist - need CGI help
Yes, you'd need to change the filter for the find command to include those file types instead of the music types. Also, grahams is using a program called rl -- which is a standard Unix command but isn't on the NMT by default -- to randomize the playlist. Without it, you get the files in the same order every time. I'm not sure where he got it, or he might have cross-compiled it himself.

Finally, you have to link to the script like this:
Code:
<a href="/HARD_DISK/script.cgi" vod="playlist">Play movies</a>
Find all posts by this user
12-11-2008, 03:58 AM (This post was last modified: 12-11-2008 04:05 AM by grahams.)
Post: #12
RE: Script for Random playlist - need CGI help
(12-10-2008 11:24 PM)curien Wrote:  Yes, you'd need to change the filter for the find command to include those file types instead of the music types. Also, grahams is using a program called rl -- which is a standard Unix command but isn't on the NMT by default -- to randomize the playlist. Without it, you get the files in the same order every time. I'm not sure where he got it, or he might have cross-compiled it himself.

Finally, you have to link to the script like this:
Code:
<a href="/HARD_DISK/script.cgi" vod="playlist">Play movies</a>

I haven't tried building a random video playlist, but editing mp3 to avi etc. should work.

I found the rl command from the sleek theme. Also the find command is very basic by default but the sleek theme provides a full version which my script needs for -iname etc.

If I can get this to work well I'll try building an install script and include these commands.

BTW. the Sleek theme provides a way to randomize music and video I believe, but the theme itself only works well on 16:9 HD screens.
(12-10-2008 01:15 PM)curien Wrote:  The syntax for your link is wrong. aod doesn't mean anything. You use vod="playlist" ONLY for video; for audio, use pod="2". See <a href="http://www.networkedmediatank.com/download/docs/Syabas_browser_tag_2007_v1.2.html">here</a> for details.

I guess you can't have audio and video files in the same playlist.

(12-10-2008 08:37 AM)grahams Wrote:  My script runs, but still has the error.

The NMT's browser shows that "page failed to load" (or whatever it is) error if the script doesn't return any content. In theory, the cgi program is supposed to actually print an HTML (or other type of) page.

That's why I had my script just print the playlist instead of saving it to a file. If you do that, and link to the cgi correctly for an audio playlist, you just need to run one link (the playlist) to generate it and start playing.

(12-10-2008 08:37 AM)grahams Wrote:  
Code:
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi">Shuffle</a><br>

Change that to:
Code:
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi" pod="2">Shuffle</a>

You won't need the other link (to the jsp file) at all anymore.


Take out the redirection (ie, remove the "> /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp" part). By printing the results to stdout, you're actually sending it straight to the NMT's browser (as if you saved to a file and then clicked on a link to that file).

(12-10-2008 08:37 AM)grahams Wrote:  Note the commented out line for mono. This worked well, shuffled the songs and then started playing and showed the embedded album covers. However when I stop playing I get a blank screen and only power cycling cures it. Is there a way to gracefully return to the last page or homepage?

I'm currently trying to figure that out myself.

Thanks again. I cut and pasted your code directly which uses pod="2"

I'm guessing to cure the false error, I need to just get the cgi script to return something to the browser.
Find all posts by this user
12-15-2008, 02:21 AM
Post: #13
RE: Script for Random playlist - need CGI help
(12-11-2008 03:58 AM)grahams Wrote:  I haven't tried building a random video playlist, but editing mp3 to avi etc. should work.

I found the rl command from the sleek theme. Also the find command is very basic by default but the sleek theme provides a full version which my script needs for -iname etc.

If I can get this to work well I'll try building an install script and include these commands.

BTW. the Sleek theme provides a way to randomize music and video I believe, but the theme itself only works well on 16:9 HD screens.
(12-10-2008 01:15 PM)curien Wrote:  The syntax for your link is wrong. aod doesn't mean anything. You use vod="playlist" ONLY for video; for audio, use pod="2". See <a href="http://www.networkedmediatank.com/download/docs/Syabas_browser_tag_2007_v1.2.html">here</a> for details.

I guess you can't have audio and video files in the same playlist.

(12-10-2008 08:37 AM)grahams Wrote:  My script runs, but still has the error.

The NMT's browser shows that "page failed to load" (or whatever it is) error if the script doesn't return any content. In theory, the cgi program is supposed to actually print an HTML (or other type of) page.

That's why I had my script just print the playlist instead of saving it to a file. If you do that, and link to the cgi correctly for an audio playlist, you just need to run one link (the playlist) to generate it and start playing.

(12-10-2008 08:37 AM)grahams Wrote:  
Code:
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi">Shuffle</a><br>

Change that to:
Code:
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi" pod="2">Shuffle</a>

You won't need the other link (to the jsp file) at all anymore.


Take out the redirection (ie, remove the "> /opt/sybhttpd/localhost.drives/HARD_DISK/Music/random.jsp" part). By printing the results to stdout, you're actually sending it straight to the NMT's browser (as if you saved to a file and then clicked on a link to that file).


I'm currently trying to figure that out myself.

Thanks again. I cut and pasted your code directly which uses pod="2"

I'm guessing to cure the false error, I need to just get the cgi script to return something to the browser.

Success Smile

A little bit of trail and error, luck and lots of help from Curien.

The following works well

shuffle.hmtl
Code:
<center>
<H1>
<a href="http://localhost.drives:8883/HARD_DISK/shuffle.cgi" pod="2"playlist" >Shuffle</a>
<H1>
<center>

shuffle.cgi
Code:
#!/bin/sh
#

echo "Content-type: text/plain";
echo "";

/opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f \( -iname "*.mp3" -o -iname "*.wma" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.mpc" \) -print | /opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/rl | sed 's#^\(.*\)/\([^/]*\)#\2|0|0|file://\1/\2#; s/$/|/'

Curien, I found aod or pod work fine. "playlist" was the missing piece.

Now to figure out how to make this easy for others to install (needs rl & full find commands).
Find all posts by this user
12-15-2008, 04:29 AM
Post: #14
RE: Script for Random playlist
Code:
/opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f \( -iname "*.mp3" -o -iname "*.wma" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.mpc" \) -print

can be

Code:
find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f | egrep -i '\.(mp3|wma|flac|ogg|mpc)$'

Oversight: Jukebox | FeedTime: Automatic nzbs
Find all posts by this user
01-05-2009, 05:21 AM
Post: #15
RE: Script for Random playlist
(12-15-2008 04:29 AM)ydrol Wrote:  
Code:
/opt/sybhttpd/localhost.drives/HARD_DISK/.user_homepage/bin/find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f \( -iname "*.mp3" -o -iname "*.wma" -o -iname "*.flac" -o -iname "*.ogg" -o -iname "*.mpc" \) -print

can be

Code:
find /opt/sybhttpd/localhost.drives/HARD_DISK/Music/ -type f | egrep -i '\.(mp3|wma|flac|ogg|mpc)$'


Thanks, using egrep makes it a lot cleaner.
Find all posts by this user
Thread Closed 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Star [Apps] Bierdopje 4 NMT PHP script SilverViper 4 1,307 11-26-2012 04:57 PM
Last Post: Djiest
  PCH Audio playlist format? Nono 4 1,734 10-05-2011 10:10 PM
Last Post: Nono
  Need help debugging a simple script to enable NTFS write on HDD user33 10 3,176 04-05-2011 06:05 PM
Last Post: nuke12
  Help with Server Timeout Error running a script hindibuff 0 1,670 03-29-2011 06:21 AM
Last Post: hindibuff
Bug Executing a script on the server from the NMT? johnk 6 3,177 02-21-2011 10:59 PM
Last Post: johnk
  [AUDIO] lastfm script (bookmarks, auto-login, etc.) mk11 81 35,282 10-09-2010 01:45 AM
Last Post: opfeifer
  help with auto unrar script. kiranos 0 1,764 05-15-2010 10:24 AM
Last Post: kiranos
  Launch script after playing a video antro31 4 1,947 04-03-2010 06:32 PM
Last Post: antro31
Heart Create mono playlist ? kdavid 0 1,265 03-16-2010 07:17 PM
Last Post: kdavid
Exclamation Scheduled playback script kdavid 0 968 03-16-2010 10:27 AM
Last Post: kdavid

Forum Jump: