Skip to content

Instantly share code, notes, and snippets.

@sepastian
Last active March 5, 2020 08:26
Show Gist options
  • Save sepastian/d29be6b52fa39bca1a6ea1e06c71ddaa to your computer and use it in GitHub Desktop.
Save sepastian/d29be6b52fa39bca1a6ea1e06c71ddaa to your computer and use it in GitHub Desktop.
Screencast to file through gstreamer, using resolution of screen
#!/bin/bash
set -euo pipefail
# Record the screen into an h264 encoded MP4.
# Determing video resolution from the screen being recorded.
#
# Required gst-launch and xrandr.
#
# Based on https://stackoverflow.com/a/33822024/92049
# and https://stackoverflow.com/a/44686527/92049
if [ $# != 1 ]; then
echo "Usage: $(basename $0) OUTFILE"
exit 2
fi
outfile="$1"
# Append .mp4 to outfile, if required.
if [[ ! "$outfile" =~ .mp4$ ]]; then
outfile="${outfile}.mp4"
fi
# Determine resolution using xrandr.
resxy=$(xrandr | grep '\*' | awk '{print $1}')
resx=$(echo "$resxy" | cut -d 'x' -f1)
resy=$(echo "$resxy" | cut -d 'x' -f2)
echo
echo "screen resolution: ${resx}x${resy}"
echo "video resolution:${resx}x${resy}"
echo "writting video to ${outfile}"
echo "ctrl+c to stop recording"
gst-launch-1.0 --quiet ximagesrc use-damage=0 \
! video/x-raw,framerate=30/1 \
! videoscale method=0 \
! video/x-raw,width="$resx",height="$resy" \
! videoconvert \
! x264enc \
! filesink location="$outfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment