FFmpeg 3.3.4 avfoundation to record only a specifi

2020-02-11 12:56发布

问题:

I'm using FFmpeg version 3.3.4 (installed via brew) through the command line to record my desktop during some automated tests on my macOS Sierra (10.12.6).

I would like to record only a specific section of the screen but, unfortunately, I'm not able to find the proper options in the documentation to reach my goal.

According to the FFmpeg documentation for avfoundation, this is the command I use:

ffmpeg 
-f avfoundation    // avfoundation
-pix_fmt uyvy422   // pixel format
-i 1               // input: desktop
-r 30              // framerate for output
[PATH]/test.mp4      // filename

When I run this command I have the following output:

ffmpeg version 3.3.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.37)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.4 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-ffplay --enable-frei0r --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --enable-openssl --disable-lzma --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags=-I/usr/local/Cellar/openjpeg/2.3.0/include/openjpeg-2.3 --enable-nonfree --enable-vda
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
[avfoundation @ 0x7fb7d4005800] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '1':
  Duration: N/A, start: 7263.782833, bitrate: N/A
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1920x1080, 1000k tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Press [q] to stop, [?] for help
No pixel format specified, yuv422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x7fb7d4028400] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x7fb7d4028400] profile High 4:2:2, level 4.0, 4:2:2 8-bit
[libx264 @ 0x7fb7d4028400] 264 - core 148 r2795 aaa9aa8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '[PATH]/test.mp4':
  Metadata:
    encoder         : Lavf57.71.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv422p, 1920x1080, q=-1--1, 30 fps, 15360 tbn, 30 tbc
    Metadata:
      encoder         : Lavc57.89.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=   83 fps= 28 q=-1.0 Lsize=     525kB time=00:00:02.66 bitrate=1612.6kbits/s dup=1 drop=0 speed=0.889x

I tried to use the -video_size option to set the video frame size, but it doesn't work.

回答1:

First you need to find the name of the AVFoundation device that corresponds to your screen:

ffmpeg -hide_banner -f avfoundation -list_devices true -i ""

[AVFoundation input device @ 0x7fc1cd40ec60] AVFoundation video devices:
[AVFoundation input device @ 0x7fc1cd40ec60] [0] HD Pro Webcam C920
[AVFoundation input device @ 0x7fc1cd40ec60] [1] FaceTime HD Camera (Built-in)
[AVFoundation input device @ 0x7fc1cd40ec60] [2] Capture screen 0
[AVFoundation input device @ 0x7fc1cd40ec60] AVFoundation audio devices:
[AVFoundation input device @ 0x7fc1cd40ec60] [0] HD Pro Webcam C920
[AVFoundation input device @ 0x7fc1cd40ec60] [1] Built-in Microphone

So my screen is device [2] because I have two cameras attached and they are devices [0] and [1].

Then, as you are on a Mac, you will probably want to use QuickTime to view videos, so you need to ensure you make videos that are compatible - that means using -pix_fmt yuv420p.

ffmpeg -f avfoundation -i "2:0" -vf  "crop=1024:768:400:800" -pix_fmt yuv420p -y -r 10 out.mov

The 1024x768 are the width and height of the video, and the 400:800 are the offset from top-left of screen that I want to record from.