forked from NapoleonWils0n/ffmpeg-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-frame
More file actions
executable file
·86 lines (73 loc) · 2.29 KB
/
Copy pathextract-frame
File metadata and controls
executable file
·86 lines (73 loc) · 2.29 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
#!/bin/sh
# extract a frame froma a video as a png file
# script usage
usage()
{
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# extract a frame froma a video as a png file
https://trac.ffmpeg.org/wiki/Seeking
$(basename "$0") -i infile.(mp4|mov|mkv|m4v|webm) -s 00:00:00.000 -o outfile.png
-i infile.(mp4|mov|mkv|m4v)
-s 00:00:00.000 :optional argument # if option not provided defaults to 00:00:00
-o outfile.png :optional agument # if option not provided defaults to infile-name-frame-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}"
# timecode - match 00:00:00.000
timecode='^[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}([.]\{1\}[0-9]\{1,3\})?$'
# getopts check and validate options
while getopts ':i:s:o:h' opt
do
case ${opt} in
i) infile="${OPTARG}"
[ -f "${infile}" ] || usage "${infile} ${NOTFILE_ERR}";;
s) seconds="${OPTARG}"
expr "${seconds}" : "${timecode}" 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
infile_nopath="${infile##*/}"
infile_name="${infile_nopath%.*}"
# file command check input file mime type
filetype="$(file --mime-type -b "${infile}")"
# video mimetypes
mov_mime='video/quicktime'
mkv_mime='video/x-matroska'
mp4_mime='video/mp4'
m4v_mime='video/x-m4v'
webm_mime='video/webm'
# outfile file recording destination
outfile_default="${infile_name}-frame-$(date +"%Y-%m-%d-%H-%M-%S").png"
seconds_default='00:00:00'
# image to video function
extract () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${seconds:=${seconds_default}}" \
-i "${infile}" \
-q:v 2 -f image2 \
-vframes 1 \
"${outfile:=${outfile_default}}"
}
# check the files mime type
case "${filetype}" in
${mov_mime}|${mkv_mime}|${mp4_mime}|${m4v_mime}|${webm_mime}) extract "${infile}";;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac