From 07b6fa06092e3d0be94383480da65c84f8fddd9b Mon Sep 17 00:00:00 2001 From: ruzko <75946143+ruzko@users.noreply.github.com> Date: Mon, 23 May 2022 13:32:36 +0200 Subject: [PATCH 1/2] enable parallellized post-processing This significantly speeds up post-processing. Some logic is provided to determine which Pi model the script is running on, and number of parallell processes are limited accordingly --- tools/raw2ogg2anim | 84 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/tools/raw2ogg2anim b/tools/raw2ogg2anim index 011efac..70fc731 100755 --- a/tools/raw2ogg2anim +++ b/tools/raw2ogg2anim @@ -2,57 +2,99 @@ # needs: dcraw, double, netpbm tools, gstreamer, gifenc.sh + +#Determining the model of Raspberry Pi we're running on +if [ "`lscpu | grep 'Model' | grep -o 'A72'`" = "A72" ]; then +num_processes=40 +echo "Model 4, running 40 parallell processes" + +elif [ "`lscpu | grep 'Model' | grep -o 'A53'`" = "A53" ]; then +num_processes=20 +echo "Model 3, running 20 parallell processes" + + +else +num_processes=10 +echo "couldn't determine model, running 10 parallell processes" +fi + + +#converts raw frames to png format +dcraw_conversion() { + dcraw $f + echo -en "$f \r" +} + +#stretches lines so that videos recorded with line skips look better +line_doubling() { + if [ "$5" = "" ]; then + ln -s $f $f.d + else + if [ "$5" = "d" ]; then + double $f > $f.d + else + double $f > $f.D + double $f.D > $f.d + fi + fi + echo -en "$f \r" +} + +#converts from intermediary format ppm to final image format png +ppmtopng_conversion() { + pnmtopng $f > $f.png + echo -en "$f \r" +} + + +#Beginning of legacy script if [ "$4" = "" ]; then echo "format: `basename $0` vname first last fps [d[d]]"; exit; fi echo "removing old auxiliary files" rm -f out.*.raw out.*.ppm out.*.ppm.[dDT] out.*.ppm.d.png + +# this appears to be slower when parallellized, so it's a single process for the time being. echo "copying /dev/shm/out.????.raw files" for((f=$2; f<=$3; ++f)) do - # cp /dev/shm/out.$(printf "%04d" $f).raw . - cat hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw +cat /dev/shm/hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw echo -en "$f \r" done echo + echo "dcraw each .raw file (to .ppm)" for f in out.*.raw do - dcraw $f - echo -en "$f \r" + ((i=i%num_processes)); ((i++==0)) && wait + dcraw_conversion & done -echo +wait echo ".ppm -> .ppm.d" for f in out.*.ppm do - if [ "$5" = "" ]; then - ln -s $f $f.d - else - if [ "$5" = "d" ]; then - double $f > $f.d - else - double $f > $f.D - double $f.D > $f.d - fi - fi - echo -en "$f \r" + ((i=i%num_processes)); ((i++==0)) && wait + line_doubling & done -echo +wait echo ".ppm.d -> .ppm.d.png" for f in out.*.ppm.d do - pnmtopng $f > $f.png - echo -en "$f \r" + ((i=i%num_processes)); ((i++==0)) && wait + ppmtopng_conversion & done -echo +wait echo "now creating $1.ogg" gst-launch-1.0 multifilesrc location="out.%04d.ppm.d.png" index=$2 caps="image/png,framerate=\(fraction\)$4/1" ! pngdec ! videorate ! videoconvert ! videorate ! theoraenc ! oggmux ! filesink location="$1.ogg" echo "now creating $1.anim.gif" -gifenc.sh $1.ogg $1.anim.gif +gifenc.sh $1.ogg $1.gif + +echo "removing new auxiliary files" +rm -f out.* echo "done" From 1e9d1ce55ae17ad85e0d2dbf39371d025cf5aa64 Mon Sep 17 00:00:00 2001 From: ruzko <75946143+ruzko@users.noreply.github.com> Date: Tue, 24 May 2022 11:49:49 +0200 Subject: [PATCH 2/2] better readability and consistent formatting Added two line breaks between each code block, reworded comments and changed indentation. Also, since it was a bit annoying to have a long file extension when working with hundreds of GIFs, I removed the ".anim" part. I couldn't find any mention of GIFs having ".anim" file extension in other sources (e.g. https://en.wikipedia.org/wiki/GIF), so I think this is more according to spec as well. It might break some users' workflow, though. --- tools/raw2ogg2anim | 49 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/tools/raw2ogg2anim b/tools/raw2ogg2anim index 70fc731..5ce7471 100755 --- a/tools/raw2ogg2anim +++ b/tools/raw2ogg2anim @@ -3,29 +3,30 @@ # needs: dcraw, double, netpbm tools, gstreamer, gifenc.sh -#Determining the model of Raspberry Pi we're running on +#Determining the model of Raspberry Pi we're running on, to set an appropriate number of parallell processes if [ "`lscpu | grep 'Model' | grep -o 'A72'`" = "A72" ]; then -num_processes=40 -echo "Model 4, running 40 parallell processes" - -elif [ "`lscpu | grep 'Model' | grep -o 'A53'`" = "A53" ]; then -num_processes=20 -echo "Model 3, running 20 parallell processes" - - -else -num_processes=10 -echo "couldn't determine model, running 10 parallell processes" + num_processes=40 + echo "Model 4, running 40 parallell processes" +else + if [ "`lscpu | grep 'Model' | grep -o 'A53'`" = "A53" ]; then + num_processes=20 + echo "Model 3, running 20 parallell processes" + else + num_processes=10 + echo "couldn't determine model, running 10 parallell processes" + fi fi -#converts raw frames to png format +#The following three code blocks define functions to be called later +#1. converts raw frames to png format dcraw_conversion() { dcraw $f echo -en "$f \r" } -#stretches lines so that videos recorded with line skips look better + +#2. stretches lines so that videos recorded with line skips look better line_doubling() { if [ "$5" = "" ]; then ln -s $f $f.d @@ -40,28 +41,29 @@ line_doubling() { echo -en "$f \r" } -#converts from intermediary format ppm to final image format png + +#3. converts from intermediary format ppm to final image format png ppmtopng_conversion() { pnmtopng $f > $f.png echo -en "$f \r" } -#Beginning of legacy script +#Taking user inputs if [ "$4" = "" ]; then echo "format: `basename $0` vname first last fps [d[d]]"; exit; fi + echo "removing old auxiliary files" rm -f out.*.raw out.*.ppm out.*.ppm.[dDT] out.*.ppm.d.png -# this appears to be slower when parallellized, so it's a single process for the time being. +# this command appears to be slower when parallellized, so it's a single process for the time being. echo "copying /dev/shm/out.????.raw files" for((f=$2; f<=$3; ++f)) do -cat /dev/shm/hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw + cat /dev/shm/hd0.32k /dev/shm/out.$(printf "%04d" $f).raw >out.$(printf "%04d" $f).raw echo -en "$f \r" done -echo echo "dcraw each .raw file (to .ppm)" @@ -72,6 +74,7 @@ do done wait + echo ".ppm -> .ppm.d" for f in out.*.ppm do @@ -80,6 +83,7 @@ do done wait + echo ".ppm.d -> .ppm.d.png" for f in out.*.ppm.d do @@ -88,13 +92,18 @@ do done wait + echo "now creating $1.ogg" gst-launch-1.0 multifilesrc location="out.%04d.ppm.d.png" index=$2 caps="image/png,framerate=\(fraction\)$4/1" ! pngdec ! videorate ! videoconvert ! videorate ! theoraenc ! oggmux ! filesink location="$1.ogg" -echo "now creating $1.anim.gif" + +echo "now creating $1.gif" gifenc.sh $1.ogg $1.gif + +#comment the following two line if you want to keep individual image frames and other files in the current directory echo "removing new auxiliary files" rm -f out.* + echo "done"