[Video as code] 화면캡처(Screen Capture)

Video As Code를 주제로 영상까지 만들려고 했던 글입니다. 오늘 ffmpeg을 이용하여 영상캡처를 할 일이 있어서 정리해서 올립니다.

1.
요즘 영상을 만들기 위해 가장 많이 사용하는 프로그램은 OBS가 아닐까 합니다. 특히 라이브방송을 할 때 많이 사용하는 듯 합니다. 제작이든 라이브방송이든 영상자료로 추가 편집을 하는 경우가 있습니다. 자막을 넣는 경우입니다. 이 때 대부분 멀티미디어를 편집할 때 GUI를 사용합니다. 멀티미디어를 생산하고 편집하는 직업이 있습니다. 편집자라고 부릅니다.

Davinchi Resolve
Adobe Premiere
Final Cut Pro

PD로 일하는 큰딸이 사용하는 프로그램이 Final Cut입니다. Final Cut이나 Adobe Premiere는 상용입니다. Davinchi Resolve도 상용이지만 공개판도 있고 기능도 훌륭합니다. 이런 도구들이 가지는 장점은 나열할 수 없을 정도로 많습니다. 결정적인 단점은 하드웨어의 성능입니다 제가 ffmpeg을 공부하면서 영상을 만드는 이유입니다.

2.
영상으로 무슨 작업을 하고자 할 때 출발점은 영상의 생산입니다. 비디오도 그렇고 오디오도 그렇습니다. 모든 분들이 스마트폰이나 웹캠을 이용하여 영상을 촬영하거나 라이브방송을 할 필요는 없습니다. 하고 싶고 할 수 있는 주제가 있으면 다양한 선택이 가능합니다. 그 중 하나가 화면캡처입니다. Screen Recording입니다. 리눅스에서 사용가능한 오픈소스가 많습니다. 제가 설치해서 시험해본 것들이 KazamSimpleScreenRecorder입니다.

조금더 높은 품질의 영상을 얻을 수 없을까 찾아보다가 ffmpeg을 확인하였습니다. 우선 ffmpeg에서 사용가능한 장비들이 무엇인지 확인하였습니다.

오디오(pulse, oss, alsa 등), 비디오(v4l2) 및 화면캡처(x11grab, kmsgrab 등)가 가능합니다. 화면캡처는 x11grab입니다.

screen capture codec (x11grab)

그리고 video4linux2는 웹캡과 관련한 부분입니다.

이제 웹캠이 아니라 화면캡처방식인인 x11grab을 이용하는 방식을 살펴봅니다. ffmpeg이 x11grab을 사용할 때 ‘-f’옵션을 사용합니다. man page를 보면 보통은 사용하지 않는다고 합니다. x11grab은 화면캡처라 명시적으로 지정해야 하네요.

-f fmt (input/output) Force input or output file format. The format is normally auto detected for input files and guessed from the file extension for output files, so this option is not needed in most cases.

참, x11grab은 리눅스에서 사용합니다. 맥os와 윈도우의 경우 다릅니다. 아래에서 확인하실 수 있습니다.

Capturing your Desktop / Screen Recording

그러면 x11grab의 옵션은 무엇이 있을까요? x11grab에서 가져왔습니다.

draw_mouse
Specify whether to draw the mouse pointer. A value of 0 specifies not to draw the pointer. Default value is 1.
follow_mouse
Make the grabbed area follow the mouse. The argument can be centered or a number of pixels PIXELS

When it is specified with “centered”, the grabbing region follows the mouse pointer and keeps the pointer at the center of region; otherwise, the region follows only when the mouse pointer reaches within PIXELS (greater than zero) to the edge of region.

For example:

To follow only when the mouse pointer reaches within 100 pixels to edge:

framerate
Set the grabbing frame rate. Default value is ntsc, corresponding to a frame rate of 30000/1001.
show_region
Show grabbed region on screen.

If show_region is specified with 1, then the grabbing region will be indicated on screen. With this option, it is easy to know what is being grabbed if only a portion of the screen is grabbed.

region_border
Set the region border thickness if ‘-show_region 1‘ is used. Range is 1 to 128 and default is 3 (XCB-based x11grab only)

For example:

With follow_mouse:

video_size
Set the video frame size. Default value is vga.
grab_x
grab_y
Set the grabbing region coordinates. They are expressed as offset from the top left corner of the X11 window and correspond to the x_offset and y_offset parameters in the device name. The default value for both options is 0.

위 옵션중 특정한 목적을 위해 많이 사용하는 것이 follow_mouse, show_region입니다. show_region을 사용한 경우입니다.

3.
그러면 x11grab을 이용하여 화면캡처를 해보도록 하겠습니다…

ffmpeg -f x11grab -i :0.0

i 옵션을 보면 :0.0으로 했습니다. 무슨 의미일까요? DISPLAY와 관련한 설명입니다.

DISPLAY NAMES

hostname:displaynumber.screennumber

This information is used by the application to determine how it should connect to the server and which screen it should use by default (on displays with multiple monitors):

hostname
The hostname specifies the name of the machine to which the display is physically connected. If the hostname is not given,the most efficient way of communicating to a server on the same machine will be used.
2.
displaynumber
The phrase “display” is usually used to refer to collection of monitors that share a common keyboard and pointer (mouse, tablet, etc.). Most workstations tend to only have one keyboard, and therefore, only one display. Larger, multi-user systems, however, frequently have several displays so that more than one person can be doing graphics work at once. To avoid confusion, each display on a machine is assigned a display number (beginning at 0) when the X server for that display is started. The display number must always be given in a display name.

screennumber
Some displays share a single keyboard and pointer among two or more monitors. Since each monitor has its own set of windows,each screen is assigned a screen number (beginning at 0) when the X server for that display is started. If the screen number is not given, screen 0 will be used.

:0.0중 앞 0은 디스플레이이름이고 뒤 0은 스크린이름입니다. 이를 확인하면 다음과 같습니다. 먼저 디스플레이이름입니다.

다음은 스크린네임입니다.

4.
한걸음더 나아가보죠. 일정한 크기의 영역을 갈무리하지 않고 화면상의 윈도우를 기준으로 하여 갈무리를 하는 방법을 없을까요? 보통 GUI프로그램의 경우 이런 기능을 지원합니다. 손쉽게 하는 방법중 하나가 windows ID를 이용하는 방법입니다.

이제 window ID를 이용하여 동영상 캡처를 하는 예제입니다.

ffmpeg -f x11grab -i :0.0 -window_id 0x180042d -vcodec libx264 output.mkv

좀더 자세한 정보는 아래에서 확인하실 수 있습니다.

Making a Video of a Single Window

이제 정리해보겠습니다. 배우면서 정리한 글입니다. 저보다 먼저 잘 정리하신 분들이 계십니다.

Screencast with FFmpeg은 친절히 설명하는 글입니다. Screen Recording Using FFmpeg X11grab & Xwininfo 은 이상의 내용을 Shell로 정리한 것입니다.

Leave a Comment

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.