forked from NapoleonWils0n/ffmpeg-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg2video
More file actions
executable file
·79 lines (66 loc) · 1.92 KB
/
Copy pathimg2video
File metadata and controls
executable file
·79 lines (66 loc) · 1.92 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
#!/bin/sh
# image to video
# script usage
usage()
{
echo "\
# image to video
$(basename "$0") -i infile.(png|jpg|jpeg) -d (000) -o outfile.mp4
-i infile.(mp4|mkv|mov|m4v)
-d (000) : duration
-o outfile.mp4 :optional agument # if option not provided defaults to infile-name-video-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'
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}"
# duration regex match 0.[1-9]
dur_regex='^[0-9]\{1,3\}$'
# getopts check and validate options
while getopts ':i:d:o:h' opt
do
case ${opt} in
i) infile="${OPTARG}"
[ -f "${infile}" ] || usage "${infile} ${NOTFILE_ERR}";;
d) dur="${OPTARG}"
expr "${dur}" : "${dur_regex}" 1>/dev/null;;
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
infile_nopath="${infile##*/}"
infile_name="${infile_nopath%.*}"
# outfile file recording destination
outfile_default="${infile_name}-video-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
# file command check input file mime type
filetype="$(file --mime-type -b "${infile}")"
# image mimetypes
png_mime='image/png'
jpg_mime='image/jpeg'
# image to video function
imgtovid () {
ffmpeg \
-hide_banner \
-stats -v panic \
-framerate 1/"${dur}" \
-i "${infile}" \
-c:v libx264 -crf 18 -profile:v high \
-r 30 -pix_fmt yuv420p \
-movflags +faststart -f mp4 \
"${outfile:=${outfile_default}}"
}
# check the files mime type is a image
case "${filetype}" in
${png_mime}|${jpg_mime}) imgtovid "${infile}";;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac