-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_audiotracks_mkv.sh
130 lines (91 loc) · 2.59 KB
/
extract_audiotracks_mkv.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# Extract non-english audio tracks and subtitles from each MKV file in the given directory
#function to grab tracknumber and from variable
extract_track_info() {
#how to use this function
#extract_track_info $string will create array with results and return the array to caller
#by default set these variables
string=${1}
eng_id=0
# Create an associative array
declare -a tracks=()
#loop through string
while read string
do
#by default set these variables
language=''
tracknumber=''
#get the tracknumber
tracknumber=`echo $string | cut -c1-15 | grep -o '[0-9]*'`
#get just the language of the track name
language=`echo "$string" | tr " " "\n" | grep 'track_name'`
language=${language/':'/}
language=${language/'track_name'/}
#check if language is english
if [ $language = "english" ]; then
#set eng_id to this value
set eng_id = $tracknumber
fi
#append these to array
tracks[1]=$tracknumber
tracks[2]=$language
tracks[3]=$eng_id
done
#return array
echo ${tracks[@]}
}
# If no directory is given, work in local dir
if [ "$1" = "" ]; then
DIR="."
else
DIR="$1"
fi
# Get all the MKV files in this dir and its subdirs
find "$DIR" -type f -name '*.mkv' | while read filename
do
#get movie title
title=$(basename "$filename")
extension="${title##*.}"
title="${title%.*}"
# Find out which tracks contain audio tracks
mkvmerge -I "$filename" | grep 'audio' | while read sublime
do
#by default set these variables
#eng_id=0
#language=''
#tracknumber=''
#call function to get parse each line of the mkvmerge info
tracks=$(extract_track_info $sublime)
#display title of movie
echo $title
echo "Records: ${#tracks[@]}"
echo "Track ID: $tracks[1]"
echo "Language: $tracks[2]"
#display results
#echo 'Track ID: '$tracknumber
#echo 'Language: '$language
#create new audio track filename
fn=$(basename "$filename")
extension="${fn##*.}"
fn="${fn%.*}"
fn="$fn"."$language"."$extension"
#if this track is english save the track_id
#if [ $language = "english" ]; then
#set the track id to this track number
#eng_id=${tracknumber}
#fi
#check if language is not english
#if [ ! $language = "english" ]; then
#check if $fn does not exist
#if [ ! -f "$fn" ]; then
#now extract this audio track to a file just as a backup move
#mkvextract tracks "$filename" "$tracknumber":"${fn}"
#fi
#has english id been identified
#if [ $eng_id > 0 ]; then
#now remove this audio track
#echo mkvmerge -o "$filename" --atracks "$eng_id" "$filename"
#fi
#fi
done
done