Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# TEST VERSION PIPE CONCEPT
rev="8"
_log(){
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${streamid} - $1" >> /tmp/ffmpeg.log
}
_log_para(){
echo "$1" | fold -w 120 | sed "s/^.*$/$(date '+%Y-%m-%d %H:%M:%S') - ${streamid} - = &/" >> /tmp/ffmpeg.log
}
_term(){
rm /tmp/ffmpeg-${streamid}.stderr
_log "*** KILLCHILD ***"
kill -TERM "$childpid" 2>/dev/null
}
trap _term SIGTERM
arch=`uname -a | sed 's/.*synology_//' | cut -d '_' -f 1`
nas=`uname -a | sed 's/.*synology_//' | cut -d '_' -f 2`
pid=$$
paramvs=$@
stream="${@: -1}"
streamid="FFM$pid"
bin1=/var/packages/ffmpeg/target/bin/ffmpeg
bin2=/var/packages/VideoStation/target/bin/ffmpeg.orig
args=()
vcodec="KO"
while [[ $# -gt 0 ]]
do
case "$1" in
-i)
shift
movie="$1"
args+=("-i" "$1")
;;
-scodec)
shift
scodec="$1"
args+=("-scodec" "$1")
;;
-f)
shift
fcodec="$1"
args+=("-f" "$1")
;;
-map)
shift
args+=("-map" "$1")
idmap=`echo $1 | cut -d : -f 2`
if [ "$vcodec" = "KO" ]; then
vcodec=`/var/packages/ffmpeg/target/bin/ffprobe -v error -select_streams $idmap -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$movie"`
vcodecprofile=`/var/packages/ffmpeg/target/bin/ffprobe -v error -select_streams $idmap -show_entries stream=profile -of default=noprint_wrappers=1:nokey=1 "$movie"`
else
acodec=`/var/packages/ffmpeg/target/bin/ffprobe -v error -select_streams $idmap -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$movie"`
fi
;;
*)
args+=("$1")
;;
esac
shift
done
_log "*** PROCESS START REV $rev DS$nas ($arch) PID $pid ***"
_log "MOVIE = $movie"
set -- "${args[@]}"
args1sv=()
args2sv=()
args1vs=()
args2vs=()
while [[ $# -gt 0 ]]
do
case "$1" in
-ss)
shift
args1sv+=("-ss" "$1")
args1sv+=("-noaccurate_seek")
args1vs+=("-ss" "$1")
args1vs+=("-noaccurate_seek")
args2sv+=("-analyzeduration" "10000000")
args2vs+=("-analyzeduration" "10000000")
;;
-i)
shift
args1sv+=("-i" "$1")
args2sv+=("-i" "pipe:0" "-map" "0")
args1vs+=("-i" "$1")
args2vs+=("-i" "pipe:0" "-map" "0")
;;
-vcodec)
shift
args1sv+=("-vcodec" "copy")
args2sv+=("-vcodec" "$1")
args1vs+=("-vcodec" "$1")
args2vs+=("-vcodec" "copy")
;;
-acodec)
shift
if [ "$1" = "libfaac" ]; then
args1sv+=("-acodec" "aac")
args2vs+=("-acodec" "aac")
else
args1sv+=("-acodec" "$1")
args2vs+=("-acodec" "$1")
fi
args2sv+=("-acodec" "copy")
args1vs+=("-acodec" "copy")
;;
-ab)
shift
args1sv+=("-ab" "$1")
args2vs+=("-ab" "$1")
;;
-ac)
shift
args1sv+=("-ac" "$1")
args2vs+=("-ac" "$1")
;;
-f)
shift
args1sv+=("-f" "mpegts")
args2sv+=("-f" "$1")
args1vs+=("-f" "mpegts")
args2vs+=("-f" "$1")
;;
-segment_format)
shift
args2vs+=("-segment_format" "$1")
args2sv+=("-segment_format" "$1")
;;
-segment_list_type)
shift
args2vs+=("-segment_list_type" "$1")
args2sv+=("-segment_list_type" "$1")
;;
-hls_seek_time)
shift
args2vs+=("-hls_seek_time" "$1")
args2sv+=("-hls_seek_time" "$1")
;;
-segment_time)
shift
args2vs+=("-segment_time" "$1")
args2sv+=("-segment_time" "$1")
;;
-segment_time_delta)
shift
args2vs+=("-segment_time_delta" "$1")
args2sv+=("-segment_time_delta" "$1")
;;
-segment_start_number)
shift
args2vs+=("-segment_start_number" "$1")
args2sv+=("-segment_start_number" "$1")
;;
-individual_header_trailer)
shift
args2vs+=("-individual_header_trailer" "$1")
args2sv+=("-individual_header_trailer" "$1")
;;
-avoid_negative_ts)
shift
args2vs+=("-avoid_negative_ts" "$1")
args2sv+=("-avoid_negative_ts" "$1")
;;
-break_non_keyframes)
shift
args2vs+=("-break_non_keyframes" "$1")
args2sv+=("-break_non_keyframes" "$1")
;;
-max_muxing_queue_size)
shift
args2vs+=("-max_muxing_queue_size" "$1")
args2sv+=("-max_muxing_queue_size" "$1")
;;
-map)
shift
args1sv+=("-map" "$1")
args1vs+=("-map" "$1")
;;
*)
if [ "$stream" = "$1" ]; then
args1sv+=("-bufsize" "1024k" "pipe:1")
args2sv+=("$1")
args1vs+=("-bufsize" "1024k" "pipe:1")
args2vs+=("$1")
else
args2sv+=("$1")
args1vs+=("$1")
fi
;;
esac
shift
done
sed -i -e "s/{\"PID\":${pid},\"hardware_transcode\":true,/{\"PID\":${pid},\"hardware_transcode\":false,/" /tmp/VideoStation/enabled
startexectime=`date +%s`
if [ "$scodec" = "subrip" ]; then
_log "FFMPEG = $bin2"
_log "CODEC = $scodec"
_log "PARAMVS ="
_log_para "$paramvs"
$bin2 "${args[@]}" 2> /tmp/ffmpeg-${streamid}.stderr &
elif [ "$fcodec" = "mjpeg" ]; then
_log "FFMPEG = $bin2"
_log "CODEC = $fcodec"
_log "PARAMVS ="
_log_para "$paramvs"
$bin2 "${args[@]}" 2> /tmp/ffmpeg-${streamid}.stderr &
else
_log "VCODEC = $vcodec ($vcodecprofile)"
_log "ACODEC = $acodec"
_log "PARAMVS ="
_log_para "$paramvs"
_log "MODE = SV"
_log "FFMPEG1 = $bin1"
_log "FFMPEG2 = $bin2"
_log "PARAM1 ="
param1=${args1sv[@]}
_log_para "$param1"
_log "PARAM2 ="
param2=${args2sv[@]}
_log_para "$param2"
$bin1 "${args1sv[@]}" | $bin2 "${args2sv[@]}" 2> /tmp/ffmpeg-${streamid}.stderr &
fi
childpid=$!
_log "CHILDPID = $childpid"
wait $childpid
if grep "Conversion failed!" /tmp/ffmpeg-${streamid}.stderr || grep "Error opening filters!" /tmp/ffmpeg-${streamid}.stderr || grep "Unrecognized option" /tmp/ffmpeg-${streamid}.stderr || grep "Invalid data found when processing input" /tmp/ffmpeg-${streamid}.stderr; then
_log "*** CHILD END ***"
startexectime=`date +%s`
_log "STDOUT ="
#_log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
_log_para "`cat /tmp/ffmpeg-${streamid}.stderr`"
_log "MODE = VS"
_log "FFMPEG1 = $bin2"
_log "FFMPEG2 = $bin1"
_log "PARAM1 ="
param1=${args1vs[@]}
_log_para "$param1"
_log "PARAM2 ="
param2=${args2vs[@]}
_log_para "$param2"
$bin2 "${args1vs[@]}" | $bin1 "${args2vs[@]}" 2> /tmp/ffmpeg-${streamid}.stderr &
childpid=$!
_log "CHILDPID = $childpid"
wait $childpid
fi
if grep "Conversion failed!" /tmp/ffmpeg-${streamid}.stderr || grep "Error opening filters!" /tmp/ffmpeg-${streamid}.stderr || grep "Unrecognized option" /tmp/ffmpeg-${streamid}.stderr || grep "Invalid data found when processing input" /tmp/ffmpeg-${streamid}.stderr; then
_log "*** CHILD END ***"
startexectime=`date +%s`
_log "STDOUT ="
#_log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
_log_para "`cat /tmp/ffmpeg-${streamid}.stderr`"
_log "MODE = ORIG"
_log "FFMPEG = $bin2"
$bin2 "${args[@]}" 2> /tmp/ffmpeg-${streamid}.stderr &
childpid=$!
_log "CHILDPID = $childpid"
wait $childpid
fi
stopexectime=`date +%s`
if test $((stopexectime-startexectime)) -lt 10; then
_log "STDOUT ="
#_log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
_log_para "`cat /tmp/ffmpeg-${streamid}.stderr`"
fi
_log "*** CHILD END ***"
_log "*** PROCESS END ***"
rm /tmp/ffmpeg-${streamid}.stderr
@th0ma7

This comment has been minimized.

Copy link

@th0ma7 th0ma7 commented Jan 30, 2020

@BenjaminPoncet,
Having a proposal for you: what if we where able to provide your various scripts on a cpu arch basis so it automagically installs all is needed and do all the required changes?

@ymartin59 had started a proof of concept here on which I had added a few thoughts and changes a month or two ago:

I was just thinking that all your scripts could be installed as a "package" and, based on user experience comments you have been receiving it could:
a) make all the default necessary adjustments
b) install / symlink the appropriate script version (pipe, qsv or and default) based on arch

I could do the work on the packaging side work with your help to adjust your repo so it can be fetched easily and associate various platforms where it makes the most sense.

Thoughts?

@BenjaminPoncet

This comment has been minimized.

Copy link
Owner Author

@BenjaminPoncet BenjaminPoncet commented Feb 1, 2020

@th0ma7,

Sure, making a package that would install the wrapper on its own could be really cool, but will Synology appreciate distributing a package that is nothing more or less than a VideoStation hack?

However, for me, it's still a bit early. The special versions of the wrapper are actually branches intended to join the default wrapper as options that I'd like to enable automatically according to the arch.

The second big topic is ARM NAS which uses Gstreamer for video transcoding. I'm going to release a hack this weekend but it won't be able to be integrated in a package. I'm still working on a wrapper but it's not working very well so far.

I had well identified the pull request of @ymartin59 we can continue these discussions on it.

@namzu

This comment has been minimized.

Copy link

@namzu namzu commented May 11, 2021

It seems no working any more.
On DSM 6.2.4 and VideoStation 2.4.10.

@ymartin59

This comment has been minimized.

Copy link

@ymartin59 ymartin59 commented May 14, 2021

@BenjaminPoncet SynoCommunity packages are distributed by our own means... Synology may sue us for license agreement breakage if any IP owner begins to request for stopping such usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment