Find the answer to your Linux question:
Results 1 to 4 of 4
hello again i have solved some of my orig problem but can someone help me clean up this script #!/bin/bash # constants ENCODER="oggenc -q 4 " MPLAYER="mplayer" PIPE=`mktemp` usage() { ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Location
    UK
    Posts
    2

    soundpipe help


    hello again i have solved some of my orig problem but can someone help me clean up this script

    #!/bin/bash

    # constants
    ENCODER="oggenc -q 4 "
    MPLAYER="mplayer"
    PIPE=`mktemp`

    usage()
    {
    echo "Usage: $0 <stream>"
    exit 1
    }

    #this bit idon't know what it does. exactly. is it all necessary? now i have no options.
    while getopts "o:s:" opt; do
    case "$opt" in
    ?) usage ;;
    esac
    done
    shift `expr $OPTIND - 1`

    # this bit too
    [ -z "$1" ] && usage
    input="$@"

    rm -f $PIPE
    mkfifo $PIPE
    $ENCODER -o $input.ogg -r $PIPE &
    $MPLAYER -vc dummy -vo null -ao pcm:file=$PIPE $input
    echo "^D" > $PIPE
    rm -f $PIPE

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044

    Re: soundpipe help

    Quote Originally Posted by matt me
    hello again i have solved some of my orig problem but can someone help me clean up this script
    I'll bite.
    Code:
    #!/bin/bash
    
    # constants
    ENCODER="oggenc -q 4 "
    MPLAYER="mplayer"
    PIPE=`mktemp`
    
    usage&#40;&#41;
    &#123;
    	echo "Usage&#58; $0 <stream>"
    	exit 1
    &#125;
    
    #this bit  idon't know what it does. exactly. is it all necessary? now i have no options.
    while getopts "o&#58;s&#58;" opt; do 
    	case "$opt" in
    		?&#41; usage ;;
    	esac
    done
    getopts is used to process options. This one is allowing two options, o and s which each take a value (the following colon specifies that).
    Code:
    shift `expr $OPTIND - 1`
    This gets rid of the arguments you've processed and leaves $1 with the first unprocessed argument value.
    Code:
    # this bit too
    &#91; -z "$1" &#93; && usage
    This bit tests the first argument (ignoring the processed options, remember) for being empty (the -z for zero) and if it is (&&) calls the usage() function.
    Code:
    input="$@"
    This line assigns all the remaining arguments to the variable named "input", each variable being surrounded by double quotes (cf. $*, which concatenates all the aguments together, separated by the first character of IFS).
    Code:
    rm -f $PIPE
    mkfifo $PIPE
    $ENCODER -o $input.ogg -r $PIPE &
    $MPLAYER -vc dummy -vo null -ao pcm&#58;file=$PIPE $input 
    echo "^D" > $PIPE
    rm -f $PIPE
    [/quote]
    Any more questions?

  3. #3
    Just Joined!
    Join Date
    Jun 2005
    Location
    UK
    Posts
    2
    thanks very much all the guides on shell scripting were way too complicated, and the one on wikibooks has yet to be written.

    i moved the script to perl, using system() to run commands - i wanted it to run for all mpc/wma files in the current directory. i imagine it would be possible to use a shell script to feed variables to a perl script?

    on the audio side,
    a) mkfifo $PIPE (what does this do? is it really necessary?)
    b) is pcm the best audio format to use? it's prob too large for half hour long files!
    c) echo "^D" > $PIPE (i guess this closes the pipe somehow, but i took it out in the perl script, and got away with it)

    and if anyone knows any perl...
    i used a fixed location for pipe, cos i wanted to assign it tothe value of mktemp, but $pipe = system("mktemp") didn't work.

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by matt me
    i moved the script to perl, using system() to run commands - i wanted it to run for all mpc/wma files in the current directory. i imagine it would be possible to use a shell script to feed variables to a perl script?
    Not really much point, though. Perl can handle multiple file names by using the "while (<>) { }" construct, with the body of your code between the braces. You could also get your shellscript to handle all files in the current directory by using:
    Code:
    for file in *
    do
    <your code here>
    done
    with the body of your existing script between "do" and "done".

    on the audio side,
    a) mkfifo $PIPE (what does this do? is it really necessary?)
    This creates a "pipe" file named for the value returned by the "mktemp" program.
    b) is pcm the best audio format to use? it's prob too large for half hour long files!
    I'lll pass on this - I know scripts, but not digital audio formats!
    c) echo "^D" > $PIPE (i guess this closes the pipe somehow, but i took it out in the perl script, and got away with it)
    As you've deduced, it sends an EOF to the pipe. Closing the pipe has the same effect, so shouldn't be necessary.
    [/quote]and if anyone knows any perl...
    i used a fixed location for pipe, cos i wanted to assign it to the value of mktemp, but $pipe = system("mktemp") didn't work.[/quote]
    They system() call returns the exit status of the program you run, not the standard output of it. In Perl you do something like:
    Code:
    open&#40;FH, "mktemp |"&#41;;
    and then the encoder will read from the FH filehandle.

    I'd stick with the shellscript to start with, as it's quicker to get the bugs out and easier to understand what's going on - Perl can be a bit obscure if you hit a snag and can't figure it out. It's more powerful than shellscript, but is overkill for what you're doing here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •