-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathoverlay-clip
executable file
·109 lines (85 loc) · 3.36 KB
/
overlay-clip
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
#!/bin/sh
#===============================================================================
# overlay-clip
# overlay one video clip on top of another video clip
#===============================================================================
# dependencies:
# ffmpeg ffprobe cut
#===============================================================================
# script usage
#===============================================================================
usage()
{
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# overlay one video clip on top of another video clip
$(basename "$0") -i input.(mp4|mkv|mov|m4v) -v input.(mp4|mkv|mov|m4v) -p [0-999] -o output.mp4
-i input.(mp4|mkv|mov|m4v) : bottom video
-v input.(mp4|mkv|mov|m4v) : overlay video
-p [0-999] : time to overlay the video
-o output.mp4 : optional argument # if option not provided defaults to input-name-overlay-date-time"
exit 2
}
#===============================================================================
# error messages
#===============================================================================
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
#===============================================================================
# check number of arguments passed to the script
#===============================================================================
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
#===============================================================================
# getopts check the options passed to the script
#===============================================================================
while getopts ':i:v:p:o:h' opt
do
case ${opt} in
i) video="${OPTARG}";;
v) overlay="${OPTARG}";;
p) position="${OPTARG}";;
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))
#===============================================================================
# variables
#===============================================================================
# video name extension and overlay video extensions
video_nopath="${video##*/}"
video_name="${video_nopath%.*}"
# defaults for variables if not defined
outfile_default="${video_name}-overlay-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
#===============================================================================
# functions
#===============================================================================
# overlay video function
overlay_video () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${video}" \
-i "${overlay}" \
-filter_complex \
"[0:0]setpts=PTS-STARTPTS[firstclip];
[1:0]setpts=PTS+${position}/TB[secondclip];
[firstclip][secondclip]overlay=enable='between(t\,${position},${dp})'[ov]" \
-map "[ov]" -map 0:1 \
-pix_fmt yuv420p \
-c:a copy -c:v libx264 -crf 18 \
-movflags +faststart \
-f mp4 \
"${outfile:=${outfile_default}}"
}
# get overlay videos duration with ffprobe
duration="$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${overlay}" | cut -d\. -f1)"
# position + duration
dp="$((position+duration))"
# run the overlay_video function
overlay_video "${video}" "${overlay}"