-
Notifications
You must be signed in to change notification settings - Fork 76
/
ffmpeg41-wrapper-DSM7_X-Simplest
156 lines (131 loc) · 4.33 KB
/
ffmpeg41-wrapper-DSM7_X-Simplest
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
#!/bin/bash
##################################################
# Simplest wrapper. This wrapper works with the Video Station´s Transcoding Profiles and always will be using MP3 2.0 96kbps or 128kbps when transcoding.
# It derives all parameters to the SynoCommunity´s ffmpeg.
rev="AME_S_3.2"
# Changes:
# AME_S_1.0: Initial release of the simplest wrapper. Fork from AlexPresso. (Deprecated, migrate to AME_S_1.1)
# AME_S_1.1: Correcting the log file. (Deprecated, migrate to AME_S_1.2)
# AME_S_1.2: Improved the viewing of logs. (Deprecated, migrate to AME_S_1.3)
# AME_S_1.3: Fixed a bug that doesn't remove the stderr file due a misconfiguration. (Deprecated, migrate to AME_S_2.1)
# AME_S_2.1: Added support for FFMPEG 6.X binaries and deprecate the FFMPEG 4.X. (Deprecated, migrate to AME_S_2.2)
# AME_S_2.2: Added support for GStreamer libraries. (Deprecated, migrate to AME_S_2.3)
# AME_S_2.3: Homogenize the closing of processes with the Advanced Wrapper, to correct a bug carried over from Alex's code. (Deprecated, migrate to AME_S_3.1)
# AME_S_3.1: Added support for FFMPEG 7.X binaries and deprecate the FFMPEG 6.X. (Deprecated, migrate to AME_S_3.2)
# AME_S_3.2: Fixed a bug that doesn't log the actions using the Simplest one. Added few parameters changes to work properly with FFmpeg 7.0.
##################################################
# VARIABLES
WHITE="\u001B[37m"
RED="\u001b[31m"
BLUE="\u001b[36m"
PURPLE="\u001B[35m"
GREEN="\u001b[32m"
YELLOW="\u001b[33m"
pid=$$
childpid=""
streamid="FFM$pid"
stderrfile="/tmp/ffmpeg-${streamid}.stderr"
logfile="/tmp/wrapper_ffmpeg.log"
errcode=0
arch=`uname -a | sed 's/.*synology_//' | cut -d '_' -f 1`
nas=`uname -a | sed 's/.*synology_//' | cut -d '_' -f 2`
bin1=/var/packages/ffmpeg7/target/bin/ffmpeg
args=()
##################################################
# FUNCIONES
function log() {
echo -e "${WHITE}[$(date '+%Y-%m-%d %H:%M:%S')] [$1] $2" >> $logfile
}
function newline() {
echo "" >> $logfile
}
function info() {
log "${BLUE}INFO" "${WHITE}$1"
}
function kill_child() {
if [[ "$childpid" != "" ]]; then
kill "$childpid" > /dev/null 2> /dev/null || :
fi
}
function handle_error() {
log "${RED}ERROR" "${RED}An error occurred, here is the $stderrfile content:${WHITE}"
newline
cat "$stderrfile" >> $logfile
newline
errcode=1
kill_child
}
function _term() {
info "${PURPLE}========================================[END FFmpeg $pid]${WHITE}"
newline
rm -f "$stderrfile"
kill -TERM "$childpid" 2> /dev/null
exit "$errcode"
}
function fix_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-ss)
shift
args+=("-ss" "$1" "-noaccurate_seek")
;;
-vbsf)
shift
args+=("-bsf:v" "$1")
;;
-acodec)
shift
if [[ "$1" = "libfaac" ]]; then
args+=("-acodec" "aac")
else
args+=("-acodec" "libfdk_aac")
fi
;;
-vf)
shift
arg="$1"
if [[ "$arg" =~ "scale_vaapi" ]]; then
scale_w=$(echo "$arg" | sed -n 's/.*w=\([0-9]\+\):h=\([0-9]\+\).*/\1/p')
scale_h=$(echo "$arg" | sed -n 's/.*w=\([0-9]\+\):h=\([0-9]\+\).*/\2/p')
if (( scale_w && scale_h )); then
arg="format=nv12|vaapi,hwupload,scale_vaapi=w=$scale_w:h=$scale_h:format=nv12,setsar=sar=1"
else
arg="format=nv12|vaapi,hwupload,scale_vaapi=format=nv12,setsar=sar=1"
fi
fi
args+=("-vf" "$arg")
;;
-r)
shift
;;
*) args+=("$1") ;;
esac
shift
done
}
#########################
# EJECUCION
trap _term SIGINT SIGTERM
trap handle_error ERR
# export LD_LIBRARY_PATH=/var/packages/VideoStation/target/lib/patch/lib/
# export GST_PLUGIN_PATH=/var/packages/VideoStation/target/lib/patch/plugins/
fix_args "$@"
newline
info "${PURPLE}*** PROCESS START REV $rev DS$nas ($arch) PID $pid ***"
info "${PURPLE}========================================[START FFmpeg $pid]${WHITE}"
info "${BLUE}BINARY = ${WHITE}$bin1${WHITE}"
info "${GREEN}DEFAULT_ARGS: ${WHITE}$*${WHITE}"
info "${GREEN}UPDATED_ARGS: ${WHITE}${args[*]}${WHITE}"
info "${YELLOW}Trying with UPDATED_ARGS.${WHITE}"
$bin1 "${args[@]}" 2> $stderrfile
childpid=$!
wait "$childpid"
if [[ $errcode -eq 0 ]]; then
_term
fi
errcode=0
info "${YELLOW}Trying with DEFAULT_ARGS.${WHITE}"
$bin1 "$@" 2> $stderrfile
childpid=$!
wait "$childpid"
_term