-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmp42mkv.sh
executable file
·182 lines (145 loc) · 5.34 KB
/
mp42mkv.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
#!/bin/bash
#
# Copyright 2020-2021 Jeremy Hansen <jebrhansen -at- gmail.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# This script is used to convert mp4 files to mkv files. It doesn't
# transcode, it just switches the container keeping the existing codecs and
# streams as is. It can support mp4 files in the current directory or
# subdirectories, or you can pass a directory to the script and it will check
# for mp4 files in that directory or subdirectories.
# If an srt file exists in the folder with the same name as the mp4, you can
# optionally add the subtitles into the resulting mkv file.
# You can optionally delete the original mp4 (and srt if used).
# -----------------------------------------------------------------------------
# Set up the functions
# Help function
function display_help() {
cat <<EOH
-- Usage:
$(basename $0) [options] [location]
-- Option parameters:
-h : This help.
-s : Add subtitle of same name to resulting mkv
-d : Delete the original mp4 after conversion (and remove srt if -s is used)
-- Description:
This script is used to convert mp4 files to mkv files. It doesn't
transcode, it just switches the container keeping the existing codecs and
streams as is. It can support mp4 files in the current directory or
subdirectories, or you can pass a directory to the script and it will check
for mp4 files in that directory or subdirectories.
If an srt file exists in the folder with the same name as the mp4, you can
optionally add the subtitles into the resulting mkv file.
You can optionally delete the original mp4 (and srt if used).
EOH
}
# The actual workhorse. Grabs the filename and removes the extension, then
# runs it through ffmpeg to store it in an mkv container
# Optionally add subtitles to the mkv and remove the original mp4 and srt
convertmp42mkv()
{
for fullfile in *.mp4; do
filename=$(basename "$fullfile")
filename="${filename%.*}"
echo -e "\e[1A\e[KConverting $fullfile to mkv"
# Only continue if it completes the conversion
if ffmpeg -stats -hide_banner -loglevel quiet -i "$fullfile" -vcodec copy -acodec copy "$filename".mkv; then
# Check if we should add subs (if they exist)
if [ "$ADDSUBS" == "yes" ] && [ -f "$filename"*.srt ]; then
# If there's more than 1 sub file, skip adding them.
if [ "$(ls "$filename"*.srt | wc -l)" -gt "1" ]; then
continue
fi
# Only continue if it successfully adds the subtitles
if mkvmerge -o "${filename}"-with-sub.mkv "$fullfile" --language "0:eng" --track-name "0:eng" "$filename"*.srt; then
mv "$filename"-with-sub.mkv "$filename".mkv
# Check if we should delete the subtitle file
if [ "$DELETE" == "yes" ]; then
rm -f "$filename"*.srt
fi
fi
fi
# Check if we should delete the original mp4
if [ "$DELETE" == "yes" ]; then
rm -f "$fullfile"
fi
fi
done
echo
}
# If the mp4s exist in subdirectories, let's loop through those and convert
# all the files within them
loopthrufolders()
{
for i in */; do
if ls "$i"/*.mp4 >/dev/null 2>&1; then
echo -e "Entering Directory $i\n"
cd "$i"
convertmp42mkv
cd ..
else
echo "No mp4 files in $PREFIX/$i."
fi
done
}
# Now onto the rest of the script
# Option parsing:
while getopts "dhs" OPTION
do
case $OPTION in
d ) DELETE=yes
;;
h ) display_help
exit ;;
s ) ADDSUBS=yes
;;
* ) display_help
exit ;;
esac
done
shift $(($OPTIND - 1))
# Make sure we end up back where we started
CWD=$(pwd)
# Allow setting a separate directory from the one we're in
PREFIX=
if [ -n "$1" ]; then
cd "$1"
location="$1"
else
location="current"
fi
# Check for files in the main directory
if ls ./*.mp4 >/dev/null 2>&1; then
echo -e "Converting files in $location directory.\n"
convertmp42mkv
# If no files in the main directory, check for files in subdirectories
elif ls */*.mp4 >/dev/null 2>&1; then
loopthrufolders
# If files still can't be found, exit the script with an error
else
echo "No mp4 files found in $location directory or its subdirectories"
# If a directory was passed to the script, change back to that directory.
if [ -n "$1" ]; then
cd "$CWD"
fi
exit 1
fi
# Make sure we end up back where we started
cd "$CWD"