From 06c310ba413ab8222a072d0d6e9fc47aaa106bcc Mon Sep 17 00:00:00 2001 From: speedie Date: Sun, 11 Dec 2022 18:12:56 +0100 Subject: [PATCH] huge cleanup --- src/speedwm-dfmpeg | 166 +++++++++++---------------------------------- 1 file changed, 41 insertions(+), 125 deletions(-) diff --git a/src/speedwm-dfmpeg b/src/speedwm-dfmpeg index b89376c..969c3b8 100755 --- a/src/speedwm-dfmpeg +++ b/src/speedwm-dfmpeg @@ -7,148 +7,71 @@ # https://speedie.gq/dmenu-scripts # speedwm specfic dotfile directory, probably don't change -DOTDIR=$HOME/.config/speedwm -NOMKCONFIG=false # Do not create a config file if this is set to true +[ -z "$DFMPEG_DOTDIR" ] && DFMPEG_DOTDIR="$HOME/.config/speedwm" +[ -z "$NOMKCONFIG" ] && NOMKCONFIG=false +[ -z "$HAVEGRID" ] && HAVEGRID=true +[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=dmenu +[ "$1" = "-s" ] && pkill -x ffmpeg && rm -f /tmp/dfmpeg-recording && STATUS=idle && exit -# 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/globalrc" ]; then - . $HOME/.config/speedwm/globalrc -else - - 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/globalrc -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/dfmpegrc for configuration!" && . $DOTDIR/dfmpegrc -} +mkdir -p "$DFMPEG_DOTDIR" MKCONFIG() { - if [ -e "$DOTDIR/dfmpegrc" ]; then - USECONFIG - else - if [ "$NOMKCONFIG" = "false" ]; then - mkdir -p $DOTDIR - printf "RESOLUTION=$RESOLUTION # The resolution to capture in" > $DOTDIR/dfmpegrc - printf "\nAUDIO_DEVICE=$AUDIO_DEVICE # How to capture audio (alsa/pulseaudio)" >> $DOTDIR/dfmpegrc - printf "\nFRAME_RATE=$FRAME_RATE # Frame rate to capture in." >> $DOTDIR/dfmpegrc - printf "\nOUTPUT_PATH=$OUTPUT_PATH # Directory where captures will be saved." >> $DOTDIR/dfmpegrc - printf "\nOUTPUT_FORMAT=$OUTPUT_FORMAT # What format to use" >> $DOTDIR/dfmpegrc - printf "\n#OUTPUT_FILENAME='filename'" >> $DOTDIR/dfmpegrc - printf "\n\nRECORD_DEVICE=$RECORD_DEVICE" >> $DOTDIR/dfmpegrc - printf "\nWH=$WH" >> $DOTDIR/dfmpegrc - printf "\nTERM=$TERMINAL # Terminal to use when editing the config file" >> $DOTDIR/dfmpegrc - printf "\nEDITOR=$EDITOR # Editor to edit the config file" >> $DOTDIR/dfmpegrc - printf "\nMEDIA_PLAYER=$MEDIA_PLAYER # Media player to play videos in" >> $DOTDIR/dfmpegrc - printf "\nENCODE=$ENCODE # Encode or not on 'Stop' (true/false)" >> $DOTDIR/dfmpegrc - printf "\nENCODE_CODEC=$ENCODE_CODEC # Codec to encode in (libx264 is default)" >> $DOTDIR/dfmpegrc - printf "\nENCODE_PRESET=$ENCODE_PRESET # ffmpeg preset. Available options: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow" >> $DOTDIR/dfmpegrc - fi - fi + [ -e "$DFMPEG_DOTDIR/dfmpegrc" ] && . "$DFMPEG_DOTDIR/dfmpegrc" && return || \ + cat << CFGFILE > "$DFMPEG_DOTDIR/dfmpegrc" +# dfmpeg config file +# See https://codeberg.org/speedie/speedwm-extras for more information +RESOLUTION="\$(xdpyinfo | awk '/dimensions/ {print \$2}')" # The resolution to record in +AUDIO_BACKEND="pulse" # How to capture audio (alsa/pulse) +AUDIO_DEVICE="default" # Device to capture audio from. If you're using pulseaudio, check this using pactl list short sources. Set to default to use default. +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-"\$(date "+%d_%m_%y-%H%M%S")"" # 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. + +# Extra features +TERM="st" # 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 +CFGFILE } 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="ffmpeg -f $RECORD_DEVICE -s $RESOLUTION -i $WH -f $AUDIO_BACKEND -r $FRAME_RATE -i $AUDIO_DEVICE $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="ffmpeg -y -f x11grab -framerate $FRAME_RATE -s $RESOLUTION -i $WH -f $AUDIO_BACKEND -i $AUDIO_DEVICE -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 + 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 + 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" } # Hardcoded version number -dfmpeg_ver="2022-08-06-r1" +dfmpeg_ver="2022-12-11-r1" about_screen="dfmpeg $dfmpeg_ver." # About screen text -about_screen_2="Licensed under MIT, written by speedie.gq and jornmann." +about_screen_2="Licensed under MIT, written by speedie.gq and emilyd." 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 @@ -164,11 +87,10 @@ encodeopts() { # 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 last recording") [ "$ENCODE" != "false" ] && 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/dfmpegrc ; exit 0 ;; - "Convert") convert ; exit 0 ;; + "Configure") $TERM $EDITOR "$DFMPEG_DOTDIR/dfmpegrc" ; exit 0 ;; "") exit 0 ;; "..") $0 && exit 0 ;; "------") exit 0 ;; @@ -182,16 +104,10 @@ case "$(printf 'Start\nStop\nStart without audio\nPlay last\n------\nMore option "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 ;; + "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 + printf "$about_screen\n$about_screen_2\n$about_screen_3\n$about_screen_4\n" | $RUNLAUNCHER -l 40 $GRIDARG $GRIDNUM ;; esac - -exit 0 # This fixes a small bug.