From bfb1066bbea5fdfb56d2e8b713dd814cad7ccea3 Mon Sep 17 00:00:00 2001 From: rqdmap <55649208+rqdmap@users.noreply.github.com> Date: Tue, 18 Apr 2023 10:57:23 +0800 Subject: [PATCH 1/5] Make giph stop all previous recordings properly --- src/giph | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/giph b/src/giph index 8127d42..7adc17f 100755 --- a/src/giph +++ b/src/giph @@ -470,8 +470,14 @@ function giph() { } function stop_all_recordings() { - for pid in "$(pgrep -f "bash.+giph")"; do - kill -INT -$pid + for pid in $(pgrep -f "bash.+giph"); do + if [ "$pid" -eq $$ ]; then + continue + fi + record_pid=$(pgrep -P $pid -f "ffmpeg -f x11grab") + if [ -n "$record_pid" ]; then + kill -INT $record_pid + fi done } From 67d1467b8521f0b406992446e57a219d79cdd3dd Mon Sep 17 00:00:00 2001 From: rqdmap <55649208+rqdmap@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:23:50 +0800 Subject: [PATCH 2/5] fix improper numeric compare --- src/giph | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/giph b/src/giph index 7adc17f..182d14d 100755 --- a/src/giph +++ b/src/giph @@ -471,7 +471,7 @@ function giph() { function stop_all_recordings() { for pid in $(pgrep -f "bash.+giph"); do - if [ "$pid" -eq $$ ]; then + if [ $pid -eq $$ ]; then continue fi record_pid=$(pgrep -P $pid -f "ffmpeg -f x11grab") From 3d0af19c3c670ed0cf75ddd99742f7e0a4bb5e32 Mon Sep 17 00:00:00 2001 From: rqdmap <55649208+rqdmap@users.noreply.github.com> Date: Thu, 20 Apr 2023 13:11:13 +0800 Subject: [PATCH 3/5] Keep the orgin function still, add new SIGUSR1 to stop recording only --- src/giph | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/giph b/src/giph index 182d14d..fe764b3 100755 --- a/src/giph +++ b/src/giph @@ -2,6 +2,25 @@ stty -echoctl # don't print ^C when pressing ctrl+c +function handle_usr1(){ + if [[ $FFMPEG_PID ]]; then + kill -int "$FFMPEG_PID" + FFMPEG_PID="" + + wait + + [ "$?" = 1 ] && log_error "recording video with ffmpeg failed" + log "completed ffmpeg video recording" 1 true + + convert + wait + + exit + fi +} + +trap handle_usr1 SIGUSR1 + readonly VERSION=1.1.1 # verbosity @@ -49,6 +68,7 @@ OPTIONS -ms, --microphone-source=STRING Overwrite the default microphone source. -y, --notify Send notification on error or success. --stop Finish any running giph recordings. + --finish Finish any screen recordings only. SLOP OPTIONS -b, --bordersize=FLOAT Set the selection border thickness. @@ -130,6 +150,9 @@ while [[ "$1" == -* ]]; do --stop) SHOULD_STOP=true ;; + --finish) + SHOULD_FINISH=true + ;; -s|--select) SLOP=1 ;; @@ -454,11 +477,13 @@ function delete_temporary_directory() { log "deleted temporary directory $TEMP_DIRECTORY" 2 true } -function giph() { +function prepare() { get_geometry create_temporary_directory record +} +function convert() { if [[ "$FORMAT" == "gif" || "$FORMAT" == "webm" ]]; then encode fi @@ -467,22 +492,37 @@ function giph() { delete_temporary_directory exit 0 + +} +function giph() { + prepare + convert } + function stop_all_recordings() { for pid in $(pgrep -f "bash.+giph"); do if [ $pid -eq $$ ]; then continue fi - record_pid=$(pgrep -P $pid -f "ffmpeg -f x11grab") - if [ -n "$record_pid" ]; then - kill -INT $record_pid + kill -INT -$pid + done +} + + +function stop_recording_only() { + for pid in $(pgrep -f "bash.+giph"); do + if [ "$pid" -eq $$ ]; then + continue fi + kill -USR1 $pid done } if [ -n "$SHOULD_STOP" ]; then stop_all_recordings +elif [ -n "$SHOULD_FINISH" ]; then + stop_recording_only else giph fi From 3b2e0364b6e20e72987b3b1446ceae091a522aac Mon Sep 17 00:00:00 2001 From: rqdmap <55649208+rqdmap@users.noreply.github.com> Date: Thu, 4 May 2023 20:22:18 +0800 Subject: [PATCH 4/5] Replace old stop_all_recordings function --- src/giph | 53 ++++++++++++++++------------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/src/giph b/src/giph index fe764b3..ac09487 100755 --- a/src/giph +++ b/src/giph @@ -2,20 +2,13 @@ stty -echoctl # don't print ^C when pressing ctrl+c +signal_usr1_handled=false function handle_usr1(){ - if [[ $FFMPEG_PID ]]; then - kill -int "$FFMPEG_PID" - FFMPEG_PID="" - - wait - - [ "$?" = 1 ] && log_error "recording video with ffmpeg failed" - log "completed ffmpeg video recording" 1 true - - convert - wait - - exit + # Ensure giph only receives SIGUSR1 ONCE + if [[ -n $FFMPEG_PID && "$signal_usr1_handled" == false ]]; then + signal_usr1_handled=true + kill $FFMPEG_PID + wait $FFMPEG_PID fi } @@ -68,7 +61,7 @@ OPTIONS -ms, --microphone-source=STRING Overwrite the default microphone source. -y, --notify Send notification on error or success. --stop Finish any running giph recordings. - --finish Finish any screen recordings only. + --kill Abort all giph recordings. SLOP OPTIONS -b, --bordersize=FLOAT Set the selection border thickness. @@ -150,8 +143,8 @@ while [[ "$1" == -* ]]; do --stop) SHOULD_STOP=true ;; - --finish) - SHOULD_FINISH=true + --kill) + SHOULD_KILL=true ;; -s|--select) SLOP=1 @@ -477,13 +470,11 @@ function delete_temporary_directory() { log "deleted temporary directory $TEMP_DIRECTORY" 2 true } -function prepare() { +function giph() { get_geometry create_temporary_directory record -} -function convert() { if [[ "$FORMAT" == "gif" || "$FORMAT" == "webm" ]]; then encode fi @@ -492,37 +483,25 @@ function convert() { delete_temporary_directory exit 0 - -} -function giph() { - prepare - convert } function stop_all_recordings() { for pid in $(pgrep -f "bash.+giph"); do - if [ $pid -eq $$ ]; then - continue - fi - kill -INT -$pid + kill -USR1 $pid done } -function stop_recording_only() { - for pid in $(pgrep -f "bash.+giph"); do - if [ "$pid" -eq $$ ]; then - continue - fi - kill -USR1 $pid - done +# Todo +function abort_all_recordings() { + : } if [ -n "$SHOULD_STOP" ]; then stop_all_recordings -elif [ -n "$SHOULD_FINISH" ]; then - stop_recording_only +elif [ -n "$SHOULD_KILL" ]; then + abort_all_recordings else giph fi From 335a73b709d6e88a81301baed5e678207d8d0a66 Mon Sep 17 00:00:00 2001 From: rqdmap <55649208+rqdmap@users.noreply.github.com> Date: Thu, 4 May 2023 21:09:10 +0800 Subject: [PATCH 5/5] Add --kill flag --- src/giph | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/giph b/src/giph index ac09487..6057ef0 100755 --- a/src/giph +++ b/src/giph @@ -7,12 +7,21 @@ function handle_usr1(){ # Ensure giph only receives SIGUSR1 ONCE if [[ -n $FFMPEG_PID && "$signal_usr1_handled" == false ]]; then signal_usr1_handled=true - kill $FFMPEG_PID + kill $FFMPEG_PID wait $FFMPEG_PID fi } +function handle_usr2(){ + if [[ -n $FFMPEG_PID ]]; then + kill -KILL $FFMPEG_PID + fi + delete_temporary_directory + exit 1 +} + trap handle_usr1 SIGUSR1 +trap handle_usr2 SIGUSR2 readonly VERSION=1.1.1 @@ -493,9 +502,10 @@ function stop_all_recordings() { } -# Todo function abort_all_recordings() { - : + for pid in $(pgrep -f "bash.+giph"); do + kill -USR2 $pid + done } if [ -n "$SHOULD_STOP" ]; then