Skip to content

Instantly share code, notes, and snippets.

@bczhc
Last active September 7, 2022 06:37
Show Gist options
  • Save bczhc/cb8ceeaec3085601becfee7877aee210 to your computer and use it in GitHub Desktop.
Save bczhc/cb8ceeaec3085601becfee7877aee210 to your computer and use it in GitHub Desktop.
Linux上录屏
#!/bin/bash
# 用于把录屏方案1输出的视频转换成录屏方案2
# Codec: h264_nvenc, High H264 profile and yuv420p pixel format
ffmpeg -i "$1" -c:a copy -c:v h264_nvenc -b:v 0 -profile:v high -pix_fmt yuv420p -rc-lookahead 20 "$2"
#!/bin/bash
set -e
# if env "PAREC_DEVICE" not set, use default value "alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"
if [ -z "$PAREC_DEVICE" ]; then
echo 'PAREC_DEVICE not set, use default value "alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"' >&2
parec_device="alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"
else
parec_device="$PAREC_DEVICE"
fi
echo "$parec_device" >&2
parec --device="$parec_device" \
--channels=2 \
--rate=44100 \
--format=s16le
#!/bin/bash
# get self filename
self_name="$(basename "$0")"
function print_help() {
echo "Usage: $self_name [<option>]... [<output_file>]"
echo "Options:"
echo " -h, --help Print this help"
echo " -a, --audio Record audio from sound card"
echo " -f, --framerate <framerate> Set framerate (default: 30)"
echo " -s, --scheme <scheme> Set scheme (default: 2)"
echo
echo "Methods:"
echo " 1: cpu, libx264rgb, ultrafast preset, High 4:4:4 Predictive profile"
echo " 2: gpu, h264_nvenc, High profile, yuv420p pixel format"
exit 0
}
# loop through arguments, and check if -h or --help is presented
for arg in "$@"; do
if [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then
print_help
fi
done
record_audio=0
framerate=30
scheme=2
# loop through all options
while [[ "$1" == -* ]]; do
case "$1" in
-a | --audio)
record_audio=1
shift
;;
-f | --framerate)
framerate="$2"
shift 2
;;
-s | --scheme)
scheme="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# set output_file from argument, and if not presented, use default
if [[ -z "$1" ]]; then
output_file="$(date "+%Y-%m-%d_%H%M%S").mkv"
else
output_file="$1"
fi
# if `framerate` is not an integer, print error
if ! [[ "$framerate" =~ ^[0-9]+$ ]]; then
echo "Invalid framerate: $framerate"
exit 1
fi
# if scheme is not 1 or 2, print error
if ! [[ "$scheme" =~ ^[1-2]$ ]]; then
echo "Invalid scheme: $scheme"
exit 1
fi
# if $DISPLAY is not set, print error
if [[ -z "$DISPLAY" ]]; then
echo "\$DISPLAY env missing"
exit 1
fi
# scheme 1 and don't record audio
if [[ "$scheme" == 1 && "$record_audio" == 0 ]]; then
ffmpeg -f x11grab -framerate "$framerate" -i "$DISPLAY" \
-c:v libx264rgb \
-vf setpts=N/FR/TB -crf 0 \
-preset ultrafast \
"$output_file"
exit 0
fi
# scheme 1 and record audio
if [[ "$scheme" == 1 && "$record_audio" == 1 ]]; then
./record-audio | ffmpeg -f x11grab -framerate "$framerate" -i "$DISPLAY" \
-ar 44100 -ac 2 -f s16le -i pipe:0 \
-c:v libx264rgb \
-vf setpts=N/FR/TB -crf 0 \
-preset ultrafast \
-c:a libopus -b:a 96k \
"$output_file"
exit 0
fi
# scheme 2 and don't record audio
if [[ "$scheme" == 2 && "$record_audio" == 0 ]]; then
ffmpeg -f x11grab -framerate "$framerate" -i "$DISPLAY" \
-c:v hevc_nvenc \
-b:v 0 \
"$output_file"
exit 0
fi
# scheme 2 and record audio
if [[ "$scheme" == 2 && "$record_audio" == 1 ]]; then
./record-audio | ffmpeg -f x11grab -framerate "$framerate" -i "$DISPLAY" \
-ar 44100 -ac 2 -f s16le -i pipe:0 \
-c:v hevc_nvenc \
-b:v 0 \
-c:a libopus -b:a 96k \
"$output_file"
exit 0
fi
@bczhc
Copy link
Author

bczhc commented Apr 20, 2022

Thanks to Copilot again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment