LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   file convert (https://www.linuxquestions.org/questions/debian-26/file-convert-4175443252/)

tsitras 12-28-2012 08:37 AM

file convert
 
hi. i am looking to convert files (manually) for my asterisk. i do have some files in .gsm, .wav, .alaw, .ulaw format. i need to change the format between them.
in another box that i have that runs centos 6 the command is
file convert name.wav file.ulaw
same applies for all other formats.
unfortunately i cannot see how to make it work in debian.

Snark1994 12-28-2012 10:37 AM

Sorry, you run
Code:

file convert foo.wav bah.ulaw
and it converts the file "foo.wav" to the file "bah.ulaw"?

What version of 'file' do you have on your CentOS box?

I would write a shell script for this - I would expect that to convert between each pair of formats, you will need to run a different command, so I would write a script to work out the format of the files (based on their extensions) and then run the appropriate command. Does that answer your question, or were you looking for something more specific?

tsitras 12-28-2012 11:00 AM

could you post the script please?
the file formats that i would like to convert between them are:
wav, mp3, gsm, g729, alaw, ulaw

Snark1994 12-29-2012 09:58 AM

You're lucky it's a Saturday ;) normally it's better for you to try it yourself: I and the other people on the forum will often not have time to write and test a script for you, and you will learn a lot more doing it yourself! However, here it is:

Code:

#!/usr/bin/env bash

#####################
# Edit this section #
#####################

# Edit this section to add new formats:
# commands["extension_from extension_to"]="convert_command"
# INFILE and OUTFILE in the conversion command are replaced with the appropriate filenames
declare -A commands
commands["wav mp3"]="ffmpeg -i INFILE OUTFILE"
commands["mp3 wav"]="ffmpeg -i INFILE OUTFILE"
commands["wav gsm"]="sox INFILE -r 8000 -c 1 OUTFILE resample -ql"
commands["mp3 gsm"]="sox INFILE -r 8000 -c 1 OUTFILE resample -ql"
commands["gsm mp3"]="lame -V 9 INFILE OUTFILE"

#####################
# Stop editing here #
#####################
if [ ! $# -eq 2 ]; then
    echo "Usage: $0 inputfile outputfile"
    exit 1
fi
ext1=${1##*.}
ext2=${2##*.}

command=${commands["$ext1 $ext2"]-"NULL"}
if [ "$command" == "NULL" ]; then
    echo "Error: No rule for converting .$ext1 to .$ext2"
    exit 1
fi
command=$(echo "$command" | sed "s/INFILE/'$1'/g; s/OUTFILE/'$2'/g")
eval $command

Hope this helps,

tsitras 12-29-2012 10:10 AM

thanks a lot.

Snark1994 12-29-2012 10:11 AM

If it works for you, then please remember to come back and mark the thread as 'SOLVED'.

Thanks,


All times are GMT -5. The time now is 08:00 AM.