I'm using LAME to convert .wav files to .mp3 how I can append the passed arguments in terminal to output .mp3 name?

5,799

Original version

I suggest that you use a shellscript.

  • Use for example the name wav2mp3

  • Store the command line and all other relevant information in the shellscript.

  • I suggest that you avoid characters with a special meaning (space [ and ]) in the file name, replace with _

    #!/bin/bash
    
    options="-b 128 -m j -h -V 1 -B 256 -F"
    OptInName=${options//\ /_}
    
    # only testing here, so making it an echo command line
    
    echo lame "$options" *.wav "mymp3_$OptInName.mp3"
    
  • Make it executable

    chmod ugo+x wav2mp3
    
  • Run it (it is 'only' echoing here, showing what the real thing would look like),

    $ ./wav2mp3
    lame -b 128 -m j -h -V 1 -B 256 -F hello.wav hello world.wav mymp3_-b_128_-m_j_-h_-V_1_-B_256_-F.mp3
    

Version with a parameter

If the b-value is the only option, you want to change, you can have that as the only parameter, when you call wav2mp3.

#!/bin/bash

if [ $# -ne 1 ]
then
 echo "Usage:    $0 <b-value>"
 echo "Example:  $0 128"
else
 options="-b $1 -m j -h -V 1 -B 256 -F"
 OptInName=${options//\ /_}

# only testing here, so making it an echo command line

 echo lame "$options" *.wav "mymp3_$OptInName.mp3"
fi

Examples:

$ ./wav2mp3 128
lame -b 128 -m j -h -V 1 -B 256 -F hello.wav hello world.wav mymp3_-b_128_-m_j_-h_-V_1_-B_256_-F.mp3
$ ./wav2mp3 256
lame -b 256 -m j -h -V 1 -B 256 -F hello.wav hello world.wav mymp3_-b_256_-m_j_-h_-V_1_-B_256_-F.mp3

Version with arbitrary number of parameters

#!/bin/bash

if [ $# -eq 0 ]
then
 echo "Usage:    $0 <parameters>"
 echo "Example:  $0 -b 192 -m j -h -V 1 -B 256 -F"
else
 options="$*"
 OptInName=${options//\ /_}

# only testing here, so making it an echo command line

# When using parameters without white space (and this is the case here),
# you should use $* and when calling the program (in this case 'lame')
# I think you should *not* use quotes (") in order to get them separated.
# So $options, not "$options" in the line below.

 echo lame $options *.wav "mymp3_$OptInName.mp3"
fi

Example:

$ ./wav2mp3star -b 192 -m j -h -V 1 -B 256 -F
lame -b 192 -m j -h -V 1 -B 256 -F hello.wav hello world.wav mymp3_-b_192_-m_j_-h_-V_1_-B_256_-F.mp3
Share:
5,799

Related videos on Youtube

Lucifer
Author by

Lucifer

Updated on September 18, 2022

Comments

  • Lucifer
    Lucifer over 1 year

    My LAME command presently is :

    lame -b 128 -m j -h -V 1 -B 256 -F *.wav file.mp3
    

    What I want would be :

    in file : file.mp3

    out file : file_-b_128_-m_j_-h_-V_1_-B_256_-F.mp3

    Also I will be changing the arguments please don't post an answer that only works with these set of arguments.

    I use Ubuntu 16.04 and my LAME version is

    LAME 64bits version 3.99.5 (http://lame.sf.net)

    I have an idea maybe we can tail history with : history | tail -n 1 and append it to the .mp3 file created.

    • choroba
      choroba over 6 years
      Is lame -b 128 -m j -h -V 1 -B 256 -F *.wav 'mymp3 [ -b 128 -m j -h -V 1 -B 256 -F ].mp3' doing what you want?
    • Lucifer
      Lucifer over 6 years
      That was an example. I'm asking in general for a command $z -r option_here *.in x.out , how can one get "x [option_here].out"
  • Lucifer
    Lucifer over 6 years
    Nice try but, what about when i use-b 192 or -b 256. I want to save the effort of me having to manually change the options string you have each time i execute wav2mp3 with different -b Value. Please modify your solution so that I give the different options each time I execute wav2mp3 and they get appended each of those times.
  • sudodus
    sudodus over 6 years
    If that is the only option, you want to change, you can have that as the only parameter, when you call wav2mp3. I will modify the answer according to that.
  • sudodus
    sudodus over 6 years
    @Himanshuxd, I suggest that you extend your script with more parameters according to what you need (and balance simplicity versus flexibility). It might be a good idea to make separate scripts to solve different tasks.
  • Lucifer
    Lucifer over 6 years
    maybe I can use ./x.sh 128 j x 256 and have $0, $1, $2, $3. In my script with an echo at start showing what order these variables are to be passed and what value they default to (will make default values with if ... fi too). So you're 2nd answer is good enough for my task.
  • sudodus
    sudodus over 6 years
    @Himanshuxd, Yes, it is a good idea to add some parameters. $0 is the command that calls the script , $1 is the first parameter, $2 the second parameter etc. If you want an arbitrary number of parameters, you can use $* or $@, as described in the bash manual, linux.die.net/man/1/bash
  • sudodus
    sudodus over 6 years
    @Himanshuxd, I added a script version with arbitrary number of parameters using $*, which I think is best in this case.
  • Lucifer
    Lucifer over 6 years
    yes that was what I was looking for in the first place. Updated my question to reflect your answers.
  • sudodus
    sudodus over 6 years
    @Himanshuxd, I'm glad that I finally understood your intentions. Good luck with your file conversions :-)