New script for recompressing AVCHD video files into x264 mkv files
[videoscripts/.git] / avchd2h264
1 #!/bin/bash
2
3 # Command line options
4 INPUT=$1
5 OUTPUT_DIR=$2
6
7 # Recommended min quality for 1080p HD = 22
8 # Quality 17 ~ 30% of the original size
9 # Quality 18 ~ 25% of the original size
10 # Quality 20 ~ 15% of the original size
11 QUALITY=18
12
13 if [ -z "$INPUT" ]; then
14   echo "Usage: $0 input outputdir"
15   exit 1
16 fi
17
18 if [ -z "$OUTPUT_DIR" ]; then
19     OUTPUT_DIR="."
20 fi
21
22 FILENAME=`basename "${INPUT%.*}"`
23 OUTPUT="$OUTPUT_DIR/$FILENAME.mkv"
24
25 # We want our audio to be pass-through, so detect what the audio of the input is encoded with, and tell handbrake to make the output match
26 AUDIO_CODEC=`avconv -i "$INPUT" 2>&1 | grep "Audio" | sed -r -e 's/.*?Audio: (\S+),.*?/\1/'`
27 if [ -z "$AUDIO_CODEC" ]; then
28   echo "-E- Unable to determine audio codec to use for the input video: $INPUT"
29   exit 1
30 fi
31 AUDIO_ENC="copy:$AUDIO_CODEC"
32
33 echo "-> Converting \"$INPUT\" to 1080p compressed \"$OUTPUT\" file using audio codec $AUDIO_CODEC"
34
35 if [[ -e "$OUTPUT" ]]; then
36   echo "-E- Output file $OUTPUT already exists. Aborting..."
37   exit 1
38 fi
39
40 HandBrakeCLI -i "$INPUT" -o "$OUTPUT" -f mkv --denoise="weak" -e x264 -q $QUALITY -x b-adapt=2:rc-lookahead=50 -v 2 -E $AUDIO_ENC -a 1 -6 dpl2 --strict-anamorphic --crop 0:0:0:0 --preset="High Profile"
41