-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranscode_f47.sh
executable file
·207 lines (185 loc) · 7.12 KB
/
transcode_f47.sh
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
#!/bin/bash
#-----------------------------------------------------------#
# This script is a lossless transcode worker which expects #
# to be invoked by a `watchmedo` "created" event. #
# #
# It compares aggregate checksums of ffmpeg's `-f framemd5` #
# outputs of both source and destination to ensure that #
# the FFV1 Matroska transcode is lossless. #
# #
# Bad transcodes are deleted, and their sources moved. #
# #
# Test usage: #
# ./transcode.sh /path/to/file.mov created #
# #
# Watchdog: #
# See: https://github.com/gorakhargosh/watchdog #
#-----------------------------------------------------------#
# Global variables
OUTPUT="${QNAP_VID}/processing/source"
SUCCESS="${QNAP_08}/processing/transcode/original"
ERROR="${QNAP_08}/processing/transcode/error"
# Receive args at launch
INPUT="$1"
EVENT="$2"
# Log function
function log {
timestamp=$(date "+%Y-%m-%d - %H.%M.%S")
echo "$1"
echo -e "$timestamp\t$INPUT\t$1" >> "${LOG_PATH}f47_transcode.log"
}
# Function to check for control json activity
function control {
boole=$(cat "${LOG_PATH}downtime_control.json" | grep "ofcom_transcode" | awk -F': ' '{print $2}')
if [ "$boole" = false, ] ; then
log "Control json requests script exit immediately"
exit 0
fi
}
# Start script
if [ "$EVENT" = "created" ]; then
log "Start"
# Control check
control
# Only process audio-visual content, but allow application/octet-stream
MIME=$(file --mime-type -b "$INPUT")
TYPE="${MIME%%/*}"
if [[ "$TYPE" != "video" && "$TYPE" != "audio" && "$TYPE" != "application" ]]; then
# Move non-compliant media to error/
#mv "$INPUT" "${ERROR}/"
log "Invalid MIME type"
exit 1
fi
# Only process ffprobe-able content
if ffprobe "$INPUT" 2> /dev/null; then
# OK
:
else
# Do not process non-audio-visual file
log "Unable to ffprobe"
exit 1
fi
# Wrangle filename for output
FRAMEMD5_PATH="${LOG_PATH}framemd5"
BASE=$(basename "$INPUT" | cut -d. -f1)
TMP="${OUTPUT}/partial.${BASE}.mkv"
DST="${OUTPUT}/${BASE}.mkv"
FRAMEMD5_MOV="${FRAMEMD5_PATH}/${BASE}.mov.framemd5"
FRAMEMD5_MKV="${FRAMEMD5_PATH}/${BASE}.mkv.framemd5"
# Check that temporary output does not already exist,
# to permit other instances of this script to process
# media from the same path simultaneously
if [ -e "$TMP" ]; then
log "Transcoding already in progress on this file"
log "End"
exit 0
fi
# Check that destination file does not already exist
if [ -e "$DST" ]; then
# A fixity-checked transcode already exists, quit
log "Transcode already exists"
log "End"
exit 0
fi
# Create MD5 checksum of all FrameMD5s for source media
# First check whether frameMD5 for MOV exists, delete it if it does
if [ -e "$FRAMEMD5_MOV" ]; then
# MOV frameMD5 already exists so deleting it
log "MOV frameMD5 already exists, deleting it"
rm "$FRAMEMD5_MOV"
log "Generate source MOV framemd5"
ffmpeg -nostdin -i "$INPUT" -f framemd5 "$FRAMEMD5_MOV"
else
# MOV frameMD5 does not exist, so create it
log "Generate source mov framemd5"
ffmpeg -nostdin -i "$INPUT" -f framemd5 "$FRAMEMD5_MOV"
fi
# Transcode source to FFV1 Matroska
log "Begin transcode to MKV"
# Test for PAL or NTSC source and modify ffmpeg colour metadata parameters based on result
INPUT_STANDARD=$(mediainfo --Inform="Video;%Standard%" "$INPUT")
HEIGHT=$(mediainfo --Inform='Video;%Height%' "$INPUT")
if [ "$INPUT_STANDARD" = PAL ] || [ "$HEIGHT" = 608 ] ; then
log "Add PAL colour metadata"
ffmpeg -i "$INPUT" \
-ignore_editlist 1 \
-sn \
-nostdin \
-c:v ffv1 \
-g 1 \
-level 3 \
-c:a copy \
-map 0 \
-dn \
-slicecrc 1 \
-slices 24 \
-color_primaries bt470bg \
-color_trc bt709 \
-colorspace bt470bg \
-color_range 1 \
-n \
"$TMP"
else
log "Add NTSC colour metadata"
ffmpeg -i "$INPUT" \
-ignore_editlist 1 \
-sn \
-nostdin \
-c:v ffv1 \
-g 1 \
-level 3 \
-c:a copy \
-map 0 \
-dn \
-slicecrc 1 \
-slices 24 \
-color_primaries bt709 \
-color_trc bt709 \
-colorspace bt709 \
-color_range 1 \
-n \
"$TMP"
fi
log "Finish transcode to mkv"
# Calculate filesize of MKV to ensure no zero byte MKVs persist
OUTPUT_FILESIZE=$(stat --format=%s "$TMP")
log "Output mkv filesize is ${OUTPUT_FILESIZE} bytes"
# First check whether frameMD5 for MKV exists, delete it if it does
if [ -e "$FRAMEMD5_MKV" ]; then
# MKV frameMD5 already exists so deleting it
log "MKV frameMD5 already exists, deleting it"
rm "$FRAMEMD5_MKV"
log "Generate source MKV framemd5"
ffmpeg -nostdin -i "${TMP}" -f framemd5 "$FRAMEMD5_MKV"
else
# MKV frameMD5 does not exist, so create it
log "Generate source mkv framemd5"
ffmpeg -nostdin -i "${TMP}" -f framemd5 "$FRAMEMD5_MKV"
fi
if diff "$FRAMEMD5_MOV" "$FRAMEMD5_MKV" > /dev/null; then
# MKV filesize > 0 and frameMD5s match
STATUS="successful transcode"
log "FrameMD5s match and MKV is not zero bytes (size = ${OUTPUT_FILESIZE})"
log "Add to transcode_success list, to remove temporary partial. prefix from mkv filename at ${TMP}"
echo "mv ${TMP} ${DST}" >> "${LOG_PATH}f47_transcode_success.txt"
log "Move source mov to transcode/original folder"
mv -vn "$INPUT" "${SUCCESS}/"
log "Move mov and mkv frameMD5s to pass folder"
mv -vn "$FRAMEMD5_MOV" "${FRAMEMD5_PATH}/pass/"
mv -vn "$FRAMEMD5_MKV" "${FRAMEMD5_PATH}/pass/"
else
# FrameMD5s do not match
STATUS="failure - frameMD5 mismatch"
log "FrameMD5s do not match"
log "Remove invalid mkv"
rm -vf "$TMP"
log "Move source mov to transcode/error folder"
mv -vn "$INPUT" "${ERROR}/"
log "Move both frameMD5s to fail folder"
mv -vn "$FRAMEMD5_MKV" "${FRAMEMD5_PATH}/fail/"
mv -vn "$FRAMEMD5_MOV" "${FRAMEMD5_PATH}/fail/"
fi
log "End of transcode process - status: ${STATUS}"
fi
# Use transcode_success list to rename the MKV files, removing the temporary partial. prefix from filename
parallel < "${LOG_PATH}f47_transcode_success.txt"