音视频-ffmpeg命令行录制和播放视频

查看支持设备

Win平台
ffmpeg -f dshow -list_devices true -I dummy

因为Win没有额外的摄像头, 用Mac

Mac平台
ffmpeg -f avfoundation -list_devices true -I ''

输出

ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_3 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[AVFoundation indev @ 0x7fcae5f26180] AVFoundation video devices:
[AVFoundation indev @ 0x7fcae5f26180] [0] FaceTime HD Camera
[AVFoundation indev @ 0x7fcae5f26180] [1] Capture screen 0
[AVFoundation indev @ 0x7fcae5f26180] AVFoundation audio devices:
[AVFoundation indev @ 0x7fcae5f26180] [0] Built-in Microphone

需要用到的就是: [AVFoundation indev ...] [0] FaceTime HD Camera

Mac查看支持的参数 :

ffmpeg -h demuxer=avfoundation

输出

Demuxer avfoundation [AVFoundation input device]:
AVFoundation indev AVOptions:
  -list_devices          .D........ list available devices (default false)
  -video_device_index         .D........ select video device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
  -audio_device_index         .D........ select audio device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
  -pixel_format          .D........ set pixel format (default yuv420p)
  -framerate          .D........ set frame rate (default "ntsc")
  -video_size         .D........ set video size
  -capture_cursor        .D........ capture the screen cursor (default false)
  -capture_mouse_clicks     .D........ capture the screen mouse clicks (default false)
  -capture_raw_data      .D........ capture the raw data from device connection (default false)
  -drop_late_frames      .D........ drop frames that are available later than expected (default true)

主要

  -list_devices          .D........ 支持的所有设备 (default false)
  -pixel_format          .D........  像素格式 (default yuv420p)
  -framerate          .D........ 帧率 (default "ntsc")
  -video_size         .D........ 视频大小

关于ntsc

ntsc = 30000 / 1001 ≈ 29.970030

Mac下ffmpeg录制

ffmpeg -f avfoundation -framerate 30 -i 0 out.yuv

和音频录制差不多.对比音频录制的参数

ffmpeg -f avfoundation -i :0 out.wav

录制完毕之后文件大小

Input #0, avfoundation, from '0':
  Duration: N/A, start: 55870.407567, bitrate: N/A
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1280x720, 30 tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to 'out.yuv':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1280x720, q=2-31, 442368 kb/s, 30 fps, 30 tbn, 30 tbc
    Metadata:
      encoder         : Lavc58.91.100 rawvideo
frame=  170 fps= 30 q=-0.0 Lsize=  306000kB time=00:00:05.66 bitrate=442368.0kbits/s dup=1 drop=0 speed=1.02x    
video:306000kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

主要看输出信息 Output #0
-pixel_format : 像素格式 uyvy422
-framerate : 帧率 30
-video_size : 视频大小1280x720

持续时间5.66秒, 总文件大小达到了313.3MB.

UYVY422数据格式排列

U Y V Y U Y V Y
U Y V Y U Y V Y

一个像素存储信息UYVY422, 占用 : 16位, 2个字节


一帧大小 : 1280x720 = 921600
一帧总像素大小: 1280x720 * 2 = 1843200
一秒30帧 : 1280x720 * 2 * 30 = 55296000
持续5.66秒 : 1280x720 * 2 * 30 * 5.66 = 312975360 = 312,975,360


从文件查看上来看是 : 313,344,000. 用 313,344,000 / 55296000 ≈ 5.66666666666667. 差不多, 理论计算时间5.66, 是时间精度丢失了.

image.png

播放视频

ffplay -video_size 1280x720 -pixel_format uyvy422 -framerate 30 out.yuv 

实际播放是没有问题

这里可以通过改变-framerate 30 来实现快进与放慢播放

你可能感兴趣的:(音视频-ffmpeg命令行录制和播放视频)