Skip to content

Commit

Permalink
Updated the converter script.
Browse files Browse the repository at this point in the history
ce-flipper-conv.sh now takes several positional arguments (dither mode, framerate to sample at, gamma adjustment, and color). Color is limited to 4-bit for size reasons.
In addition, the default dither mode has been changed to o8x8 -- o2x2 generated nicer, better contrast results for some input videos, but o8x8 is a far more general-purpose dither filter.
  • Loading branch information
bmdeeal authored Mar 10, 2022
1 parent 2457124 commit 44bd972
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions ce-flipper-conv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#show help
#this also stops rogue arguments from being passed to ffmpeg or imagemagick
if [ $# -eq 0 ] || [[ "$1" =~ ^-.* ]] || [ "$1" == "" ]
if [ $# -eq 0 ] || [ $# -gt 5 ] || [[ "$1" =~ ^-.* ]] || [[ "$2" =~ ^-.* ]] || [[ "$3" =~ - ]] || [[ "$4" =~ - ]] || [ "$1" == "" ]
then
echo "ce-flipper-conv.sh -- convert a video to CE-Flipper format"
echo "2022 B.M.Deeal."
Expand All @@ -15,18 +15,51 @@ then
echo "Requires ffmpeg and imagemagick."
echo ""
echo "usage:"
echo " ce-flipper-conv.sh video-file"
echo " ce-flipper-conv.sh video-file [dither mode] [framerate] [gamma] [color|gray]"
echo ""
echo "example:"
echo "$ ce-flipper-conv.sh myvideo.webm o4x4 3 1 color"
echo ""
echo "By default, the dither mode is o8x8. This is passed directly to imagemagick."
echo "To see a list of supported dither modes, run the command:"
echo " convert -list threshold"
echo ""
echo "Framerate is 3 by default. Use 0 to extract all frames."
echo "Gamma is 1 by default. Above 1 is brighter, below 1 is dimmer."
echo "To enable color, simply type the word 'color' as the 5th argument."
echo ""
exit 0
fi

#image settings
dither="${2:-o8x8}"
gamma="${4:-1}"

#construct filenames
fn="$1" #name of file
fn_base="$(basename -- "$fn")" #name of file without path
fn_ext="${fn_base##*.}" #extension of file
fn_strip="$(basename "$fn" ".$fn_ext")" #filename without extension
fn_dir="$(dirname "$fn")" #directory of file
out_dir="$fn_dir/ce-flipper-output" #output directory for script
framerate="${3:-3}"

#handle color
mono_flag="-monochrome"
colors="2"

#enable color if specified
if [[ "$5" == "color" ]] || [[ "$5" == "colour" ]]
then
mono_flag=""
colors="16"
fi

#set dumping all frames
if ! [ "$framerate" == "0" ]
then
framecmd="-r $framerate"
fi

#generate output folder
mkdir "$out_dir"
Expand All @@ -35,20 +68,20 @@ then
#make flag file for ce-flipper
touch "$out_dir/$fn_strip.anim"
#convert video to .png
if ! ffmpeg -i "$fn" -r 3 "$out_dir/$fn_strip.anim-%5d.png"
#if ! ffmpeg -i "$fn" -r 3 "$out_dir/$fn_strip.anim-%5d.png"
if ! ffmpeg -i "$fn" $framecmd "$out_dir/$fn_strip.anim-%5d.png"
then
echo "error: ffmpeg could not convert '$1'!"
exit 1
fi
#convert .png to .bmp
#you can play with that convert command to your liking
#would just use normal error-diffusion dithering but imagemagick produces particularly ugly results for monochrome images (contrast doing it in GIMP, which looks quite nice)
#CE-Flipper doesn't need to use 1-bit .bmp files, but my device is grayscale (and slow) so it works better with them
cd "$out_dir"
for f in *.png
do
echo "generating $(basename "$f" ".png").bmp"
convert "$f" -resize "240x120>" +dither -ordered-dither o2x2 -monochrome -colors 2 "BMP3:$(basename "$f" ".png").bmp"
convert "$f" -gamma "$gamma" -resize "240x120>" +dither -ordered-dither "$dither" $mono_flag -colors "$colors" "BMP3:$(basename "$f" ".png").bmp"
rm "$f"
done
else
Expand Down

0 comments on commit 44bd972

Please sign in to comment.