This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMKV-to-M2TS.sh
executable file
·284 lines (242 loc) · 9.99 KB
/
MKV-to-M2TS.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
#
# License
#
# Creates a PlayStation 3 compatible M2TS from a MKV
# Copyright (c) 2009 Flexion.Org, http://flexion.org/
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#Your script don’t work with more than one audio track, i modify this lines:
#AUDIO_ID=`grep audio ${MKV_TRACKS} | cut -d’ ‘ -f3 | sed ’s/://’ | head -n 1`
#AUDIO_FORMAT=`grep audio ${MKV_TRACKS} | cut -d’ ‘ -f5 | sed ’s/(\|A_\|)//g’ | head -n 1`
#and add this lines below of the past lines:
#if [ "$VIDEO_ID" = "" ]; then VIDEO_ID=0; fi
#if [ "$AUDIO_ID" = "" ]; then AUDIO_ID=0; fi
#if [ "$SUBS_ID" = "" ]; then SUBS_ID=0 ; fi
#Thanks for share your work!
#Sorry for my bad english. I’m mexican.
IFS=$'\n'
VER="1.1"
echo "MKV-to-M2TS v${VER} - Creates a PlayStation 3 compatible M2TS from a MKV."
echo "Copyright (c) 2009 Flexion.Org, http://flexion.org. MIT License"
echo
function usage {
echo
echo "Usage"
echo " ${0} movie.mkv [--split] [--help]"
echo ""
echo "You can also pass the following optional parameter"
echo " --split : If required, the .M2TS output will be split at a boundary less than"
echo " 4GB for FAT32 compatibility"
echo " --help : This help."
echo
exit 1
}
# Pass in the .mkv filename
function get_info {
MKV_FILENAME=${1}
local MKV_TRACKS=`mktemp`
mkvmerge -i ${MKV_FILENAME} > ${MKV_TRACKS}
local MKV_INFO=`mktemp`
mkvinfo ${MKV_FILENAME} > ${MKV_INFO}
# Get the track ids for audio/video assumes one audio and one video track currently.
VIDEO_ID=`grep video ${MKV_TRACKS} | cut -d' ' -f3 | sed 's/://'`
AUDIO_ID=`grep audio ${MKV_TRACKS} | cut -d' ' -f3 | sed 's/://' | head -n1`
SUBS_ID=`grep subtitles ${MKV_TRACKS} | cut -d' ' -f3 | sed 's/://' | head -n1`
# Get the audio/video format. Strip the V_, A_ and brackets.
VIDEO_FORMAT=`grep video ${MKV_TRACKS} | cut -d' ' -f5 | sed 's/(\|V_\|)//g'`
AUDIO_FORMAT=`grep audio ${MKV_TRACKS} | cut -d' ' -f5 | sed 's/(\|A_\|)//g' | head -n1`
# Are there any subtitles in the .mkv
if [ -z ${SUBS_ID} ]; then
SUBS_FORMAT=""
else
SUBS_FORMAT=`grep subtitles ${MKV_TRACKS} | cut -d' ' -f5 | sed 's/(\|S_\|)//g' | head -n1`
fi
# Get the video frames per seconds (FPS), number of audio channels and audio sample rate.
if [ $VIDEO_ID -lt $AUDIO_ID ]; then
# Video is before Audio track
VIDEO_FPS=`grep fps ${MKV_INFO} | sed -n 1p | cut -d'(' -f2 | cut -d' ' -f1`
else
# Video is after Audio track
VIDEO_FPS=`grep fps ${MKV_INFO} | sed -n 2p | cut -d'(' -f2 | cut -d' ' -f1`
fi
# Guess the FPS if we didn't find one (it seems tsMuxer does it that way)
# see http://en.wikipedia.org/wiki/24p
# Its needed for files with a subtitle track, otherwise tsMuxer compains
if [ -z "${VIDEO_FPS}" ]; then
echo "WARNING! H.264 stream does not contain fps field. Defaulting to 23.976."
VIDEO_FPS="23.976"
fi
VIDEO_WIDTH=`grep "Pixel width" ${MKV_INFO} | cut -d':' -f2 | sed 's/ //g'`
VIDEO_HEIGHT=`grep "Pixel height" ${MKV_INFO} | cut -d':' -f2 | sed 's/ //g'`
# Get the sample rate
AUDIO_RATE=`grep -A 1 "Audio track" ${MKV_INFO} | sed -n 2p | cut -c 27-31`
# Get the number of channels
AUDIO_CH=`grep Channels ${MKV_INFO} | sed -e 1q | cut -d':' -f2 | sed 's/ //g'`
# Is the video h264 and audio AC3 or DTS?
if [ "${VIDEO_FORMAT}" != "MPEG4/ISO/AVC" ]; then
echo "ERROR! The Video track is not H.264. I can't process ${VIDEO_FORMAT}, please use a different tool."
exit 1
elif [ "${AUDIO_FORMAT}" != "DTS" ] && [ "${AUDIO_FORMAT}" != "AC3" ]; then
echo "ERROR! The audio track is not DTS or AC3. I can't process ${AUDIO_FORMAT}, please use a different tool."
exit 1
else
echo -e "Video\t : Track ${VIDEO_ID} and of format ${VIDEO_FORMAT} (${VIDEO_WIDTH}x${VIDEO_HEIGHT} @ ${VIDEO_FPS}fps)"
echo -e "Audio\t : Track ${AUDIO_ID} and of format ${AUDIO_FORMAT} with ${AUDIO_CH} channels @ ${AUDIO_RATE}hz"
if [ -z ${SUBS_ID} ]; then
echo -e "Subs\t : none"
else
# Check the format of the subtitles. If they are not TEXT/UTF8 we can't use them.
if [ "${SUBS_FORMAT}" != "TEXT/UTF8" ]; then
SUBS_ID=""
echo -e "Subs\t : ${SUBS_FORMAT} is not supported, skipping subtitle processing"
else
echo -e "Subs\t : Track ${SUBS_ID} and of format ${SUBS_FORMAT}"
fi
fi
fi
# Clean up the temp files
rm ${MKV_TRACKS} 2>/dev/null
rm ${MKV_INFO} 2>/dev/null
}
# Define the commands we will be using. If you don't have them, get them! ;-)
REQUIRED_TOOLS=`cat << EOF
aften
chmod
cut
dcadec
echo
file
grep
mktemp
mkvextract
mkvinfo
mkvmerge
rm
sed
stat
tsMuxeR
EOF`
for REQUIRED_TOOL in ${REQUIRED_TOOLS}
do
# Is the required tool in the path?
which ${REQUIRED_TOOL} >/dev/null
if [ $? -eq 1 ]; then
echo "ERROR! \"${REQUIRED_TOOL}\" is missing. ${0} requires it to operate."
echo " Please install \"${REQUIRED_TOOL}\"."
exit 1
fi
done
# Get the first parameter passed in and validate it.
if [ $# -lt 1 ]; then
echo "ERROR! ${0} requires a .mkv file as input"
usage
elif [ "${1}" == "-h" ] || [ "${1}" == "--h" ] || [ "${1}" == "-help" ] || [ "${1}" == "--help" ] || [ "${1}" == "-?" ]; then
usage
else
MKV_FILENAME=${1}
# Is the .mkv a real Matroska file?
MKV_VALID=`file ${MKV_FILENAME} | grep Matroska`
if [ -z "${MKV_VALID}" ]; then
echo "ERROR! ${0} requires valid a Matroska file as input. \"${1}\" is not a Matroska file."
usage
fi
# It appears to be a valid Matroska file.
BASENAME=$(basename "$1" .mkv)
M2TS_FILENAME=${BASENAME}.m2ts
META_FILENAME=${BASENAME}.meta
# Audio filenames should we need to transcode DTS to AC3.
AC3_FILENAME=${BASENAME}.ac3
DTS_FILENAME=${BASENAME}.dts
shift
fi
# Init optional parameters.
M2TS_SPLIT_SIZE=0
# Check for optional parameters
while [ $# -gt 0 ];
do
case "${1}" in
-s|--split|-split)
# Get the size of the .mkv file in bytes (b)
MKV_SIZE=`stat -c%s "${MKV_FILENAME}"`
# The PS3 can't play files which are bigger than 4GB and FAT32 doesn't like files bigger than 4GB.
# Lets figure out the M2TS split size should in kilo-bytes (kb)
if [ ${MKV_SIZE} -ge 12884901888 ]; then
# >= 12gb : Split into 3.5GB chunks ensuring PS3 and FAT32 compatibility
M2TS_SPLIT_SIZE="3670016"
elif [ ${MKV_SIZE} -ge 9663676416 ]; then
# >= 9gb : Divide .mkv filesize by 3 and split by that amount
M2TS_SPLIT_SIZE=$(((${MKV_SIZE} / 3) / 1024))
elif [ ${MKV_SIZE} -ge 4294967296 ]; then
# >= 4gb : Divide .mkv filesize by 2 and split by that amount
M2TS_SPLIT_SIZE=$(((${MKV_SIZE} / 2) / 1024))
fi
shift;;
-h|--h|-help|--help|-?)
usage;;
*)
echo "ERROR! \"${1}\" is not s supported parameter."
usage;;
esac
done
# Add 'KB' to the split size for tsMuxeR compatibility
M2TS_SPLIT_SIZE=`echo "${M2TS_SPLIT_SIZE}KB"`
# Get the required infor from the MKV file and validate we can process it.
get_info ${MKV_FILENAME}
# Remove .meta file from previous run. Then create a new tsMuxeR .meta file.
rm ${META_FILENAME} 2>/dev/null
# Add split options, if required.
if [ "${M2TS_SPLIT_SIZE}" != "0KB" ]; then
echo "MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbr --vbv-len=500 --split-size=${M2TS_SPLIT_SIZE}" >> ${META_FILENAME}
else
echo "MUXOPT --no-pcr-on-video-pid --new-audio-pes --vbr --vbv-len=500" >> ${META_FILENAME}
fi
# Adding video stream.
echo "V_MPEG4/ISO/AVC, \"${MKV_FILENAME}\", fps=${VIDEO_FPS}, level=4.1, insertSEI, contSPS, ar=As source, track=${VIDEO_ID}, lang=und" >> ${META_FILENAME}
# Add audio stream.
if [ "${AUDIO_FORMAT}" == "AC3" ]; then
# We have AC3, no need to transcode.
echo "A_AC3, \"${MKV_FILENAME}\", track=${AUDIO_ID}, lang=und" >> ${META_FILENAME}
else
# We have DTS, transcoding required.
mkfifo ${DTS_FILENAME}
mkfifo ${AC3_FILENAME}
dcadec -o wavall "${DTS_FILENAME}" | aften -b 640 -v 0 -readtoeof 1 - "${AC3_FILENAME}" &
mkvextract tracks "${MKV_FILENAME}" ${AUDIO_ID}:"${DTS_FILENAME}" &
echo "A_AC3, \"${AC3_FILENAME}\", track=1, lang=und" >> ${META_FILENAME}
fi
# Add any subtitles, if required.
if [ "${SUBS_ID}" != "" ]; then
ARIAL_TTF=`locate Arial.ttf | head -n1`
echo "S_TEXT/UTF8, \"${MKV_FILENAME}\", font-name=\"${ARIAL_TTF}\", font-size=65, font-color=0x00ffffff, bottom-offset=24, font-border=2, text-align=center, video-width=${VIDEO_WIDTH}, video-height=${VIDEO_HEIGHT}, fps=${VIDEO_FPS}, track=${SUBS_ID}, lang=und" >> ${META_FILENAME}
fi
# For debugging
#cat ${META_FILENAME}
# Convert the MKV to M2TS
tsMuxeR ${META_FILENAME} ${M2TS_FILENAME}
# Remove the transient files
rm ${META_FILENAME} 2>/dev/null
rm ${AC3_FILENAME} 2>/dev/null
rm ${DTS_FILENAME} 2>/dev/null
# Change the permission on the M2TS file(s) to something sane.
chmod 644 ${BASENAME}*.m2ts 2>/dev/null
echo "All Done!"