Little help needed


Forum: Programming and Scripting
Topic: Little help needed
started by: Zucca

Posted by Zucca on Jan. 30 2007,09:45
I got an idea of web radio with only few components...
I plan to use < poc > to stream my mp3's to web. I can tke stream from standard input. But afaik it cannot take raw stream. If it could it would be easy to just generate playlist and play it with mpg123 using -s switch.
So I managed to get it working with this: cat `cat playlist.m3u`. This will simply ouput everything inside those files specified in 'playlist.m3u'.
Ok it's missing continous play and ramdomization etc...
But the main problem is that if there's a space(s) in the paths of mp3-files then cat cuts the line there and assumes that was the path of the file and then tires to find a file which is a rest of the path after the space.
Example: /home/dsl/My Music/song.mp3
cat sees that as two files: ''/home/dsl/My' AND 'Music/song.mp3'.

I've been trying to use sed to work around this problem,  but I haven't got any solution for this problem.
Any help?

Posted by Zucca on Jan. 30 2007,09:56
Hmm. I might found a solution to this, but It's abit "CPU" consuming...
Code Sample
mpg123 -Z --list list.m3u -s | lame -r <additoinal switches> - - | poc <switches>


So after decoding mp3's with mpg123 then encode back to mp3 format suitable for streaming and output them directly to poc.

I could say this operation as "tweaking it to work". :p

EDIT: Well I could get only rumble out of it... (at few points I still could hear music.)
I tested it by outputting lame's data to a file and then listen it with xmms.

EDIT2: Okay. Now it works fine. I used wav stream instead of raw between mpg123 and lame.
Here's what I did:
Code Sample
mpg123 --list mylist.m3u -Zq -w - | lame -b 32 -q 9 - lowq.mp3


But I'd like to know a way to stream files in my playlist without any encoding/decoding...

Posted by mikshaw on Jan. 30 2007,14:53
Ok, if we can back up before you started encoding the files, you said they would stream fine but you had trouble with the format of the playlist. Is this correct?

So my gess is all you need to do is find out how to create a properly formatted playlist.
I have some questions:
What should a poc playlist look like? This is probably the most important issue.
Why are you using that redundant cat command?
Do you already have playlists made that you're trying to convert?
Is creating a playlist from scratch an option?

Posted by Zucca on Jan. 30 2007,17:09
Quote
What should a poc playlist look like? This is probably the most important issue.
Poc can stream files specified in commandline or it can take data from standard input and stream it. I'd like to use stdin.

Quote
Why are you using that redundant cat command?
I assume you mean that cat `cat playlist.m3u`?
M3u playlist simply contain a path in each line (some might have commented lines). These paths are paths to music files.
See it for yourself:
Code Sample
wget -O - http://palm.zapto.org:8888/vast/recurse.m3u | less

Now if you cat file like this it will display just path's to music files on your screen. But if you grab the output of cat and paste it into another cat's as commandline arguments then prints the contents of these files specified in that list.
For example: If you have a text file (named index.txt) containing text:
Code Sample
/home/dsl/music/part1.txt
/home/dsl/music/part2.txt

Then you execute:
Code Sample
cat `cat index.txt` >> part1_and_part2.txt
Then you have created a file that will have the contents of both /home/dsl/music/part1.txt and /home/dsl/music/part2.txt in that order. By doing this to mp3-files but then instead of creating a huge combined mp3-file you might like to pipe the data to, for example to mpg123. I did that it worked quite well.

Quote
Do you already have playlists made that you're trying to convert?
Yes. M3u format.
Quote
Is creating a playlist from scratch an option?

Well m3u playlist format if the most simple way since only return character is used as mark that seperates the paths.

Now. Only problem with cat and this playlist is if the path contains spaces.
I need to make it understand that space is not end of file's path, only return character can determine it.

Posted by mikshaw on Jan. 30 2007,19:04
I think I misunderstood what you were doing.

Do you need both mp321 and poc to do this? I was thinking poc was a player, but i guess I was wrong?

I'm not seeing where encoding/decoding is related to filenames with spaces. Are you having two separate problems here? It sounds like you jumped from one issue to the other and back again without fully addressing either issue?

Posted by ^thehatsrule^ on Jan. 30 2007,19:09
If you plan on using sed, you could use "s/ /\ /g"
If you plan on using a shell script, you could use IFS
(i.e.
IFS="
"
or use 'read')

Or you could just remember to never use spaces in filenames :P

Posted by Zucca on Jan. 30 2007,20:35
I tried that sed command and it didn't worked.
I'm thinking It should be sed -e 's/ /\\ /g'. But I'm not sure about that.

Anyway. Everybody needs to sleep sometimes. And now is my time. :p
I'm thinking this over tomorrow.

Posted by ^thehatsrule^ on Jan. 30 2007,20:44
Ah you're right, I forgot the double \\ for escape and that single quotes are needed for non-sub.
Posted by Zucca on Jan. 31 2007,18:54
Quote (Zucca @ Jan. 30 2007,17:35)
I'm thinking It should be sed -e 's/ /\\ /g'. But I'm not sure about that.

Well It didn't. =D

I did like this:
Code Sample
echo "one two three" | sed -e 's/ /\\ /g'

It really printed: "one\ two\ three"
But if I had e file named "one two three" and I did this:
Code Sample
cat `echo "one two three" | sed -e 's/ /\\ /g'`

It would print an error message like this:
Quote
cat: one: No such file or directory
cat: two: No such file or directory
cat: three: No such file or directory

So cat still thinks that "one two three" is actually three seperate files.

Posted by mikshaw on Jan. 31 2007,20:04
while read line; do
commands
done < playlist

I still have very little idea of what you're trying to accomplish, but the above inputs lines literally.  The cat command should do the same, so I'm thinking there is a problem with the way poc or mpg321 is reading the input.  I have an mpg321 script that also didn't work with spaces in filenames, but wrapping the variable which represents the filename in quotes fixed the problem:

Code Sample
find ${DIR} | while read song; do
case "$song" in
*.mp3|*.Mp3|*.MP3) ${MP3_PLAYER} $@ "$song" && sleep 1 && clear;;
*.ogg|*.Ogg|*.OGG) ${OGG_PLAYER} $@ "$song" && sleep 1 && clear;;
esac;
done


Of course this runs a unique instance of mpg321 for each song rather than using a playlist, but the point is that I needed the filename in quotes.

Posted by ^thehatsrule^ on Jan. 31 2007,22:01
Heh, what happens if you put double quotes around the outer cat?

Personally, I'd just use IFS - it's easy to do and read.

Posted by Zucca on Feb. 01 2007,05:38
Quote (^thehatsrule^ @ Jan. 30 2007,16:09)
If you plan on using sed, you could use "s/ /\ /g"
If you plan on using a shell script, you could use IFS
(i.e.
IFS="
"
or use 'read')

Or you could just remember to never use spaces in filenames :P

I don't understad this...
You are creating a variable here containing only one return char?

EDIT
Quote
      read  [-ers]  [-u  fd] [-t timeout] [-a aname] [-p prompt] [-n nchars]
      [-d delim] [name ...]
             One  line  is  read  from  the standard input, or from the file
             descriptor fd supplied as an argument to the -u option, and the
             first  word  is  assigned to the first name, the second word to
             the second name, and so  on,  with  leftover  words  and  their
             intervening separators assigned to the last name.  If there are
             fewer words read from the input stream than names, the  remain-
             ing names are assigned empty values.  The characters in IFS are
             used to split the line into words.  The backslash character (\)
             may  be used to remove any special meaning for the next charac-
             ter read and for line continuation.


Now I may know what you mean...

Posted by Zucca on Feb. 01 2007,05:53
It worked just fine.
But. I'd like to have it on one line.
So I did this:
Code Sample
IFS=`echo -e "\n"` && cat `echo -e "one two three\nfour five six\nseven" | sed -e 's/ /\\ /g'`

Now It should work but it ain't. =D
It tells me:
Quote
cat: one two three
four five six
seven: No such file or directory

When it should tell:
Quote
cat: one two three: No such file or directory
cat: four five six: No such file or directory
cat: seven: No such file or directory


Ok. I'm just about to... "Lose it. It means go crazy. Nuts. Insane. Bonzo. No longer in possession of one's faculties. Three fries short of a Happy Meal. WACKO!!"

Posted by ^thehatsrule^ on Feb. 01 2007,06:34
Well... you'd have to use something like a for loop to do that...
IFS=`printf "\n"`; for each_line in `cat list.txt`; do; cat "$each_line"; done
IFS is a special variable in posix shells that indicate the field separator (or something along those lines - usually set to whitespace)

Either way, you'll have a couple solutions... just choose one :P

Posted by WDef on Feb. 03 2007,20:41
It's probably not worth trying to get things on one line. Using command substitution (backticks) will fail once you get to a certain size and needs to be avoided if you want to process a lot of files.

This is what I'd probably do:

Code Sample
# 1st make sure file ends in a newline
sed -i '$ s/$/\n/' list.txt;

while read LINE; do
     # set single quotes around each filename to be safe
    LN=\'"${LINE}"\'
    echo "${LN}" | xargs cat
done <"list.txt"


This has the advantage that your list of filenames list.txt can be arbitrarily long.

Not tried with busybox.

Posted by WDef on Feb. 05 2007,12:58
Since I have nothing better to do -

Perl gobbles lines at once. Type this in a shell:

Code Sample
perl -ne 'open(FH,$_);while (<FH>){print}' list.txt

Posted by Zucca on Feb. 13 2007,18:08
I think solution to give mp3 stream to poc via stdin in a simple way is something like this:

loop (read list of files; ramdomize order;) {
cat random mp3
} | poc


Poc must be running all the time or clients listening it will be disconnect.
That's why we cannot do like this:
loop {
 cat random_mp3 | poc
}

But I think best way is to make mpg123 to ouput wav stream to lame and then output newly encoded, (low bitrate) stream to poc.
Advantages: lower bandwidth usage, no need to make script that randomizes playlist.
Disadvantages: more CPU usage.

Posted by Zucca on Feb. 13 2007,18:13
BTW.
Is there a simple way to grab speaker output?

I would like to do something like this:

cat /dev/speakers | lame <swithches> | poc
Then people could hear what I listen on my computer... ;)

Posted by chaostic on Mar. 16 2007,06:31
I know I'm way late on this, but saw this during a search and I figured out how to fix your problem with cating filenames with spaces.

Code Sample

cat mp3list.m3u | sed -e 's/ /\\ /g'` > temp.txt && cat 'cat temp.txt' | poc &


This has cat read the m3u, piping it into sed with changes all the spaces to "\ ". Sed then outputs it to standard out which is redirect to the new file temp.txt, which is identical to mp3list.m3u except for the \s. Then, after that command is done, you redo the recursive cat and pipe that to poc. & to background it if needed/wanted.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.