Skip to content

Instantly share code, notes, and snippets.

@kaleksandrov
Created May 5, 2016 19:20
Show Gist options
  • Select an option

  • Save kaleksandrov/58eed0d9676db0f7fec74c56a3b26631 to your computer and use it in GitHub Desktop.

Select an option

Save kaleksandrov/58eed0d9676db0f7fec74c56a3b26631 to your computer and use it in GitHub Desktop.

Revisions

  1. kaleksandrov created this gist May 5, 2016.
    52 changes: 52 additions & 0 deletions 2mp3.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/bin/sh

    case $# in
    1)
    FROM="$1"
    TO="$1"
    ;;
    2)
    FROM="$1"
    TO="$2"
    ;;
    *)
    echo "Usage: $0 <from-folder-or-file> <optional:to-folder-or-file>"
    exit 1
    ;;
    esac

    IFS=''

    if ! [ -e "$FROM" ]
    then
    echo "The source path is invalid: $FROM"
    exit 2
    elif [ -d "$FROM" ]
    then
    FROM=$FROM/*.*
    if ! [ -d "$TO" ]
    then
    echo "The destination file does not exists: $TO"
    exit 3
    fi
    elif [ -f "$FROM" ]
    then
    FROM=$FROM
    if [ -e "$TO" -a -d "$TO" ]
    then
    TO="$TO$(basename $FROM)"
    fi
    fi

    for FILE in $FROM
    do
    SIMPLE_FILE="$(basename $FILE)"
    EXTENSION="${SIMPLE_FILE##*.}"
    FILENAME="${SIMPLE_FILE%.*}"

    echo '------------'
    ffmpeg -i "$FILE" -vn -acodec libmp3lame -ac 2 -ab 320k -ar 48000 "$TO/$FILENAME.mp3"
    echo '------------'
    done

    unset IFS