if [ $# -lt 6 ]; then
	echo "$0 <h264_video> <video_FPS> <g711_audio> <audio_format> <audio_channel> <audio_sampling_rate>"
	echo "ex : $0 beethoven_480p_h264.bin 30 beethoven_g711u.raw s16le 1 8000"
	exit
fi

VideoFile=$1
VideoFps=$2
AudioFile=$3
AudioFormat=$4
AudioChannel=$5
AudioSamplingRate=$6
AudioEncoding=""

echo "Convert video file ,please wait"
cvlc -q $VideoFile --demux=H264 --h264-fps $VideoFps :sout=#transcode{vcodec=h264}:file{dst=tmp.mp4} vlc://quit

if [ "$AudioFormat" == "s16le" ]
then
	AudioFormat="s16"
	AudioEncoding="signed-integer"
elif [ "$AudioFormat" == "alaw" ]
then
	AudioFormat="raw"
	AudioEncoding="a-law"
elif [ "$AudioFormat" == "mulaw" ]
then
	AudioFormat="raw"
	AudioEncoding="mu-law"
else
	echo "The audio format [$AudioFormat] is not support"
	echo "Support Audio Format is [s16le, alaw raw]"
	exit
fi

echo "Convert audio file ,please wait"
sox --channels $AudioChannel --type $AudioFormat --rate $AudioSamplingRate -e $AudioEncoding $AudioFile tmp.wav
cvlc -q tmp.wav --sout="#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100}:std{access=file,mux=raw,dst='tmp.mp3'}" vlc://quit


echo "Merge video and audio,please wait"
cvlc -q tmp.mp4 --input-slave tmp.mp3 :sout=#transcode{vcodec=h264}:file{dst=output.mp4} vlc://quit
rm tmp.wav
rm tmp.mp3
rm tmp.mp4
echo "Finish -> output.mp4"
