-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkv_fix_av_order.sh
More file actions
executable file
·57 lines (49 loc) · 1.45 KB
/
mkv_fix_av_order.sh
File metadata and controls
executable file
·57 lines (49 loc) · 1.45 KB
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
#!/bin/bash
trap "exit" INT
mkvfile="$1"
# Check if file is MKV
if [[ ! -f $mkvfile ]] || [[ "$(file -b "$mkvfile")" != "Matroska data" ]]
then
echo "MKV file not provdied"
exit 1
fi
# Check MKV file info
mkvresult=`mkvmerge -i "$mkvfile" | tail -n+2`
# Count Tracks
trackcount=`echo "$mkvresult" | wc -l`
# If video is first track, nothing to fix
if ( echo "$mkvresult" | head -1 | grep -iq video )
then
exit 0
fi
# Check if video isn't first track
if ( echo "$mkvresult" | head -1 | grep -iqv video )
then
echo "fixing AV order $mkvfile"
filebasename=${mkvfile%.*}
audfilename="${filebasename}_A.mkv"
vidfilename="${filebasename}_V.mkv"
extrasfilename="${filebasename}_E.mkv"
renamedfile="${filebasename}.mkv.old"
# Extract audio
mkvmerge -q -o "$audfilename" -D -S -B -T --no-chapters -M --no-global-tags "$mkvfile"
# Extract video
mkvmerge -q -o "$vidfilename" -A -S -B -T --no-chapters -M --no-global-tags "$mkvfile"
# Extract extras/attachments
mkvmerge -q -o "$extrasfilename" -A -D "$mkvfile"
# Rename current file to .old
mv "$mkvfile" "$renamedfile"
# Combine video, audio, extra
mkvmerge -q -o "$mkvfile" "$vidfilename" "$audfilename" "$extrasfilename"
rm "$audfilename"
rm "$vidfilename"
rm "$extrasfilename"
if [ -f "$renamedfile" ] && [ -f "$mkvfile" ]
then
rm "$renamedfile"
echo "$mkvfile AV order fixed"
else
echo "Error fixing AV order $mkvfile."
mv "$renamedfile" "$mkvfile"
fi
fi