Convertir un fichier MKV en AVI
Publi� le 7 décembre 2006

Voici un script destiné à convertir un fichier au format matroska (MKV) en fichier AVI pour lecture sur les platines de salon. La vidéo sera convertie au format xvid et la bande sonore au format mp3.

Placez le script suivant dans un fichier et donnez lui les droits d'éxécution (chmod +x convert.sh).

Les variables INPUT et OUTPUT sont à définir avec le chemin complet du fichier à convertir et sa destination.


#!/bin/sh

apt-get install mkvtoolnix transcode

BLUE
="\033[1;36m"
STANDARD="\033[0;0m"

VIDEO_CHANNEL=1
AUDIO_CHANNEL
=2
AUDIO_TYPE
="ogg"

LAME_BITRATE=128

INPUT
="nothing"
OUTPUT="nothing"

while [ "x$1" != "x"  ]
do
  case
"$1" in
      
"--bitrate" | "-b" )
      
shift
      LAME_BITRATE
=$1
      shift
      
;;
      
"--video-channel" | "-v" )
      
shift
      VIDEO_CHANNEL
=$1
      shift
      
;;
      
"--audio-channel" | "-a" )
      
shift
      AUDIO_CHANNEL
=$1
      shift
      
;;
      
"--audio-type" | "-at" )
      
shift
      AUDIO_TYPE
=$1
      shift
      
;;
      
"--input" | "-i" )
      
shift
      INPUT
=$1
      shift
      
;;
      
"--output" | "-o" )
      
shift
      OUTPUT
=$1
      shift
      
;;
      
"--help" | "-h" | * )
      echo
"$(basename $0) : Convert a Matroska file into Avi"
      
echo "  Usage : $(basename $0) [options]"
      
echo "    Options :"
      
echo "      -i f  | --input f         : f is the input file [MANDATORY]"
      
echo "      -o f  | --output f        : f is the output file"
      
echo "      -b n  | --bitrate n       : n is the bitrate for mp3 sound (default : 128)"
      
echo "      -v n  | --video-channel n : n is the video channel to use (default : 1)"
      
echo "      -a n  | --audio-channel n : n is the audio channel to use (default : 2)"
      
echo "      -at t | --audio-type t    : t is the codec of the original sound track (default : ogg)"
      
echo "      -h    | --help            : what you're reading right now !"
      
exit
      ;;
  
esac
done

if [ $OUTPUT = "nothing" ]
    
then
    OUTPUT
=${INPUT%mkv}avi
fi

MKVEXTRACT_AVI_OUTPUT
=`mktemp aviXXXXXX`
rm -f $MKVEXTRACT_AVI_OUTPUT
MKVEXTRACT_OGG_OUTPUT
=`mktemp oggXXXXXX`
rm -f $MKVEXTRACT_OGG_OUTPUT
MKVEXTRACT_WAV_OUTPUT
=`mktemp wavXXXXXX`
rm -f $MKVEXTRACT_WAV_OUTPUT
MKVEXTRACT_MP3_OUTPUT
=`mktemp mp3XXXXXX`
rm -f $MKVEXTRACT_MP3_OUTPUT

echo -e "$BLUE-= Extracting video =-$STANDARD"
mkvextract tracks $INPUT $VIDEO_CHANNEL:$MKVEXTRACT_AVI_OUTPUT

echo -e "$BLUE-= Extracting audio =-$STANDARD"
mkvextract tracks $INPUT $AUDIO_CHANNEL:$MKVEXTRACT_OGG_OUTPUT

case "$AUDIO_TYPE" in
    
"ogg" | "OGG" )
    echo -
e "$BLUE-= Converting ogg/vorbis to wav =-$STANDARD"
    
oggdec --output=$MKVEXTRACT_WAV_OUTPUT.wav $MKVEXTRACT_OGG_OUTPUT
    rm
-f $MKVEXTRACT_OGG_OUTPUT
  
    
echo -e "$BLUE-= Converting wav to mp3 =-$STANDARD"
    
lame --cbr -b$LAME_BITRATE $MKVEXTRACT_WAV_OUTPUT.wav $MKVEXTRACT_MP3_OUTPUT
    rm
-f $MKVEXTRACT_WAV_OUTPUT.wav
    
;;
    
"mp3" | "MP3" )
    
MKVEXTRACT_MP3_OUTPUT=$MKVEXTRACT_OGG_OUTPUT
    
;;
esac

echo -e "$BLUE-= Building final avi =-$STANDARD"
avimerge -i $MKVEXTRACT_AVI_OUTPUT -p $MKVEXTRACT_MP3_OUTPUT -o $OUTPUT
rm
-f $MKVEXTRACT_AVI_OUTPUT $MKVEXTRACT_MP3_OUTPUT

echo -e "$BLUE-= Done ! file created : $OUTPUT =-$STANDARD"

apt-get autoremove mkvtoolnix transcode