Last active
April 19, 2017 17:30
-
-
Save noonien/b4e15b9e43b964f06a743ce64fa9cf89 to your computer and use it in GitHub Desktop.
Record window, encode and upload
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
#!/usr/bin/env python3 | |
import argparse | |
import subprocess | |
import time | |
import os | |
import re | |
parser = argparse.ArgumentParser(description='Record window') | |
parser.add_argument('-window-id', type=int) | |
parser.add_argument('-fps', type=int, default=30) | |
parser.add_argument('-sound', type=str, choices=['mic', 'desktop', 'both']) | |
def _shell_out(cmd): | |
return subprocess.check_output(cmd, shell=True).decode('utf-8') | |
def get_window_id(): | |
print("click on the window you want to record") | |
return int(_shell_out('xwininfo -int | grep "Window id" | cut -d\ -f4')) | |
def get_window_geometry(id): | |
gm_str = _shell_out('xwininfo -id %d | grep -P "(geometry|Corners)"' % id).strip() | |
lines = gm_str.split('\n') | |
pos = [int(p) for p in re.split(r'\s+', lines[0])[1].strip('+').split('+')] | |
size = [int(p) for p in re.search(r'\d+x\d+', lines[1]).group(0).split('x')] | |
return (size[0], size[1], pos[0], pos[1]) | |
def record(args, geometry): | |
print('recording @ %d fps, sound: %r' % (args.fps, args.sound)) | |
output = os.path.expanduser('~/.cache/rec_%d_%d.mp4' % (args.window_id, time.time())) | |
print('output: %s' % output) | |
cmd = [ | |
'ffmpeg', '-video_size', '%dx%d' % geometry[:2], | |
'-framerate', str(args.fps), | |
'-f', 'x11grab', '-i', '%s+%d,%d' % (os.environ['DISPLAY'], geometry[2], geometry[3]) | |
] | |
if args.sound: | |
if args.sound == 'mic' or args.sound == 'both': | |
mic_src = _shell_out('pacmd stat | grep source | cut -d: -f2').strip() | |
cmd.extend(['-f', 'pulse', '-i', mic_src]) | |
if args.sound == 'desktop' or args.sound == 'both': | |
desktop_src = _shell_out('pacmd stat | grep sink | cut -d: -f2').strip() | |
cmd.extend(['-f', 'pulse', '-i', desktop_src+'.monitor']) | |
if args.sound == 'both': | |
cmd.extend(['-filter_complex', 'amerge']) | |
cmd.extend(['-ac', '2']) | |
cmd.append(output) | |
subprocess.call(cmd) | |
return output | |
def upload(args, input): | |
print('uploading %s' % input) | |
subprocess.call(['curl', '--upload-file', input, 'https://transfer.sh']) | |
os.remove(input) | |
def main(): | |
args = parser.parse_args() | |
if args.window_id is None: | |
args.window_id = get_window_id() | |
print('window id: %d' % args.window_id) | |
geometry = get_window_geometry(args.window_id) | |
print('window geometry: %dx%d @ %dx%d' % geometry) | |
output = record(args, geometry) | |
upload(args, output) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment