Files
repos_scripts/screen
2023-10-06 16:53:16 +05:30

122 lines
6.4 KiB
Bash
Executable File

#!/bin/sh
# script for screeen recording with either mic or speaker
# ability to record both mic and speaker at same time is coming soon
info() {
notify-send -h "string:x-canonical-private-synchronous:${0##*/}" -i "$1" -t "${2:-5000}" "$3" "$4"
}
menu() {
bemenu --fn 'IBM Plex Sans 15' -i -c -W "0.${2:-5}" -B 3 -p "$1" -l 26 --accept-single
}
compress() {
duration=$(ffprobe -hide_banner -i "$tmp_file" 2>&1 | sed -nE 's|.*Duration: ([^,]*).*|\1|p')
i=20
while :; do
ffmpeg -loglevel error -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -i "$tmp_file" -vf 'format=nv12,hwupload' -c:v h264_vaapi -qp "$i" "$process_file" -y -progress - | while read -r line; do
complete=$(printf "%s\n" "$line" | sed -nE 's|out_time=(.*)|\1|p')
size=$(du -m "$process_file" | cut -f1)
[ -n "$complete" ] && info "ffmpeg" "700" "Compressing File under 25 MB Using Quality Preset : $i.." "$complete / $duration ($size MB)"
[ "$size" -gt 25 ] && killall -s KILL ffmpeg && break
done
[ "$(du -m "$process_file" | cut -f1)" -lt 25 ] && break
: $((i += 1))
done
info "ffmpeg" "1000" "File Successfully compressed with Quality Preset : $i"
[ -f "$process_file" ] && tmp_file=$process_file
}
record() {
rm -f "$tmp_file" "$process_file"
#shellcheck disable=SC2034
mic="$(pactl list sources | grep -B1 'node.nick = "Digital Microphone"' | sed -nE 's|.*node.name = "([^"]*)"|\1.monitor|p')" #It is called in eval on line 9
#shellcheck disable=SC2034
speaker="$(pactl list sources | grep -B1 'node.nick = "Speaker + Headphones"' | sed -nE 's|.*node.name = "([^"]*)"|\1.monitor|p')" #It is called in eval on line 9
source=$(printf "screen only\nspeaker\nmic" | menu "Audio Source:" "3")
[ -z "$source" ] && info "" "2000" "No option Selected" && return 0
monitor=$(hyprctl monitors | sed -nE 's|Monitor ([^ ]*).*|\1|p' | menu "Choose Monitor:" "3")
[ -z "$monitor" ] && info "" "2000" "No option Selected" && return 0
[ "$source" != "screen only" ] && pactl set-default-source "$(eval "printf \$$source")" && arg=-a
#shellcheck disable=SC2086,SC2090
info "simplescreenrecorder" "1000" "Select Region" "Tap/Click to select full" && setsid -f wf-recorder -x yuv420p -f /tmp/screenrecord.mp4 --output "$monitor" $arg -g "$(slurp)" >/dev/null 2>&1
info "simplescreenrecorder" "700" "Recording Started" "Capturing $monitor($source)"
}
stop() {
pgrep -af wf-recorder >/dev/null && killall -s SIGINT wf-recorder 2>/dev/null || return 0
info "" "" "Recording Stopped"
case $(printf "upload to discord\nupload to nextcloud\nupload to oshi\nsave locally" | menu "Post-Record:" '3') in
*oshi)
info "icloud" "" "Uploading to Oshi"
out=$(curl -sk https://oshi.at -F shorturl=0 -F "f=@$tmp_file")
[ -z "$out" ] && info "" "2000" "Unable to upload to oshi,moved the file from temp to your home folder as <unix_timestamp>.mp4" && mv "$tmp_file" "$HOME/$(date +%s).mp4" && return 1
printf "%s" "$out" | sed -nE 's|DL: (.*)|\1|p' | wl-copy && info "com.github.davidmhewitt.clipped" "2000" "Uploaded and Copied link to clipboard"
;;
*nextcloud)
#shellcheck disable=SC1091
. "$HOME"/.secrets
#load nextcloud creds only
load_nextcloud_creds
cloudname="screenrecord-$(date +"%F_%H-%M-%S").mp4"
#upload to nextcloud instance, universal code
info "nextcloud" "10000" "Uploading to NextCloud"
curl -s "$NC_HOST/remote.php/dav/files/${NC_USER}${NC_FOLDER_PATH}/$cloudname" -H "Authorization: Basic $NC_AUTH" -T "$tmp_file" || (info "" "2000" "Unable to upload to Nextcloud,moved the file from temp to your home folder as $cloudname" && mv "$tmp_file" "$HOME/$cloudname" && return 1)
info "nextcloud" "" "Getting File Id"
file_id=$(curl -s "$NC_HOST/remote.php/dav/files/${NC_USER}${NC_FOLDER_PATH}" -X 'PROPFIND' -H "Authorization: Basic $NC_AUTH" --data-raw '<?xml version="1.0"?><d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns" xmlns:ocs="http://open-collaboration-services.org/ns"><d:prop><oc:fileid /><nc:system-tags /></d:prop></d:propfind>' | sed 's|d:response|\n|g' | sed -nE "s|.*<d:href>.*$cloudname.*<oc:fileid>([^<]*).*|\1|p" | head -1)
[ "$file_id" = "$NC_FOLDER_ID" ] && file_id=""
info "nextcloud" "10000" "Setting Retention Policy & Generating Share Link"
curl -sX PUT "$NC_HOST/remote.php/dav/systemtags-relations/files/$file_id/1" -H "Authorization: Basic $NC_AUTH" -: -s -H "Authorization: Basic $NC_AUTH" "$NC_HOST/ocs/v2.php/apps/files_sharing/api/v1/shares" -H 'content-type: application/json' -H 'accept: application/json, text/plain, */*' -H 'OCS-APIRequest: true' --data-raw "{\"path\":\"${NC_FOLDER_PATH}/$cloudname\",\"shareType\":3,\"attributes\":\"[]\"}" | sed -nE 's|.*"url":"([^"]*)".*|\1|p' | sed 's/\\//g' | wl-copy && info "com.github.davidmhewitt.clipped" "2000" "Share Link Created and copied to clipboard"
;;
*discord)
#shellcheck disable=SC1091
. "$HOME"/.secrets
#load discord creds only
load_discord_creds
base_url="https://discord.com/api/v10"
channel=$(curl -s "$base_url/guilds/$SERVER_ID/channels" -H "Authorization: $TOKEN" | tr '{}' '\n' | sed -nE 's|.*"id":"([^"]*)".*last_message_id.*"name":"([^"]*)".*|\1\t\2|p' | menu "Select Channel: ")
channel_name=$(printf "%s" "$channel" | cut -f2)
channel_id=$(printf "%s" "$channel" | cut -f1)
[ "$(du -m "$tmp_file" | cut -f1)" -gt 25 ] && compress #compressing file if greater than 25 MB
info "discord" "" "Uploading to Discord in $channel_name"
content="${tmp_file##*/} ($(du -h "$tmp_file" | cut -f1)) uploaded by coolans"
curl -s "$base_url/channels/$channel_id/messages" -H "Authorization: $TOKEN" -H "Accept: application/json" -H "Content-Type: multipart/form-data" -X POST -F "file=@$tmp_file" -F 'payload_json={"content":"'"${content}"'"}' && info "" "2000" "Uploaded $tmp_file ($(du -h "$tmp_file" | cut -f1)) to $channel_name in Discord" && return 0
filename="screenrecord-$(date +"%F_%H-%M-%S").mp4"
mv "$tmp_file" "$HOME/$filename" && info "" "2000" "Unable to upload to Discord,file moved to your home folder as $filename"
;;
save*)
defaultname="screenrecord-$(date +"%F_%H-%M-%S").mp4"
filename=$(: | menu "Enter Filename (default: $defaultname):")
[ -z "$filename" ] && filename=$defaultname
mv "$tmp_file" "$HOME/$filename"
;;
*)
rm -f "$tmp_file" "$process_file" && info "user-trash" "2000" "Temporary File Deleted"
;;
esac
}
tmp_file="/tmp/screenrecord.mp4"
process_file="/tmp/processed.mp4"
[ "$#" -gt 0 ] && $1 "$2"