forked from mediamicroservices/mm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakebroadcast
executable file
·243 lines (228 loc) · 10.1 KB
/
makebroadcast
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
# makebroadcast
# v 1.1 change from mov output to mxf output, force all output to hd
# v 1.2 revert to mov and ffmbc
# v 1.3 revert back to ffmpeg
# v 1.4 adjust handling volume adjustment argument
version="1.4"
VOLADJUST="Y"
scriptdir=$(dirname $(which "${0}"))
. "${scriptdir}/mmfunctions" || { echo "Missing '${scriptdir}/mmfunctions'. Exiting." ; exit 1 ;};
dependencies=(ffmpeg ffprobe)
_initialize_make
suffix=""
extension="mov"
relativepath="service"
_usage(){
echo
echo "$(basename "${0}") ${version}"
echo "This application will create a file suitable for broadcast or editing from a video file or package input with the following options."
echo "Dependencies: ${dependencies[@]}"
echo "Usage: $(basename "${0}") [ -l ] [ -r ] [ -c ] fileorpackage1 [ fileorpackage2 ...]"
echo " -F (apply a formula, override default settings)"
echo " -l (only use the left channel of the first audio track)"
echo " -r (only use the right channel of the first audio track)"
echo " -c (run the video through a crop detection filter, then crop the video accordingly during transcoding, slower)"
echo " -v Y|N (run the video through a volume adjustment filter, then adjust the volume accordingly during transcoding, slower). Defaults to ${VOLADJUST}."
echo " -d directory ( directory to deliver the resulting file to )"
echo " -o directory ( directory to write the resulting file to )"
echo " -n (dry-run mode, show the commands that would be run but don't do anything)"
echo " -e emailaddress ( send an email about the delivery, only valid if -d is used )"
echo " -E emailaddress ( send an email about process outcome )"
echo " -h ( display this help )"
echo
exit
}
[ "${#}" = 0 ] && _usage
# command-line options to set mediaid and original variables
OPTIND=1
while getopts ":F:lrcv:o:d:e:E:nh" opt ; do
case "${opt}" in
F) formula="${OPTARG}";;
l) audiomap="L";;
r) audiomap="R";;
c) cropdetect="Y" ;;
v) VOLADJUST="${OPTARG}";;
o) outputdir_forced="$OPTARG" && _check_outputdir_forced ;;
d) deliverdir="$OPTARG" && check_deliverdir ;;
e) emailaddress_delivery="$OPTARG" && check_emailaddress "${emailaddress_delivery}" ;;
E) emailaddress_outcome="$OPTARG" && check_emailaddress "${emailaddress_outcome}" ;;
n) DRYRUN=true;;
h) _usage ;;
*) echo "bad option -$OPTARG" ; _usage ;;
:) echo "Option -$OPTARG requires an argument" ; exit 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
while [ "${*}" != "" ] ; do
# get context about the input
input="${1}"
shift
if [ -z "${outputdir_forced}" ] ; then
[ -d "${input}" ] && { outputdir="${input}/objects/${relativepath}" && logdir="${input}/metadata/submissionDocumentation/logs" ;};
[ -f "${input}" ] && { outputdir=$(dirname "${input}")"/${relativepath}" && logdir="$(dirname "${input}")/${relativepath}/logs" ;};
[ ! "${outputdir}" ] && { outputdir="${input}/objects/${relativepath}" && logdir="${input}/metadata/submissionDocumentation/logs" ;};
else
outputdir="${outputdir_forced}"
logdir="${outputdir}/logs"
fi
_find_input "${input}"
mediaid=$(basename "${input}" | cut -d. -f1)
ingestlog="${logdir}/capture.log"
_set_up_output
# clear local arrays
intime=""
outtime=""
audio_adjustment=""
unset inputoptions
unset middleoptions
unset middleoptionsxdcam
unset middleoptionsimx
unset audiomapping_ffmpeg
filterchain_start=""
get_videostreamcount "${sourcefile}"
get_audiostreamcount "${sourcefile}"
if [[ "${VIDEOSTREAMCOUNT}" > 0 ]] ; then
# get information on the input
get_sar "${sourcefile}"
if [ "${cropdetect}" = "Y" ] ; then
get_cropdetection "${sourcefile}"
filterchain_start="${filterchain_start}${CROPADJ},"
width=$(echo "${CROPADJ}" | cut -d= -f2 | cut -d: -f1)
height=$(echo "${CROPADJ}" | cut -d= -f2 | cut -d: -f2)
dar="(${width}/${height})*${sar}"
else
get_width "${sourcefile}"
get_height "${sourcefile}"
get_dar "${sourcefile}"
fi
# set in and out times if used
if [[ -f "${ingestlog}" && "${isobject}" = "Y" ]] ; then
if [ ! ${formula} ] ; then
formula=$(_readingestlog "formula")
fi
intime=$(_readingestlog "intime")
outtime=$(_readingestlog "outtime")
if [[ "${intime}" ]] ; then
report -dt "ATTENTION: Transcoding will use intime (${intime}) during transcoding."
middleoptions+=(-ss "${intime}")
fi
if [[ "${outtime}" ]] ; then
report -dt "ATTENTION: Transcoding will use outtime (${outtime}) during transcoding."
middleoptions+=(-to "${outtime}")
fi
fi
#options for either xdcam or imx
middleoptions+=(-r ntsc)
middleoptions+=(-c:v mpeg2video)
middleoptions+=(-intra_vlc 1)
middleoptions+=(-non_linear_quant 1)
middleoptions+=(-qmin 1)
middleoptions+=(-dc 10)
middleoptions+=(-lmin QP2LAMBDA)
middleoptions+=(-ps 1)
middleoptions+=(-flags +ildct+ilme)
middleoptions+=(-pix_fmt yuv422p)
middleoptions+=(-metadata:s:v:0 timecode=00:00:00.00)
#xdcam options
middleoptionsxdcam+=(-s 1920x1080)
middleoptionsxdcam+=(-vtag xd5b)
middleoptionsxdcam+=(-aspect 16:9)
middleoptionsxdcam+=(-q:v 1)
middleoptionsxdcam+=(-qmax 8)
middleoptionsxdcam+=(-g 15)
middleoptionsxdcam+=(-bf 2)
middleoptionsxdcam+=(-b:v 50M)
middleoptionsxdcam+=(-maxrate:v 50M)
middleoptionsxdcam+=(-minrate:v 50M)
middleoptionsxdcam+=(-bufsize 17825792)
middleoptionsxdcam+=(-rc_init_occupancy 17825792)
# imx options
middleoptionsimx+=(-aspect 4:3)
middleoptionsimx+=(-qmax 12)
middleoptionsimx+=(-g 1)
middleoptionsimx+=(-top 1)
middleoptionsimx+=(-intra)
middleoptionsimx+=(-b:v 30M)
middleoptionsimx+=(-maxrate:v 30M)
middleoptionsimx+=(-minrate:v 30M)
middleoptionsimx+=(-bufsize 1001000)
middleoptionsimx+=(-rc_init_occupancy 1001000)
middleoptionsimx+=(-rc_max_vbv_use 1)
middleoptionsimx+=(-rc_min_vbv_use 1)
middleoptionsimx+=(-rc_buf_aggressivity 0.25)
middleoptionsimx+=(-vbsf imxdump)
middleoptionsimx+=(-tag:v mx3n)
# decide on an output codec, aspect ratio, and padding strategy
darratio=$(echo "scale=3 ; ${dar}" | bc)
if [ "${formula}" = "tff" ] ; then
filterchain_start="${filterchain_start}setfield=tff,"
elif [ "${formula}" = "bff" ] ; then
filterchain_start="${filterchain_start}setfield=bff,"
fi
get_fieldorder "${sourcefile}"
if [ "${fieldorder}" != "tff" ] ; then
filterchain_start="${filterchain_start}fieldorder=tff,"
fi
if [ "${formula}" = "hds" ] ; then
middleoptions+=(-vf "${filterchain_start}scale=size=hd1080:interl=1,format=yuv422p,setsar=1/1,setdar=16/9,fps=fps=ntsc")
middleoptions+=(${middleoptionsxdcam[@]})
elif [ "${darratio}" = "1.777" -a ! "${cropdetect}" = "Y" ] ; then
middleoptions+=(-vf "${filterchain_start}scale=size=hd1080:interl=1,format=yuv422p,setsar=1/1,setdar=16/9,fps=fps=ntsc")
middleoptions+=(${middleoptionsxdcam[@]})
elif [ $(echo "scale=3 ; ${darratio} > (16/9)" | bc -l) = 1 ] ; then
middleoptions+=(-vf "${filterchain_start}pad=${width}:${width}*(9/16)*(${sar}):(ow-${width})/2:(oh-${height})/2,scale=size=hd1080:interl=1")
middleoptions+=(${middleoptionsxdcam[@]})
elif [ $(echo "scale=3 ; $darratio > (3/2)" | bc -l) = 1 ] ; then
middleoptions+=(-vf "${filterchain_start}pad=${height}*(16/9)/(${sar}):${height}:(ow-${width})/2:(oh-${height})/2,scale=size=hd1080:interl=1")
middleoptions+=(${middleoptionsxdcam[@]})
elif [ "$width" = "720" -a "$height" = "480" -a $(echo "scale=3 ; $darratio < (3/2)" | bc -l) = 1 -a ! "$cropdetect" = "Y" ] ; then
middleoptions+=(-vf ${filterchain_start}pad=720:512:0:24,setsar=1/1,setdar=4/3)
middleoptions+=(${middleoptionsimx[@]})
elif [ "${height}" -eq "486" -a "${width}" -eq "720" ] ; then
middleoptions+=(-vf "${filterchain_start}crop=720:480:0:4,pad=720:512:0:24,setsar=1/1,setdar=4/3")
middleoptions+=(${middleoptionsimx[@]})
elif [ "${height}" -eq "512" -a "${width}" -eq "720" ] ;then
middleoptions+=(-vf "${filterchain_start}crop=720:480:0:32,pad=720:512:0:24,setsar=1/1,setdar=4/3")
middleoptions+=(${middleoptionsimx[@]})
else
middleoptions+=(-vf "${filterchain_start}scale=720:480,pad=720:512:0:24,setsar=1/1,setdar=4/3")
middleoptions+=(${middleoptionsimx[@]})
fi
fi
if [[ "${AUDIOSTREAMCOUNT}" > 0 ]] ; then
get_audio_index "${sourcefile}"
if [ "${audiomap}" = "L" ] ; then
audiomapping_ffmpeg=(-map_channel 0.${audio_index_1}.0)
elif [ "${audiomap}" = "R" ] ; then
audiomapping_ffmpeg=(-map_channel 0.${audio_index_1}.1)
else
get_audio_mapping "${sourcefile}"
fi
middleoptions+=(${audiomapping_ffmpeg[@]})
# add audio options
middleoptions+=(-acodec pcm_s24be)
middleoptions+=(-ar 48k)
middleoptions+=(-ac 2)
fi
# set input options for ffmpeg
get_codectagstring "${sourcefile}"
inputoptions+=(-y)
inputoptions+=(-nostdin)
if [[ "${codec_tag_string}" == "mjp2" ]] ; then
inputoptions+=(-vcodec libopenjpeg)
fi
# log process
_prep_ffmpeg_log
_prep_volume_adjustment
if [ "${concatsource}" != "" ] ; then
ffmpeginput="${concatsource}"
else
ffmpeginput="${sourcefile}"
fi
_run_critical ffmpeg ${inputoptions[@]} -i "${ffmpeginput}" ${middleoptions[@]} "${output}"
echo
_summarize_make
_deliver_output "${MAKEBROADCAST_DELIVERY_EMAIL_TO}"
_log -e
done