Created
November 20, 2024 16:52
-
-
Save markizano/17f1100d77af5cf97f2465c9120d5ce4 to your computer and use it in GitHub Desktop.
x11grab script: Screen record your desktop and audio sources with `ffmpeg` FOR FREE!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Usage: | |
| # | |
| # AVOUTFILE=screecap.mkv x11grab | |
| # | |
| # Records the screen, the output to the speakers and whatever your default mic recording device is. | |
| # After starting the record, you can toggle which input is recorded with the GUI interface: `pavucontrol` | |
| # or direct this with $PULSE_MIC_SOURCE | |
| # Use `pactl list sources` to list the available sources. | |
| if [ -n "$DEBUG" ]; then | |
| set -x | |
| fi | |
| FFMPEG=${FFMPEG:-"ffmpeg -hide_banner"} | |
| #####! BEGIN: Configuration Section !##### | |
| # 0-index of which screen to capture on the $DISPLAY | |
| SCREEN=${1:-0} | |
| # The "$W,$H" factor for the offset. What's the offset of the X,Y anchor for the top-left to start recording? | |
| OFFSET=${OFFSET:-${2:-0,0}} | |
| # This was more relevant on a computer with 2 monitors. If you have >1 display, you can include them in this list. | |
| SCREENS=( | |
| '0,0' | |
| ) | |
| # If the sizes of the displays are different, you can indicate them here. | |
| SIZES=( | |
| 1920x1080 | |
| 1920x1080 | |
| ) | |
| # Monitor the output sent to the speakers by using Pulse audio monitor. | |
| PULSE_AUDIO_SOURCE=${PULSE_AUDIO_SOURCE:-alsa_output.pci-0000_00_1f.3.analog-stereo.monitor} | |
| PULSE_MIC_SOURCE=${PULSE_MIC_SOURCE:-default} | |
| # Set the video and audio codecs to be used in the output. | |
| ACODEC=${ACODEC:-aac} | |
| VCODEC=${VCODEC:-h264} | |
| # Output file. | |
| AVOUTFILE=${AVOUTFILE:-/tmp/x11grab.mp4} | |
| # Size of the screen. | |
| SIZE=${SIZE:-${SIZES[$SCREEN]:-$3}} | |
| # Offset is how many pixels from the top-left corner of the screen to offset before capturing the screen. | |
| # NOTE: ffmpeg will complain if you indicate a size that is larger than the screen size. | |
| OFFSET=${OFFSET:-$SIZES[0]} | |
| # This loop allows for basic processing of some command line options in case you don't know the env var names. (read:accessibility) | |
| while getopts 'osavf' opt; do | |
| case "$opt" in | |
| o) OFFSET=$OPTARG ;; | |
| s) SIZE=$OPTARG ;; | |
| a) ACODEC=$OPTARG ;; | |
| v) VCODEC=$OPTARG ;; | |
| f) AVOUTFILE=$OPTARG ;; | |
| esac | |
| done | |
| #####! END: Configuration Section !##### | |
| # Audio Input options. | |
| AIOPTS=${AIOPTS:-"-thread_queue_size 256 -f pulse -i $PULSE_AUDIO_SOURCE -thread_queue_size 256 -f pulse -i $PULSE_MIC_SOURCE"} | |
| # Video Input options. | |
| VIOPTS=${VIOPTS:-"-thread_queue_size 64 -analyzeduration 100M -probesize 100M -framerate ${FRAME_RATE:-30} -f x11grab -s ${SIZE} -i $DISPLAY+${OFFSET}"} | |
| # Audio Output options. | |
| AOOPTS=${AOOPTS:-"-c:a ${ACODEC} -filter_complex '[0:a][1:a]amix[audio]' -map [audio] -map 2:v"} | |
| # Video Output options. | |
| VOOPTS=${VOOPTS:-"-c:v ${VCODEC} -preset veryfast -crf ${CRF:-28} -r ${FRAME_RATE:-30} -pix_fmt yuv420p"} | |
| AVOPTS="$AIOPTS $VIOPTS $AOOPTS $VOOPTS -y $AVOUTFILE" | |
| pkill compton | |
| #trap "$HOME/bin/compton.sh" EXIT | |
| echo "Execute: $FFMPEG $AVOPTS" | |
| eval "$FFMPEG $AVOPTS" | |
| # These are here for reference to give an idea of how these commands evolved. | |
| #ffmpeg -f pulse -i default -f x11grab -s 1920x1080 -i :0.0+0,92 -q:v 1 -y /tmp/live.mkv -acodec ac3 -ar 48000 -b 224k -vcodec libx264 -bt 8192k -r 50 | |
| # avconv is an alternative to ffmpeg. Doesn't quite behave the same, but very similar options. | |
| #avconv -f pulse -i default -f x11grab -s 1920x1080 -i :0.0+0,92 -y -f matroska -b:a 224k -b:v 8192k -c:a ac3 -c:v libx264 -y /tmp/live.mkv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment