├── .gitattributes ├── LICENSE.md ├── README.md ├── example_config ├── install ├── shadowreplay └── shadowreplay.service /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ShadowRePlay 2 | 3 | 4 | 5 | Recreates Shadowplay's replay feature on Linux 6 | 7 | 8 | 9 | - compatible with Nvidia (nvenc), AMD (vaapi,amf/vce), Intel (quicksync) GPUs, as well as libx264. 10 | 11 | 12 | 13 | ## Prerequisites 14 | 15 | 16 | 17 | - Have FFMPEG installed 18 | 19 | - `sudo pacman -S ffmpeg` 20 | 21 | - Have Xbindkeys installed 22 | 23 | - `sudo pacman -S xbindkeys` 24 | 25 | - Have libnotify installed 26 | 27 | - `sudo pacman -S libnotify` 28 | 29 | 30 | 31 | ## Installation 32 | 33 | If you are using an Arch-based Linux distribution, shadowreplay is in the AUR, so you can install it using your preferred AUR helper. 34 | 35 | Its package name is `shadowreplay-git`. 36 | 37 | If you are on a Debian-based distro like Ubuntu or anything else, shadowreplay can be installed by running the `install` file after cloning this repo. 38 | 39 | This will install the required dependencies on Arch and install the systemd user service. 40 | 41 | 42 | 43 | ## Configuration 44 | 45 | 46 | 47 | Variables specific to your desired setup can be found in the example_config which is installed to `$HOME/.config/shadowreplay` 48 | 49 | 50 | 51 | ## Setup 52 | 53 | 54 | 55 | ### Keybind Setup 56 | 57 | First, configure the key (or key combo) you want to use in order to save your replays. 58 | 59 | - For instructions on how to bind keys, check [here](http://xahlee.info/linux/linux_xbindkeys_tutorial.html) 60 | 61 | - Consider making another bind for killing ShadowRePlay. (See below) 62 | 63 | - Bind a key to the command `killall --user $USER --ignore-case --signal SIGTERM ffmpeg` so it looks something like this: 64 | 65 | ```sh 66 | 67 | # make F9 save ShadowRePlay replay 68 | 69 | "killall --user $USER --ignore-case --signal SIGTERM ffmpeg" 70 | F9 71 | 72 | 73 | 74 | # make F10 kill ShadowRePlay 75 | 76 | "killall -s1 ffmpeg" 77 | F10 78 | 79 | ``` 80 | 81 | - Start xbindkeys with `xbindkeys -f ~/.xbindkeysrc`, or reload it with `killall -s1 xbindkeys` if it's already running, for the new bind to take effect. 82 | 83 | 84 | ### Usage 85 | 86 | 87 | 88 | - Run `shadowreplay` in a terminal 89 | 90 | - Press your configured hotkey to save the replay. 91 | 92 | 93 | 94 | ## TODO 95 | 96 | - More flexible audio input support 97 | 98 | - Ability to downscale video output during recording 99 | 100 | - Dynamic video buffer size allocation based on resolution and FPS 101 | 102 | 103 | 104 | ## Notes 105 | 106 | Heavily inspired by [Toqozz's script](https://github.com/Toqozz/shadowplay-linux). 107 | 108 | 109 | 110 | Thank you [Tyler](https://github.com/durcor) for answering my stupid questions while making this. 111 | -------------------------------------------------------------------------------- /example_config: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Resolution of your X session 4 | # which you wish to record 5 | # 6 | # Default is your first monitor's resolution 7 | export RESOLUTION="$(xdpyinfo | grep dimensions | awk 'NR==1{print $2}')" 8 | 9 | # If you want to use CPU-based encoding instead 10 | # of GPU-based encoding, set to true. 11 | # 12 | # Overrides ENCODER_STANDARD 13 | export SOFTWARE_ENCODE="false" 14 | 15 | # If you have an AMD GPU and desire 16 | # to use AMF/VCE instead of VAAPI, 17 | # then set this to true. 18 | # 19 | # XXX: Requires AMF to be set up: 20 | # More details at: https://github.com/durcor/amdgpu-pro-amf-only 21 | export NONFREE_AMD_ENCODER="false" 22 | 23 | # Video Framerate 24 | export FPS="60" 25 | 26 | # Common encoders are: 27 | # h264 28 | # hevc 29 | # h265 30 | export ENCODER_STANDARD=h264 31 | 32 | # Size of replay buffer to save 33 | # Is in minutes 34 | # Can be numbers such as 10 (10 minutes) or 2.5 (2 minutes and 30 seconds) 35 | export REPLAY_BUFFER="5" 36 | 37 | # Location to save recordings 38 | # 39 | # By default, saves to the default VIDEOS directory 40 | export VIDEO_FOLDER="$(xdg-user-dir VIDEOS)" 41 | 42 | # FFMPEG Command Options 43 | # Change these to change how shadowreplay works with ffmpeg 44 | # Don't mess with these unless you know what you're doing! 45 | ffmpeg_command(){ # Create the function 46 | ffmpeg \ 47 | -f x11grab \ 48 | -s "$RESOLUTION" \ 49 | -r ${FPS} \ 50 | -i "$DISPLAY" \ 51 | -f pulse -i default \ 52 | -ar 44100 \ 53 | $ENCODER_OPTS \ 54 | -f segment \ 55 | -segment_time 30 \ 56 | -segment_wrap "$BUFFER_SEGMENTS" \ 57 | -segment_list /tmp/ShadowRePlay.m3u8 \ 58 | -segment_list_size 6 /tmp/ShadowRePlaySeg%d.ts 59 | } 60 | 61 | 62 | export -f ffmpeg_command # Export the function 63 | -------------------------------------------------------------------------------- /install: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if ! ls "$HOME"/.config/shadowreplay;then 4 | cp example_config "$HOME"/.config/shadowreplay 5 | fi 6 | 7 | DIR=$(dirname "$0") 8 | 9 | # Install dependencies on Arch 10 | if [ -e "/etc/arch-release" ]; then 11 | echo "Installing dependencies" 12 | sudo pacman --noconfirm -S ffmpeg libnotify 13 | fi 14 | 15 | # move shadowreplay to /usr/local/bin 16 | sudo install "$DIR/shadowreplay" /usr/local/bin 17 | 18 | # Save the xbindkeys config 19 | # if xbindkeys is installed 20 | if ls /bin/xbindkeys;then 21 | echo """ 22 | # make F9 save ShadowRePlay replay 23 | "killall --user "$USER" --ignore-case --signal SIGTERM ffmpeg" 24 | F9 25 | 26 | # make F10 kill ShadowRePlay 27 | "killall -s1 ffmpeg" 28 | F10 29 | """ >> "$HOME/.xbindkeysrc" 30 | fi 31 | 32 | # Install systemd user service 33 | mkdir -p "$HOME/.config/systemd/user/" 34 | cp "$DIR/shadowreplay.service" "$HOME/.config/systemd/user/" 35 | # Start shadowreplay service 36 | systemctl --user enable --now shadowreplay 37 | systemctl --user restart --now shadowreplay 38 | 39 | echo """ 40 | 41 | Installation is complete! 42 | Press F9 to save a replay""" 43 | -------------------------------------------------------------------------------- /shadowreplay: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -eu 4 | 5 | RESOLUTION="$(xdpyinfo | grep dimensions | awk 'NR==1{print $2}')" 6 | SOFTWARE_ENCODE=false 7 | FPS="60" 8 | ENCODER_STANDARD=h264 9 | REPLAY_BUFFER="5" 10 | 11 | if ls "$HOME"/Videos;then 12 | VIDEO_FOLDER="$HOME"/Videos 13 | elif ls /usr/bin/xdg-user-dir;then 14 | VIDEO_FOLDER="$(xdg-user-dir VIDEOS)" 15 | else 16 | mkdir "$HOME"/Videos 17 | VIDEO_FOLDER="$HOME"/Videos 18 | fi 19 | 20 | BUFFER_SEC=$((REPLAY_BUFFER * 60)) 21 | BUFFER_SEGMENT_LENGTH="30" 22 | BUFFER_SEGMENTS=$((BUFFER_SEC / BUFFER_SEGMENT_LENGTH)) 23 | 24 | # Source user-specific config 25 | . $HOME/.config/shadowreplay 26 | 27 | if $NONFREE_AMD_ENCODER;then 28 | ENCODER_API=amf 29 | elif lspci | grep 'VGA.*ATI' >/dev/null;then 30 | ENCODER_API=vaapi 31 | elif lspci | grep 'VGA.*NVIDIA' >/dev/null;then 32 | ENCODER_API=nvenc 33 | elif lspci | grep 'VGA.*Intel' >/dev/null;then 34 | ENCODER_API=qsv 35 | fi 36 | 37 | ENCODER="$ENCODER_STANDARD"_"$ENCODER_API" 38 | 39 | if $SOFTWARE_ENCODE;then 40 | ENCODER_API= 41 | ENCODER=libx264 42 | fi 43 | 44 | BASEDIR=$(dirname "$0") 45 | 46 | DATE=$(date +%Y-%d-%m-%H:%M:%S) 47 | FILE="$VIDEO_FOLDER/replay-$DATE.mp4" 48 | 49 | case $ENCODER_API in 50 | "vaapi") 51 | ENCODER_OPTS="-c:v $ENCODER -vf format='nv12|vaapi,hwupload' -vaapi_device /dev/dri/renderD128" 52 | ;; 53 | "amf") 54 | export VK_ICD_FILENAMES=/opt/amdgpu-pro/etc/vulkan/icd.d/amd_icd64.json:/opt/amdgpu-pro/etc/vulkan/icd.d/amd_icd32.json 55 | ENCODER_OPTS="-c:v $ENCODER" 56 | ;; 57 | "nvenc") 58 | ENCODER_OPTS="-c:v $ENCODER -preset:v llhq -profile:v high -pix_fmt nv12 -b:v 15M -acodec aac" 59 | ;; 60 | "qsv") 61 | ENCODER_OPTS="-c:v $ENCODER" 62 | ;; 63 | *) 64 | ENCODER_OPTS="-c:v $ENCODER -preset medium" 65 | ;; 66 | esac 67 | 68 | 69 | ffmpeg_command || 70 | 71 | ffmpeg \ 72 | -i /tmp/ShadowRePlay.m3u8 \ 73 | -c copy "$FILE" -loglevel quiet && 74 | 75 | find /tmp -name 'ShadowRePlay*' -exec rm {} \; && 76 | 77 | notify-send -i video \ 78 | "ShadowRePlay" \ 79 | "Replay saved to $FILE" 80 | 81 | cd "$BASEDIR" && 82 | 83 | ./shadowreplay 84 | -------------------------------------------------------------------------------- /shadowreplay.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Achieve ShadowPlay-like functionality on Linux 3 | After=graphical.target 4 | 5 | [Service] 6 | ExecStartPre=/usr/bin/xbindkeys -f "%h/.xbindkeysrc" 7 | ExecStart=/usr/local/bin/shadowreplay 8 | #Environment=DISPLAY=:0 9 | 10 | [Install] 11 | WantedBy=multi-user.target 12 | --------------------------------------------------------------------------------