forked from NapoleonWils0n/ffmpeg-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtitle-add
More file actions
executable file
·103 lines (88 loc) · 2.53 KB
/
Copy pathsubtitle-add
File metadata and controls
executable file
·103 lines (88 loc) · 2.53 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
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
#!/bin/sh
# add subtitles to a video
# script usage
usage()
{
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# add subtitles to a video
$(basename "$0") -i infile.(mp4|mkv|mov|m4v) -s subtitle.srt -o outfile.mp4
-i infile.(mp4|mkv|mov|m4v)
-s subtitle.srt
-o outfile.mp4 :optional agument # if option not provided defaults to infile-name-subs-date-time"
exit 2
}
# error messages
NOTFILE_ERR='not a file'
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
FILE_EXT_ERR='has the wrong file extension'
NOT_MEDIA_FILE_ERR='is not a media file'
# if script is run arguments pass and check the options with getopts,
# else display script usage and exit
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
# getopts check and validate options
while getopts ':i:s:o:h' opt
do
case ${opt} in
i) video="${OPTARG}"
[ -f "${video}" ] || usage "${video} ${NOTFILE_ERR}";;
s) subs="${OPTARG}"
[ -f "${subs}" ] || usage "${subs} ${NOTFILE_ERR}";;
o) outfile="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
# infile, infile name and extension
subs_ext="${subs##*.}"
video_nopath="${video##*/}"
video_name="${video_nopath%.*}"
# run the ffmpeg function based on the audio file extension
case "$subs_ext" in
srt);;
*) usage "${subs} ${FILE_EXT_ERR}";;
esac
# file command check input file mime type
video_filetype="$(file --mime-type -b "${video}")"
subs_filetype="$(file --mime-type -b "${subs}")"
# video mimetypes
mov_mime='video/quicktime'
mkv_mime='video/x-matroska'
mp4_mime='video/mp4'
m4v_mime='video/x-m4v'
# srt subtitles mime type
srt_mime='text/plain'
# check the subtitles mime type
case "${subs_filetype}" in
${srt_mime});;
*) usage "${subs} ${NOT_MEDIA_FILE_ERR}";;
esac
# defaults for variables if not defined
outfile_default="${video_name}-subs-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
# audio is aac, copy audio stream
addsubs () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${video}" \
-f srt \
-i "${subs}" \
-c:a copy \
-c:v copy \
-c:s mov_text -metadata:s:s:0 \
language=eng \
-movflags +faststart \
-f mp4 \
"${outfile:=${outfile_default}}"
}
# check the video mime type
case "${video_filetype}" in
${mov_mime}|${mkv_mime}|${mp4_mime}|${m4v_mime}) addsubs "$video" "$subs";;
*) usage "${video} ${NOT_MEDIA_FILE_ERR}";;
esac