Skip to content

Instantly share code, notes, and snippets.

@marcoherzog
Forked from BenjaminPoncet/ffmpeg-wrapper
Created February 2, 2020 15:13
Show Gist options
  • Select an option

  • Save marcoherzog/accb9b0fc6e9678e7c402807de0b3ba3 to your computer and use it in GitHub Desktop.

Select an option

Save marcoherzog/accb9b0fc6e9678e7c402807de0b3ba3 to your computer and use it in GitHub Desktop.
Synology VideoStation ffmpeg wrapper with DTS, EAC3 and TrueHD support (Installation instructions in 1st comment)
#!/bin/bash
_log(){
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${streamhash} - $1" >> /tmp/ffmpeg.log
}
_log_para(){
echo "$1" | fold -w 120 | sed "s/^.*$/$(date '+%Y-%m-%d %H:%M:%S') - ${streamhash} - = &/" >> /tmp/ffmpeg.log
}
_term(){
rm /tmp/ffmpeg-${streamhash}.stderr
_log "*** KILLCHILD ***"
kill -TERM "$childpid" 2>/dev/null
}
trap _term SIGTERM
paramvs=$@
args=()
while [[ $# -gt 0 ]]
do
case "$1" in
-i)
shift
movie="$1"
args+=("-i" "$1")
;;
*)
args+=("$1")
stream="$1"
;;
esac
shift
done
streamhash=`echo $stream | cut -d / -f 5 | cut -d _ -f 2`
bin=/var/packages/ffmpeg/target/bin/ffmpeg
if echo $stream | grep "/tmp/SYNOVIDEO_ffmpeg_.*\.srt"; then
streamhash=`echo $stream | cut -d _ -f 3 | sed -e 's/.srt//g'`
bin=/var/packages/VideoStation/target/bin/ffmpeg.orig
fi
_log "*** PROCESS START ***"
_log "MOVIE = $movie"
_log "FFMPEG = $bin"
_log_para "$paramvs"
set -- "${args[@]}"
startexectime=`date +%s`
$bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
childpid=$!
_log "CHILDPID = $childpid"
wait $childpid
stopexectime=`date +%s`
if test $((stopexectime-startexectime)) -lt 3; then
_log "STDERR ="
_log_para "`tail -n 10 /tmp/ffmpeg-${streamhash}.stderr`"
fi
_log "*** CHILD END ***"
if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
_log "*** RW MODE ***"
args=()
while [[ $# -gt 0 ]]
do
case "$1" in
-vf)
shift
scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
if let ${scale_w} AND let ${scale_h}; then
args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12,hwupload,setsar=sar=1")
else
args+=("-vf" "scale_vaapi=format=nv12,hwupload,setsar=sar=1")
fi
;;
*)
args+=("$1")
;;
esac
shift
done
set -- "${args[@]}"
paramrw=$@
_log "FFMPEG = $bin"
_log_para "$paramrw"
startexectime=`date +%s`
$bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
childpid=$!
_log "CHILDPID = $childpid"
wait $childpid
stopexectime=`date +%s`
if test $((stopexectime-startexectime)) -lt 3; then
_log "STDERR ="
_log_para "`tail -n 10 /tmp/ffmpeg-${streamhash}.stderr`"
fi
_log "*** CHILD END ***"
fi
_log "*** PROCESS END ***"
rm /tmp/ffmpeg-${streamhash}.stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment