Skip to content

Instantly share code, notes, and snippets.

@eduardomozart
Last active August 5, 2022 13:12
Show Gist options
  • Save eduardomozart/586ba26ce52f9adc44a5d8c6a26a3061 to your computer and use it in GitHub Desktop.
Save eduardomozart/586ba26ce52f9adc44a5d8c6a26a3061 to your computer and use it in GitHub Desktop.
Ruffle Exporter - Create thumbnail from SWF files
#!/bin/bash
# Ruffle-Exporter ver. 0.0.2
# Requires ''ruffle'', ''imagemagick'', ''zenity''
REDEBUG_LOG="/tmp/ruffle-export.dbg"
#REDEBUG_LOG="/dev/null"
#F1=$HOME/.thumbnails
#F2=$HOME/.cache/thumbnails
#SAVE_FOLDER=$F1
#[ -e $F2 ] && SAVE_FOLDER=$F2
ruffle_thumbnailer() {
fname=$(dde_search_prefix "$1")
fname=$(readlink -f "${fname}")
echo "fname: ${fname}" >> $REDEBUG_LOG
dirname=$(dirname "$fname")
filename=$(basename "$fname")
echo "dirname: ${dirname}" >> $REDEBUG_LOG
echo "filename: ${filename}" >> $REDEBUG_LOG
# https://askubuntu.com/questions/199110/how-can-i-instruct-nautilus-to-pre-generate-thumbnails
# https://unix.stackexchange.com/questions/91325/how-to-compute-a-thumbnail-filename-from-the-shell
# https://ubuntuforums.org/archive/index.php/t-666742.html
# https://github.com/difference-engine/thumbnail-generator-ubuntu/blob/master/thumbgen/thumbgen.py
# Generate a thumbnail at major distros
# outname=`python -c "from gi.repository import Gio, GnomeDesktop, GdkPixbuf; import os.path; factory = GnomeDesktop.DesktopThumbnailFactory(); mtime = os.path.getmtime('${fname}'); f = Gio.file_new_for_path(str('${fname}')); uri = f.get_uri(); pixbuf = GdkPixbuf.Pixbuf.new(0, 0, 8, 1, 1); factory.save_thumbnail(pixbuf, uri, mtime); print(factory.lookup(uri, mtime))"`
# outname=`python3 -c "from PyQt5.QtCore import QUrl; import hashlib; import os.path; fileUrl = QUrl.fromLocalFile('$fname').toString(QUrl.FullyEncoded); fileUrl = fileUrl.encode('utf-8'); uriHash = hashlib.md5(fileUrl).hexdigest(); print(os.path.join('/home/Eduardo/.cache/thumbnails/large','%s.png' % uriHash))"`
# Possible way to create a thumbnail at Deepin without a dthumbnailprovider for DDE-File-Manager: https://github.com/linuxdeepin/deepin-riscv/blob/07b566d791e09187bed3664b4a9848c117ead653/dde-api-5.5.5/thumbnails/loader/loader.go
#outname=$(get_recent_file "$SAVE_FOLDER/normal")
#echo "outname: ${outname}" >> $REDEBUG_LOG
outname="$dirname/${filename%.*}.png"
# no work if thumbnail already present
if [ ! -e "${outname}" ]; then
echo "ruffle ${fname}" > $REDEBUG_LOG
ruffle "$fname" &
PID_RUFFLE=$!
echo "PID_RUFFLE ${PID_RUFFLE}" >> $REDEBUG_LOG
# Ruffle do not decode Japanese filenames into window title, use Ruffle PID instead
#rufflewname="Ruffle - "$filename""
while true; do
#rufflewhandle=$(xdotool search --name "$rufflewname")
rufflewhandle=$(xdotool search --pid $PID_RUFFLE)
if expr "$rufflewhandle" : '-\?[0-9]\+$' >/dev/null
then
echo "rufflewhandle: ${rufflewhandle}" >> $REDEBUG_LOG
xdotool windowminimize $rufflewhandle
break
fi
if ! ps -p $PID_RUFFLE > /dev/null
then
echo "ruffle window was closed. exiting." >> $REDEBUG_LOG
exit
fi
done
sleep 1
#import -window "$rufflewname" -delay 200 -silent "$dirname/${filename%.*}.png"
import -window "$rufflewhandle" -delay 200 -silent "${outname}"
echo "import -window ${rufflewhandle} -delay 200 -silent '${outname}'" >> $REDEBUG_LOG
#convert -thumbnail 256x256 "/tmp/${outname}" "${outname}" &>/dev/null
#echo "convert -thumbnail 256x256 '/tmp/${outname}' '${outname}'" >> $REDEBUG_LOG
#sleep 1
#killall ruffle
kill -9 $PID_RUFFLE
else
echo "Ignoring '$fname'. Thumbnail '${outname}' already exists." >> $REDEBUG_LOG
fi
}
# Remove search prefix when thumbnailing search file results from Deepin File Manager.
dde_search_prefix() {
fname="$1"
# If $fname starts with `search` keyword
if [ "${fname:0:6}" = search ]; then
echo "Contains Search Prefix" >> $REDEBUG_LOG
# Before: search:?url=file:///home/eduardo/&keyword=*.swf#file:///home/eduardo/a%2001.swf
# After: /home/eduardo/a%2001.swf
# If it's not a search file result then this will not change the argument value.
searchprefix="file://"
fname="${fname##*$searchprefix}"
# Decode $fname to UTF-8
# Before: /home/eduardo/a%2001.swf
# After: /home/eduardo/a 01.swf (note the space)
fname=$(utfdecode "$fname")
fi
echo "$fname"
}
utfdecode() {
echo "$1" | sed 's@+@ @g;s@%@\\x@g' | xargs -0 printf "%b"
}
# https://www.baeldung.com/linux/get-recent-file-in-directory
get_recent_file () {
FILE=$(ls -Art1 "$1" | tail -n 1)
echo $FILE
exit
}
if [ $# -lt 2 ]; then
# Single argument given
ruffle_thumbnailer "$1"
else
echo "Multiple arguments given" >> $REDEBUG_LOG
echo "Arguments: $@"
# sudo apt-get install parallel
# parallel -j 3 -k "ruffle-exporter" {} ::: "$@"
#for fname in "$@"; do
# ruffle_thumbnailer "$fname"
#done
(
N=3
(
COUNTER=1
for fname in "$@"; do
((i=i%N)); ((i++==0)) && wait
zenitypercentage="$(echo "100/$#*$COUNTER" | bc -l)"
zenitypercentage="${zenitypercentage%.*}" # Convert float to integer
if [[ -z "$zenitypercentage" ]]; then zenitypercentage=0; fi # If not a number, aka. is empty - can happen if percentage value is below than zero
if [[ $zenitypercentage -ge 100 ]]; then zenitypercentage=99; fi
#zenitytitle="$(utfdecode $(basename "$fname"))\n"$COUNTER" of "$#" ("${zenitypercentage%.*}"%)"
zenitytitle="...$(utfdecode $(dde_search_prefix "${fname: -120}"))\n"$COUNTER" of "$#" ("$zenitypercentage"%)"
echo -e "$zenitytitle" >> $REDEBUG_LOG
echo "# $zenitytitle"
echo "$zenitypercentage"
if [[ $COUNTER -ge $# ]]; then
ruffle_thumbnailer "$fname"
else
ruffle_thumbnailer "$fname" &
fi
let COUNTER=COUNTER+1
done
# Zenity closes automatically when percentage reaches 100%, so we only set it as 100% when the last ruffle_thumbnailer call is finished.
echo "# $zenitytitle"
echo "100"
)
) |
zenity --progress \
--title="ruffle-export" \
--percentage=0 --width=600 --auto-close &
# get zenity process id
PID_ZENITY=${!}
# get firstly created child process id, which is running all tasks
PID_CHILD=$(pgrep -o -P $$)
# loop to check that progress dialog has not been cancelled
# http://www.bernaerts-nicolas.fr/linux/331-linux-cancel-zenity-progress-dialog-right-way
while [ "$PID_ZENITY" != "" ]
do
# get PID of all running tasks
PID_TASKS=$(pgrep -d ' ' -P ${PID_CHILD})
# check if zenity PID is still there (dialog box still open)
PID_ZENITY=$(ps h -o pid --pid ${PID_ZENITY} | xargs)
# sleep for 2 second
sleep 2
done
# if some running tasks are still there, kill them
[ "${PID_TASKS}" != "" ] && kill -9 ${PID_TASKS}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment