-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.sh
executable file
·62 lines (55 loc) · 1.87 KB
/
convert.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /bin/bash
DEFAULT_PRESET=Normal
trim() {
local var=$@
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
process() {
local inputDir="$1"
local outputDir="$2"
local preset="$3"
local i=0
local name=$(echo "$inputDir" | awk -F/ '{print $(NF-1)}')
relPath=$(realpath --relative-to="$input" "$inputDir/..")
local outputDir="${output}/${relPath}"
mkdir -p "$outputDir"
echo "Output dir is ${outputDir}"
titles=$(trim $(HandBrakeCLI -i "$inputDir" -t 0 2>&1 | grep "+ title" | wc -l))
for i in $(seq 1 $titles)
do
local outputFile=$(printf "${outputDir}/${name} - Track %02d.mp4" $i)
local outputLockFile=$(printf "${outputDir}/.${name} - Track %02d.mp4.lock" $i)
if [ -f "$outputFile" ] && [ ! -f "$outputLockFile" ]; then
echo "Skipping: $outputFile"
continue
fi
echo "Converting title $i of $titles. Start time: $(date)"
echo "Writing into: ${outputFile}"
local cmd="HandBrakeCLI --input '$inputDir' --title $i --preset '$preset' --output '$outputFile' </dev/null 2>/dev/null"
echo $cmd >> "$outputLockFile"
eval "$cmd"
rm "$outputLockFile"
done
}
preset="${3:-$DEFAULT_PRESET}"
if [ -z "$2" ] ; then
echo "Usage: ./convert.sh /path/to/input /path/to/output [preset]"
exit 1
fi
input=$(trim "$1")
output=$(trim "$2")
startFrom="${4:-1}"
echo "Searching for DVDs..."
tmpFile="/tmp/dvds.txt"
find "$input" | grep "/VIDEO_TS$" > $tmpFile
total=$(wc "$tmpFile" | awk {'print $1'})
echo "Found $total DVDs"
for ((i=startFrom; i<=total; i++))
do
line="$(tail -n+$i "$tmpFile" | head -1)"
echo "#### Processing $i of $total: $line ####"
process "$line" "$output" "$preset"
done
echo "Completed"