speedwm-personal/scripts/speedwm-dfmpeg

198 lines
7.6 KiB
Bash
Executable file

#!/bin/sh
# speedwm-dfmpeg
# dmenu/other run launcher GUI for recording your screen with ffmpeg
#
# Licensed under MIT, written by speedie
# Modified version for speedwm (available https://speedie.gq/speedwm)
# https://speedie.gq/dmenu-scripts
# speedwm specfic dotfile directory, probably don't change
DOTDIR=$HOME/.config/speedwm-de/dfmpeg
NOMKCONFIG=false # Do not create a config file if this is set to true
# Set runlauncher
case "$RUNLAUNCHER" in
"") RUNLAUNCHER=dmenu
esac
# -s flag stops recording
case "$1" in
"-s") pkill -x ffmpeg ; rm /tmp/dfmpeg-recording ; STATUS=idle ; exit ;;
esac
HAVE_GRID="true"
if [ -e "$HOME/.config/speedwm-de/global/config" ]; then
. $HOME/.config/speedwm-de/global/config
else
mkdir -p $HOME/.config/speedwm-de/global
printf "HAVE_GRID=$HAVE_GRID # Whether or not to use the Grid argument. If you do not have the dmenu grid patch, set this to false. Doing so will disable grid." > $HOME/.config/speedwm-de/global/config
fi
if [ "$HAVE_GRID" = "true" ]; then
GRIDNUM="1"
fi
if [ "$HAVE_GRID" = "true" ]; then
GRIDARG="-g"
fi
# Write default configuration
defaultConfig() {
RESOLUTION="1920x1080" # The resolution to record in
AUDIO_DEVICE="alsa" # How to capture audio (alsa/pulseaudio)
FRAME_RATE="60" # Frame rate to capture in.
RECORD_DEVICE="x11grab" # Probably do not change.
OUTPUT_PATH="$HOME/Recordings" # Path where videos will be saved.
OUTPUT_FORMAT="mp4" # What format to use
OUTPUT_FILENAME="dfmpeg-output" # File name of the output. Probably don't need to change.
ENCODE=true # Encode or not (true/false)
ENCODE_CODEC=libx264 # Codec to encode in
ENCODE_PRESET=ultrafast # ffmpeg preset. Available options: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
WH=":0.0" # Width and height, no need to change, defaults should work.
TERM="$TERMINAL" # Terminal to use when editing the configuration file.
EDITOR="vim" # Editor to edit the config file with
MEDIA_PLAYER="mpv" # Media player to play videos in
}
defaultConfig
USECONFIG() {
echo "Using $DOTDIR/config for configuration!" && . $DOTDIR/config
}
MKCONFIG() {
if [ -e "$DOTDIR/config" ]; then
USECONFIG
else
if [ "$NOMKCONFIG" = "false" ]; then
mkdir -p $DOTDIR
printf "RESOLUTION=$RESOLUTION # The resolution to capture in" > $DOTDIR/config
printf "\nAUDIO_DEVICE=$AUDIO_DEVICE # How to capture audio (alsa/pulseaudio)" >> $DOTDIR/config
printf "\nFRAME_RATE=$FRAME_RATE # Frame rate to capture in." >> $DOTDIR/config
printf "\nOUTPUT_PATH=$OUTPUT_PATH # Directory where captures will be saved." >> $DOTDIR/config
printf "\nOUTPUT_FORMAT=$OUTPUT_FORMAT # What format to use" >> $DOTDIR/config
printf "\n#OUTPUT_FILENAME='filename'" >> $DOTDIR/config
printf "\n\nRECORD_DEVICE=$RECORD_DEVICE" >> $DOTDIR/config
printf "\nWH=$WH" >> $DOTDIR/config
printf "\nTERM=$TERMINAL # Terminal to use when editing the config file" >> $DOTDIR/config
printf "\nEDITOR=$EDITOR # Editor to edit the config file" >> $DOTDIR/config
printf "\nMEDIA_PLAYER=$MEDIA_PLAYER # Media player to play videos in" >> $DOTDIR/config
printf "\nENCODE=$ENCODE # Encode or not on 'Stop' (true/false)" >> $DOTDIR/config
printf "\nENCODE_CODEC=$ENCODE_CODEC # Codec to encode in (libx264 is default)" >> $DOTDIR/config
printf "\nENCODE_PRESET=$ENCODE_PRESET # ffmpeg preset. Available options: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow" >> $DOTDIR/config
fi
fi
}
MKCONFIG
OUTPUT_FILENAME="$OUTPUT_PATH/$OUTPUT_FILENAME.$OUTPUT_FORMAT"
# Create the output path if it does not exist
if [ -e "$OUTPUT_PATH" ]; then
a=$a
else
mkdir -p $OUTPUT_PATH
fi
# Check if we should encode the video or not.
if [ "$ENCODE" = "false" ]; then
startrec="ffmpeg -f $RECORD_DEVICE -s $RESOLUTION -i $WH -f $AUDIO_DEVICE -r $FRAME_RATE -i default $OUTPUT_FILENAME"
startrec_no_audio="ffmpeg -y -f $RECORD_DEVICE -s $RESOLUTION -r $FRAME_RATE -i $WH $OUTPUT_FILENAME"
else
startrec="ffmpeg -y -f x11grab -framerate $FRAME_RATE -s $RESOLUTION -i $WH -f $AUDIO_DEVICE -i default -r 30 -c:v $ENCODE_CODEC -crf 0 -preset ultrafast -c:a aac $OUTPUT_FILENAME"
startrec_no_audio="ffmpeg -y -f x11grab -framerate $FRAME_RATE -s $RESOLUTION -i $WH -r 30 -c:v $ENCODE_CODEC -crf 0 -preset ultrafast -c:a aac $OUTPUT_FILENAME"
touch /tmp/isencoding
echo $ENCODE_CODEC > /tmp/encoding-codec
fi
# Call this function to encode a video manually.
# /tmp/rec.$OUTPUT_FORMAT must be the file. Consider copying it there and then copying it back.
encode() {
if [ "$ENCODE" = "true" ]; then
cp $(cat /tmp/outputfilename) /tmp/rec.$OUTPUT_FORMAT
ffmpeg -i /tmp/rec.$OUTPUT_FORMAT -c:v $ENCODE_CODEC -preset $ENCODE_PRESET -threads 0 -b:v -pass 1 -f $OUTPUT_FORMAT -qp 0
cp /tmp/rec.$OUTPUT_FORMAT $(cat /tmp/outputfilename)
rm -f "/tmp/rec.$OUTPUT_FORMAT"
fi
}
# Hardcoded version number
dfmpeg_ver="2022-08-06-r1"
about_screen="dfmpeg $dfmpeg_ver."
# About screen text
about_screen_2="Licensed under MIT, written by speedie.gq and jornmann."
about_screen_3="Modified for https://speedie.gq/speedwm"
about_screen_4="https://speedie.gq/dmenu-scripts"
# Conversion function
convert() {
# Convert mp4 to mp3
mp42mp3() {
COPT=$(printf 'Enter a path\n------\nExit' | $RUNLAUNCHER -l 3 -p 'Convert which file?')
case "$COPT" in
"Enter a path") exit 0 ;;
"------") exit 0 ;;
"Exit") exit 0 ;;
"") exit 0 ;;
esac
echo $COPT | grep ".mp4" && ffmpeg -i $COPT -vn $(echo $COPT | sed "s/.mp4/.mp3/g")
}
case "$(printf '.mp4 to .mp3\n------\n..\nExit' | $RUNLAUNCHER -l 40 -p 'Convertion options')" in
".mp4 to .mp3") mp42mp3 ;;
"Exit") exit 0 ;;
"------") exit 0 ;;
"..") $0 && exit 0 ;;
esac
}
encodeopts() {
case "$(printf 'Encode last recording\nEncode video\n------\n..\nExit' | $RUNLAUNCHER -l 40 -p 'Encoding options')" in
"Encode last recording") encode ; exit 0 ;;
"Encode video") echo "$(echo "" | $RUNLAUNCHER -l 1 $GRIDARG $GRIDNUM -p "What file do you want to encode?")" > /tmp/outputfilename && encode ; exit 0 ;;
"------") exit 0 ;;
"Exit") exit 0 ;;
"..") $0 && exit 0 ;;
"") exit 0 ;;
esac
}
# More options
MORE_OPTIONS() {
case "$(printf 'Encode\nConvert\nConfigure\n------\n..\nExit' | $RUNLAUNCHER -l 40 -p 'More options')" in
"Encode last recording") encode ; exit 0 ;;
"Encode video") echo "$(echo "" | $RUNLAUNCHER -l 1 $GRIDARG $GRIDNUM -p "What file do you want to encode?")" > /tmp/outputfilename && encode ; exit 0 ;;
"Encode") encodeopts ; exit 0 ;;
"Configure") $TERM $EDITOR $DOTDIR/config ; exit 0 ;;
"Convert") convert ; exit 0 ;;
"") exit 0 ;;
"..") $0 && exit 0 ;;
"------") exit 0 ;;
esac
}
case "$(printf 'Start\nStop\nStart without audio\nPlay last\n------\nMore options\nAbout\nExit' | $RUNLAUNCHER -l 40 -p 'Record your screen:')" in
"Exit") STATUS=idle && exit 0 ;;
"Start") touch /tmp/isaudio ; echo "$OUTPUT_FILENAME" > /tmp/outputfilename && STATUS=recording && touch /tmp/dfmpeg-recording
$startrec && exit 0 ;;
"Stop") pkill -x ffmpeg ; rm /tmp/dfmpeg-recording ; STATUS=idle ;;
"Start without audio") echo "$OUTPUT_FILENAME" > /tmp/outputfilename && STATUS=recording && touch /tmp/dfmpeg-recording
$startrec_no_audio && exit 0 ;;
"Play last") DFMPEG_STATUS=idle && $MEDIA_PLAYER $(cat /tmp/outputfilename) && exit 0 ;;
"More options") MORE_OPTIONS && exit 0 ;;
"") exit 0 ;;
"About")
echo $about_screen > /tmp/dfmpeg_about
echo $about_screen_2 >> /tmp/dfmpeg_about
echo $about_screen_3 >> /tmp/dfmpeg_about
echo $about_screen_4 >> /tmp/dfmpeg_about
cat /tmp/dfmpeg_about | $RUNLAUNCHER -l 40 $GRIDARG $GRIDNUM
;;
esac
exit 0 # This fixes a small bug.