How do you do command-line video screen capture on OS X with libav?
I found a solution that works really well. You're able to record with ffmpeg
using an input device called avfoundation
.
Details
More options
ffmpeg options
Here is an example command-line:
ffmpeg -f avfoundation -pix_fmt yuyv422 -i "1:1" -t 10 -vf crop=1280:720:1:65 -r 30 output.mkv
You may or may not get asked to specify
-pix_fmt
when this option is not included; it might be worth trying other formats to see if you get better performance.-
As explained in the details link,
-i
selects both video and audio input. If you want to capture audio playing on your computer, you'll need something like iShowU Audio Capture.To get that to work, you'll need to press Option + F11, select iShowU Audio Capture as an input device and as an output device. You'll notice that once you select it as an output device that you can no longer hear the audio yourself through your speakers or headphones.
A quick fix is opening Audio MIDI Setup, create a Multi-Output Device and adding iShowU Audio Capture and your normal output device. Select this new Multi-Output device in your sound outputs and there ya go, you can now hear the audio and have it captured.
Unfortunately you aren't able to control the audio levels now...if someone knows a solution to this please let me know.
-t
is the time in seconds to record.-vf
is very useful for cropping and offsetting. The current setting is cropped to 1280 width 720 height, offsetting 1 pixel to the left and 65 pixels from the top. You'll need to use a third party tool to get exact pixel measurements of your screen.-r
is framerate.
Note: This answer doesn’t use libav
but this question comes up first when searching for “mac screen record command line” in Google.
As of macOS Catalina, you can now record screen from your command line using the screencapture
tool with -v
flag.
See the help page (screencapture --help
) for a description of all options. The options listed in the man
page may be incorrect.
Meanwhile, here are some examples:
-
Record your main screen until you hit Ctrl+C in Terminal. There is no stop button at the the menu bar.
screencapture -v out.mov
-
Use
-D 2
to use a secondary screen.screencapture -D 2 -v out.mov
-
Use
-g
to also record audio using the default input. (The man page stay-A
but it did not work.)screencapture -v -g out.mov
Some general notes:
If the file already exists, recording will fail after the screen recording is completed. Please beware.
There is a little bit of delay after recording has started before it actually records your screen.
-i
(interactive mode) does not work with-v
.-iU
(interactive toolbar mode) works with-v
, but it will not use the filename specified on the command line.
For taking screenshots (not movies) there is screencapture
:
pse@Mithos:~$ screencapture -h
screencapture: illegal option -- h
usage: screencapture [-icMPmwsWxSCUtoa] [files]
-c force screen capture to go to the clipboard
-C capture the cursor as well as the screen. only in non-interactive modes
-d display errors to the user graphically
-i capture screen interactively, by selection or window
control key - causes screen shot to go to clipboard
space key - toggle between mouse selection and
window selection modes
escape key - cancels interactive screen shot
-m only capture the main monitor, undefined if -i is set
-M screen capture output will go to a new Mail message
-o in window capture mode, do not capture the shadow of the window
-P screen capture output will open in Preview
-s only allow mouse selection mode
-S in window capture mode, capture the screen not the window
-t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats)
-T<seconds> Take the picture after a delay of <seconds>, default is 5
-w only allow window selection mode
-W start interaction in window selection mode
-x do not play sounds
-a do not include windows attached to selected windows
-r do not add dpi meta data to image
-l<windowid> capture this windowsid
-R<x,y,w,h> capture screen rect
-B<bundleid> screen capture output will open in app with bundleidBS
files where to save the screen capture, 1 file per screen
For capturing video, the easiest way is to use the Screen Capture option in Quicktime Player. It even should be possible to initiate this from Terminal with some Applescript (which is beyond my skills).