forked from m1tk4/video-gadgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdbars2
executable file
·184 lines (161 loc) · 5.87 KB
/
hdbars2
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
183
184
#!/bin/bash
#
# Produces test pattern with HD SMPTE Bars, local time, timecode,
# and a beep every 1 s with visually matching indicator
#
# MIT License
#
# Copyright (c) 2022 Dimitri Tarassenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# include our common headers
source "$(dirname "$0")/_video-gadgets-common.sh"
# Show help and quit with errorlevel 1
help_and_bail() {
cat << EOF
usage: hdbars [options] <outputFormat> <outputTarget>
Outputs a SMPTE HD Test pattern with current time, timecode, custom logo
and a 1-second beep with synchronized animation to the destination of
your choice: file or streaming output.
Parameters:
outputFormat: ffmpeg target format, run ffmpeg -formats for the list;
outputTarget: target to send to;
Options:
-d <duration>
--duration <duration>
duration in seconds or per ffmpeg format
https://ffmpeg.org/ffmpeg-utils.html#time-duration-syntax),
output will be paced in realtime if duration is not
supplied
-h <header>
--header <header>
Header text to display, defaults to hostname
-f <framerate>
--framerate <framerate>
Framerate to use, default is 60000/1001 == 59.94
-l <filename>
--logo <filename>
logo file to use, expected ~135 px wide
-p <profilename>
--profile <profilename>
set the encoding profile to use. Default is "testpattern".
Run hdbars -profiles for the full list
-profiles
List the available encoding profiles
Examples:
hdbars -h Hello! -d 10 mp4 hello.mp4
hdbars -h "29.97 Test" -f 30000/1001 flv rtmp://twitch.com/mychannel
hdbars
EOF
exit 1
}
# Parse the command line params
REALTIME="-re"
LINE_1=`hostname -s`
FRAME_RATE=25000/1001
DROPFRAME=":"
ENC_PROFILE="testpattern"
while [ ${1:0:1} == "-" ]
do
case $1 in
-d|--duration)
REALTIME=""
DURATION="duration=$2:"
TOTAL_DURATION="-t $2"
shift 2
;;
-l|--logo)
if [ ! -f "$2" ]; then
>&2 echo "File '$2' does not exist"
exit 1
fi
LOGO_SOURCE="$2"
shift 2
;;
-h|--header)
LINE_1=$2
shift 2
;;
-f|--framerate)
FRAME_RATE=$2
# fix the timecode separator for whole framerates
[[ "$FRAME_RATE" =~ ^[0-9]+$ ]] && DROPFRAME=":"
shift 2
;;
-p|--profile)
ENC_PROFILE=$2
shift 2
;;
-profiles)
$(dirname "$0")/enc_profile --list
exit 0;
;;
*)
help_and_bail
;;
esac
done
[ "$#" -lt 2 ] && help_and_bail;
OUTPUT_FORMAT=$1
TARGET=$2
# Various Geometry Things
SW=1920
SH=1080
BOX_W=540
BOX_H=178
SHUTTLE_W=45
SHUTTLE_H=10
BOX_X=(${SW}-$BOX_W)/2
BOX_Y=(${SH}-$BOX_H)/3
MARGIN_X=20
MARGIN_Y=20
LOGO_W=135
# Text Related stuff
LINE_H=44
FONT_SIZE=32
TEXT_OFFSET=10
FONT=/usr/share/fonts/dejavu/DejaVuSansMono-Bold.ttf
TIME_FORMAT="%b %d %X" # see https://strftime.org/
# Logo - we'll use internal ffmpeg pattern if no PNG file was supplied
if [ "$LOGO_SOURCE" == "" ]; then
LOGO_SOURCE="-f lavfi -i smptebars=${DURATION}size=${LOGO_W}x$(( LOGO_W * 3 / 4 )):rate=$FRAME_RATE"
else
LOGO_SOURCE="-framerate $FRAME_RATE -i $LOGO_SOURCE"
fi
# Run FFMPEG
ffmpeg \
$REALTIME \
-f lavfi -i "smptehdbars=${DURATION}size=${SW}x${SH}:rate=$FRAME_RATE" \
-f lavfi -i "sine=${DURATION}frequency=220:beep_factor=8:sample_rate=44100" \
$LOGO_SOURCE \
-f lavfi -i "color=r=$FRAME_RATE:c=#[email protected]:s=${SHUTTLE_W}x${SHUTTLE_H},format=rgba" \
-filter_complex " \
drawbox=c=#[email protected]:w=${BOX_W}:h=${BOX_H}:x=$BOX_X:y=$BOX_Y:t=fill [bg]; \
[bg][2] overlay=x=$BOX_X+$MARGIN_X:y=$BOX_Y+$MARGIN_Y [bglogo]; \
[bglogo][3] overlay=x=$BOX_X+ if(mod(floor(t)\,2)\,(ceil(t)-t)\,1-(ceil(t)-t)) *($BOX_W-$SHUTTLE_W):y=$BOX_H+$BOX_Y-$SHUTTLE_H, \
drawbox=enable=lte(t-floor(t)\,0.025)+gte(t-floor(t)\,0.975):c=#FFFFFFCC:t=fill:x=$BOX_X:y=$BOX_H+$BOX_Y-$SHUTTLE_H:w=${BOX_W}:h=${SHUTTLE_H},
drawtext=text='$LINE_1':rate=$FRAME_RATE:x=$BOX_X+$MARGIN_X*2+$LOGO_W:y=$BOX_Y+$MARGIN_Y+$TEXT_OFFSET:fontfile=$FONT:fontsize=$FONT_SIZE:fontcolor=white:shadowcolor=black:shadowx=0:shadowy=0, \
drawtext=text='%{localtime\:$TIME_FORMAT}':rate=$FRAME_RATE:x=$BOX_X+$MARGIN_X*2+$LOGO_W:y=$BOX_Y+$MARGIN_Y+$TEXT_OFFSET+$LINE_H:fontfile=$FONT:fontsize=$FONT_SIZE:fontcolor=white:shadowcolor=black:shadowx=0:shadowy=0, \
drawtext=timecode='00\:00\:00\\${DROPFRAME}00':rate=$FRAME_RATE:x=$BOX_X+$MARGIN_X*2+$LOGO_W:y=$BOX_Y+$MARGIN_Y+$TEXT_OFFSET+$LINE_H*2:fontfile=$FONT:fontsize=$FONT_SIZE:fontcolor=white:shadowcolor=black:shadowx=0:shadowy=0 \
" \
-y \
-c:v rawvideo \
$TOTAL_DURATION \
-f $OUTPUT_FORMAT \
$TARGET