You are not logged in.

#1 2009-03-13 12:27:35

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

how to monitor and return terminal output while running a script

Here is a simple script to record online radio to local disk.

#!/bin/bash
URL=http://64.62.252.130:8070
WORKDIR=/home/Gez/temp/mplayerdump

mplayer -dumpstream  $URL -dumpfile $WORKDIR/current.mp3

which is not satisfying enough...

I want to write a script to do this:
1.one file per song, NOT all stream recorded in one file called current.mp3
2.auto-rename "current.mp3" to "$artist_$title.mp3" everytime a song is ended

And when i run the script in terminal, i get the out put::

...
ICY Info: StreamTitle='Offspring, The - You're Gonna Go Far, Kid [Radio Edit]';StreamUrl='http://www.1.fm';
ICY Info: StreamTitle='AFI - The Leaving Song Pt. 2';StreamUrl='http://www.1.fm';

the point is, every time the stream is swithed to a new song, this output will add one line, containing "StreamTitle="
And i had no clue how tail the terminal output directly,so i used >> to write all this output into a file called xxx,then i tried:

tail  xxx -n 1  | cut -d "'" -f 2 > i

the result is nice for me::

AFI - The Leaving Song Pt. 2

i think this could be used to rename the song.

and then i tried to write the script .... and with no result sad

i think the sign to song switch is: one line added to the terminal output, which could be described with "wc -l"
but i just have no way to do this::

while doing the stream recording  "mplayer -dumpstream  $URL -dumpfile $WORKDIR/current.mp3"
how to monitor on the terminal output, so as to tell mplayer to end this song at song switch??

i hope the answer is obvious to you.
help, please!


This silver ladybug at line 28...

Offline

#2 2009-03-13 15:35:14

carlocci
Member
From: Padova - Italy
Registered: 2008-02-12
Posts: 368

Re: how to monitor and return terminal output while running a script

I would do something like

mplayer stuff 2>&1 &
while read MPLAYEROUTPUT <&1; do
   SONGNAME=$(echo $MPLAYEROUTPUT | tail  xxx -n 1  | cut -d "'" -f 2)
   rename the file
done

notes:
a) redirecting stderr to stdout might not be necessary
b) mplayer could complain about fiddling with the file while it writes to it: I would either kill and restart mplayer everytime or take note of the times leaving the song splitting part after the broadcast
c) you might want to sanitize the mplayer input since if mplayer spits out something unusual you'd be doomed

you could also look for "bash streams redirection" or something like that in order to better understand why this solution should work.

Offline

#3 2009-03-13 18:56:35

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

THANKS, carlocci
still got no result though...

mplayer stuff 2>&1 &

the last "&" means bring it to background,right ?

is it that without this "&" the while loop after "mplayer stuff" won't start until  "mplayer stuff" is done;
while by adding this "&", the while loop is run along with the "mplayer stuff" at the same time ??

well, it seems NOT though...
here is a little script for testing,called vvv

#!/bin/bash
URL=http://64.62.252.130:8070
WORKDIR=/home/Gez/x/radioe/
mplayer -dumpstream  $URL -dumpfile $WORKDIR/current.mp3  2>&1 &
while read  MPLAYEROUTPUT <&1; do
    echo "$MPLAYEROUTPUT" | tail -n 1 | cut -d "'" -f 2 >>$WORKDIR/xx
done

i ran this with "$sh vvv", i got the output in my terminal just like before
but when i look into the file $WORKDIR/xx, which had been manually created, i got nothing in it.
SO...it seems the while loop was never run.

especially, i don't quite understand
"while read MPLAYEROUTPUT <&1"
and ... why put a MPLAYEROUTPUT here??

"kill and restart mplayer everytime" should be done, but i guess there's little bit situation here, since when mplayer is killed, there're lines written to the stdout like "Exiting... (End of file)", thus the renaming stuff would go wrong.

as to note c)  i guess i'm by far more concerned by mplayer's no-splitting than spitting.........

Last edited by lolilolicon (2009-03-13 20:29:31)


This silver ladybug at line 28...

Offline

#4 2009-03-13 21:05:47

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

This sounds like something that can go wrong in a lot of ways. Not to mention how hard it is to test it out.

Why not store everything in 1 file, and use audacity to cut out songs you want?

Offline

#5 2009-03-13 21:15:52

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

something funny.... i just ran the original script::

#!/bin/bash
URL=http://64.62.252.130:8070
WORKDIR=/home/Gez/temp/mplayerdump

mplayer -dumpstream  $URL -dumpfile $WORKDIR/current.mp3

while it is doing the recording, i manually renamed the file current.mp3 to xxx.mp3, guess what? it makes no difference to mplayer!!
the file continues to grow as if nothing happened to its name.....

i find it funny, and interesting, and, confusing

any explanation?


This silver ladybug at line 28...

Offline

#6 2009-03-13 21:22:24

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

Procyon wrote:

This sounds like something that can go wrong in a lot of ways. Not to mention how hard it is to test it out.

Why not store everything in 1 file, and use audacity to cut out songs you want?

well...:)that's ok..with audacity, but not cool i guess. hmm
Especially now that i'm really into working it out....


This silver ladybug at line 28...

Offline

#7 2009-03-13 22:02:42

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

OK, how is this

while true; do mplayer -noconfig all -dumpstream -dumpfile radiodump.mp3 'http://64.62.252.130:8070' -quiet 2>&1 | awk -F\' 'BEGIN {previous="-1" } /StreamTitle/ {print $2; if ( previous == "-1" ) { previous=$2 } if ( previous != $2 ) { system("mv radiodump.mp3 \""previous"\".mp3"); system("pkill mplayer"); exit } }'; done

I am running it right now, I wonder if it will cut off anything. Also you can't run mplayer or they will get killed too, by pkill.
If there is a ' in the filename it will only rename up to there.

When a song changes it will look like this
Song A
Song B
Song B
Song C
Song C

Between a duplicate, awk will kill mplayer and it will restart, that is why.

EDIT:

It definitely cuts off too early. Sometimes up to 15 seconds before Song B starts, it will be announced.

Last edited by Procyon (2009-03-13 22:07:36)

Offline

#8 2009-03-13 22:16:08

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

cool!!

that works!! i'm trying to understand it .....

good job man !!
Bravo

Last edited by lolilolicon (2009-03-13 22:36:50)


This silver ladybug at line 28...

Offline

#9 2009-03-13 22:25:18

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

lolilolicon wrote:

cool!!

that works!! i'm trying to understand it .....

only one thing i should mention::
when it's running  python takes up to 50% of my CPU..........and my laptop is like shaking......lol..
anyway,
good job man !!
Bravo

----i guess i was wrong , the python CPU thing........this time it's all quiet, but last time when i ran it, i dont know what happened , python did freak out........


This silver ladybug at line 28...

Offline

#10 2009-03-13 22:37:03

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

Heh, weird. But what about pieces of songs being in different places? I'm not sure if it is possible to fix that, or if -nocache helps at all.

Offline

#11 2009-03-13 22:42:06

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

Procyon wrote:

EDIT:

It definitely cuts off too early. Sometimes up to 15 seconds before Song B starts, it will be announced.

about 14 seconds too early ,maybe 13.5

Last edited by lolilolicon (2009-03-13 22:48:40)


This silver ladybug at line 28...

Offline

#12 2009-03-13 22:45:04

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

Procyon wrote:

Heh, weird. But what about pieces of songs being in different places? I'm not sure if it is possible to fix that, or if -nocache helps at all.

maybe it's the broadcast designed this way?? -- song name appears prior to song itself?...


This silver ladybug at line 28...

Offline

#13 2009-03-13 22:46:47

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

Is it 14 seconds consistently? Maybe this can be as simple as adding system("sleep 14"); before the other systems.

Offline

#14 2009-03-13 22:49:50

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

Procyon wrote:

Is it 14 seconds consistently? Maybe this can be as simple as adding system("sleep 14"); before the other systems.

by now, likely it is stable here~

let me try it


This silver ladybug at line 28...

Offline

#15 2009-03-13 23:02:59

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

added  system("sleep 13.6");

seems working fine smile


This silver ladybug at line 28...

Offline

#16 2009-03-13 23:28:30

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

it's weird, no as supposed,
while a song begins at the reasonable time, it ends 13.6s too late...meaning these 13.6 seconds are double recorded
haha  guess it's the cache thing as you mentioned, Procyon


This silver ladybug at line 28...

Offline

#17 2009-03-13 23:30:00

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

I noticed that too using -nocache

Offline

#18 2009-03-13 23:33:04

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

The alternative could be instead of putting the sleep 13.6 in awk, to put it after the entire command (so I mean it will become the second command in the while loop)

Offline

#19 2009-03-14 00:58:36

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

Procyon, i'm wondering if this is possible::

often when i listen to a .ape file, which is large and containing the whole album, there will be a .cue file along with it. using the .cue file, it's convenient to go around the .ape file.
.cue is to .ape  what an index is to a book.

so,, i think it's best not to dice the mp3 file into blocks. what's better is keep it as a whole, while write an index file to it. that would be perfect !!

Procyon, can you help? really looking forward to it! pity i can't do it myself, by now..

Last edited by lolilolicon (2009-03-14 00:59:36)


This silver ladybug at line 28...

Offline

#20 2009-03-14 09:56:16

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

Yeah that is a good idea too.

What does a cue file look like? I have a few and I think it comes down to
Start

REM COMMENT "Radio Station rip"
FILE "radiodump.mp3" MP3

Per track

  TRACK 01 AUDIO
    TITLE "To be filled in"
    PERFORMER "To be filled in"
    INDEX 01 00:00:00
  TRACK 02 AUDIO
    TITLE "To be filled in"
    PERFORMER "To be filled in"
    INDEX 01 xx:xx:xx

Try this

mplayer -nocache -noconfig all -dumpstream -dumpfile radiodump.mp3 'http://64.62.252.130:8070' -quiet 2>&1 | awk -F\' '
BEGIN {
tracknumber=1;
print "REM COMMENT \"Radio Station rip\""; 
print "FILE \"radiodump.mp3\" MP3"; 
previous=systime()
}

/StreamTitle/ {
if ( tracknumber != 1 ) {
now=systime()+13.6-previous;
}
else {
now=systime()-previous;
}
printf("  TRACK %d AUDIO\n",tracknumber); 
tracknumber+=1; 
artist=$2; 
title=$2; 
gsub(" - .*", "", artist); 
gsub(".* - ", "", title); 
printf("    TITLE \"%s\"\n",title); 
printf("    PERFORMER \"%s\"\n",artist); 
printf("    INDEX 01 %s\n", strftime("%H:%M:%S", now, 1)); 
}'

EDIT

Wait, the Index is minute, second, centisecond?

One moment

Last edited by Procyon (2009-03-14 10:03:14)

Offline

#21 2009-03-14 10:45:04

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: how to monitor and return terminal output while running a script

Well, awk stuff doesn't have smaller than seconds, but date does, so this has become quite interesting with getline.

mplayer -nocache -noconfig all -dumpstream -dumpfile radiodump.mp3 'http://64.62.252.130:8070' -quiet 2>&1 | awk -F\' '
BEGIN {
print "REM COMMENT \"Radio Station rip\""; 
print "FILE \"radiodump.mp3\" MP3"; 
"date +%s.%2N" | getline previous
close("date +%s.%2N")
tracknumber=1
}

/StreamTitle/ {
if ( tracknumber != 1 ) {
"date +%s.%2N" | getline now
close("date +%s.%2N")
now=now+13.6-previous;
}
else {
now=0;
}
printf("  TRACK %02d AUDIO\n",tracknumber); 
tracknumber+=1
artist=$2; 
title=$2; 
gsub(" - .*", "", artist); 
gsub(".* - ", "", title); 
printf("    TITLE \"%s\"\n",title); 
printf("    PERFORMER \"%s\"\n",artist); 
minutes=int(now/60)
seconds=int(now%60)
centiseconds=int(100*(now-int(now)))
printf("    INDEX 01 %02d:%02d:%02d\n", minutes, seconds, centiseconds); 
}'

Now the limit of 24 hours is also gone.

EDIT

I turned all the %d into %02d, so they appear as 01, 00, etc

Last edited by Procyon (2009-03-14 10:51:05)

Offline

#22 2009-03-14 12:35:58

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: how to monitor and return terminal output while running a script

Procyon! Awesome, man!!

it's now played on audacious easily,

you're good, man:)


This silver ladybug at line 28...

Offline

Board footer

Powered by FluxBB