Skip to content

Instantly share code, notes, and snippets.

@Garmelon
Created August 26, 2019 19:49
Show Gist options
  • Save Garmelon/13f2d7a07053862b32d84241ee7b1289 to your computer and use it in GitHub Desktop.
Save Garmelon/13f2d7a07053862b32d84241ee7b1289 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import re
import subprocess
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"output_file",
)
parser.add_argument(
"-i", "--input-fps",
help="frames per second of the input stream",
type=float,
default=1,
)
parser.add_argument(
"-o", "--output-fps",
help="frames per second of the output stream",
type=int,
default=30,
)
parser.add_argument(
"-r", "--resolution",
help="resolution of the video, defaults to '1920x1080'",
default="1920x1080",
)
parser.add_argument(
"-d", "--display-name",
help="X11 display name, of the format: 'hostname:display_number.screen_number', defaults to ':0.0'",
default=":0.0",
)
parser.add_argument(
"--big",
help="Override resolution to 1920x2130 for recording in 'big' configuration, i. e. external screen attached",
action="store_true",
)
return parser.parse_args()
def record(display_name, input_fps, output_fps, resolution, output_file):
args = ["ffmpeg"]
args.extend(["-framerate", f"{input_fps}"])
if resolution is not None:
args.extend(["-s", resolution])
args.extend(["-f", "x11grab"])
args.extend(["-i", display_name])
args.extend(["-vf", f"settb=1/{output_fps},setpts=N"])
args.extend(["-r", f"{output_fps}"])
args.extend(["-vcodec", "libx264", "-crf", "0", "-preset", "ultrafast", "-threads", "0"])
args.append(output_file)
print("Running:", args)
subprocess.run(args)
def main():
args = parse_args()
if args.big:
args.resolution = "1920x2130"
if args.resolution is not None and not re.fullmatch(r"\d+x\d+", args.resolution):
print("Resolution format incorrect")
exit(2)
if not re.fullmatch(r"\S*:\d+\.\d+", args.display_name):
print("Display name format incorrect")
exit(3)
print("Starting recording. To stop recording, just use Ctrl+C.")
try:
record(args.display_name, args.input_fps, args.output_fps, args.resolution, args.output_file)
except KeyboardInterrupt:
print()
print("Stopped recording. Goodbye!")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment