-
Notifications
You must be signed in to change notification settings - Fork 27
/
makemkvchapters
executable file
·129 lines (120 loc) · 4.5 KB
/
makemkvchapters
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
#!/bin/bash
#makemkvchapters creates ordered chapters in a matroska (mkv & mka) file
SCRIPTDIR=$(dirname "${0}")
. "${SCRIPTDIR}/mmfunctions" || { echo "Missing '${SCRIPTDIR}/mmfunctions'. Exiting." ; exit 1 ;};
unset DEPENDENCIES
DEPENDENCIES=(mkvpropedit jot ffprobe)
_initialize_make
_random_uint(){
jot -r 1 1 18446744073709551615
}
_get_duration(){
ffprobe -loglevel quiet "${1}" -sexagesimal -show_entries format=duration -of default=nw=1:nk=1
}
_ask_for_chapter(){
unset ChapterSegmentUID
_report -qn "Enter the name of the chapter or 'q' to quit: "
read -e CHAPTER_NAME
if [[ "${CHAPTER_NAME}" != "q" ]] ; then
_report -qn "Enter the start time for ${CHAPTER_NAME} or 'q' to quit: "
read -e STARTTIME
if [[ "${STARTTIME}" != "q" ]] ; then
_report -qn "Enter the end time or 'q' to quit: "
read -e ENDTIME
if [[ "${ENDTIME}" != "q" ]] ; then
_report -qn "Enter linked file if any or press enter to proceed: "
read -e LINKEDSEGMENT
if [[ "${LINKEDSEGMENT}" != "q" ]] ; then
if [[ -f "${LINKEDSEGMENT}" ]] ; then
LINKEDUID=$(mkvinfo "${LINKEDSEGMENT}" | grep "Segment UID:" | cut -d: -f2 | sed 's/0x//g;s/ //g')
ChapterSegmentUID="<ChapterSegmentUID format=\"hex\">${LINKEDUID}</ChapterSegmentUID>"
fi
CHAPTER="<ChapterAtom>
<ChapterUID>$(_random_uint)</ChapterUID>
<ChapterTimeStart>${STARTTIME}</ChapterTimeStart>
<ChapterTimeEnd>${ENDTIME}</ChapterTimeEnd>
<ChapterFlagHidden>0</ChapterFlagHidden>
<ChapterFlagEnabled>1</ChapterFlagEnabled>
${ChapterSegmentUID}
<ChapterDisplay>
<ChapterString>${CHAPTER_NAME}</ChapterString>
<ChapterLanguage>eng</ChapterLanguage>
</ChapterDisplay>
</ChapterAtom>"
CHAPTER_LIST+="${CHAPTER}"
fi
fi
fi
fi
}
while [ "${*}" != "" ] ; do
INPUT="${1}"
shift
if [ -f "${INPUT}" ] ; then
MKVCHAPTERS_XML=$(_maketemp)
MKVTAGS_XML=$(_maketemp)
while [[ "${CHAPTER_NAME}" != 'q' && "${STARTTIME}" != 'q' && "${ENDTIME}" != 'q' && "${LINKEDSEGMENT}" != 'q' ]] ; do
_ask_for_chapter
done
_report "Start and end times must be in HH:MM:SS.mmm or S.mmm format. Note mmm is milliseconds and not frames."
FULL_EDITION_UUID="$(_random_uint)"
PRES_EDITION_UUID="$(_random_uint)"
echo "<?xml version='1.0' encoding='UTF-8'?>
<!-- <!DOCTYPE Tags SYSTEM 'matroskatags.dtd'> -->
<Chapters>
<EditionEntry>
<EditionFlagOrdered>1</EditionFlagOrdered>
<EditionFlagHidden>0</EditionFlagHidden>
<EditionFlagDefault>1</EditionFlagDefault>
<EditionUID>${PRES_EDITION_UUID}</EditionUID>
${CHAPTER_LIST}
</EditionEntry>
<EditionEntry>
<EditionFlagOrdered>1</EditionFlagOrdered>
<EditionFlagHidden>0</EditionFlagHidden>
<EditionFlagDefault>0</EditionFlagDefault>
<EditionUID>${FULL_EDITION_UUID}</EditionUID>
<ChapterAtom>
<ChapterUID>$(_random_uint)</ChapterUID>
<ChapterTimeStart>00:00:00.000000000</ChapterTimeStart>
<ChapterTimeEnd>$(_get_duration "${INPUT}")</ChapterTimeEnd>
<ChapterFlagHidden>0</ChapterFlagHidden>
<ChapterFlagEnabled>1</ChapterFlagEnabled>
<ChapterDisplay>
<ChapterString>Full</ChapterString>
<ChapterLanguage>eng</ChapterLanguage>
</ChapterDisplay>
</ChapterAtom>
</EditionEntry>
</Chapters>" > "${MKVCHAPTERS_XML}"
echo "<?xml version='1.0' encoding='UTF-8'?>
<!-- <!DOCTYPE Tags SYSTEM 'matroskatags.dtd'> -->
<Tags>
<Tag>
<Targets>
<EditionUID>${FULL_EDITION_UUID}</EditionUID>
<TargetTypeValue>50</TargetTypeValue>
</Targets>
<Simple>
<Name>TITLE</Name>
<String>Full Transfer</String>
<TagLanguage>eng</TagLanguage>
<DefaultLanguage>1</DefaultLanguage>
</Simple>
</Tag>
<Tag>
<Targets>
<EditionUID>${PRES_EDITION_UUID}</EditionUID>
<TargetTypeValue>50</TargetTypeValue>
</Targets>
<Simple>
<Name>TITLE</Name>
<String>Presentation</String>
<TagLanguage>eng</TagLanguage>
<DefaultLanguage>1</DefaultLanguage>
</Simple>
</Tag>
</Tags>" > "${MKVTAGS_XML}"
mkvpropedit "${INPUT}" -c "${MKVCHAPTERS_XML}" -t global:"${MKVTAGS_XML}"
fi
done