FFmpeg视频录制(WIN)

FFmpeg视频录制(WIN)

命令

查看当前平台可用的设备

ffmpeg -devices

// 结果如下:
Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 D  dshow           DirectShow capture 	// DirectShow,是Windows平台的多媒体系统库
 D  gdigrab         GDI API Windows frame grabber
 D  lavfi           Libavfilter virtual input device
  E sdl,sdl2        SDL2 output device
 D  vfwcap          VfW video capture

查看当前平台支持的设备

ffmpeg -f dshow -list_devices true -i dummy

// 结果如下:
 DirectShow video devices (some may be both video and audio devices)
  "Integrated Camera"  // 摄像头
     Alternative name "@device_pnp_\\?\usb#vid_04f2&pid_b604&mi_00#6&31e07f50&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
 DirectShow audio devices
  "麦克风阵列 (2- Conexant SmartAudio HD)" //麦克风
     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{01E2545F-541D-4363-A995-DB47530ABA17}"

查看dshow可设置的参数

ffmpeg -h demuxer=dshow

// 结果如下:
Demuxer dshow [DirectShow capture]:
dshow indev AVOptions:
// 分辨率
  -video_size        <image_size> .D........ set video size given a string such as 640x480 or hd720.
  // 像素格式
  -pixel_format      <pix_fmt>    .D........ set video pixel format (default none)
  // 帧率(每秒采集多少帧画面)
  -framerate         <string>     .D........ set video frame rate
  // 采样率
  -sample_rate       <int>        .D........ set audio sample rate (from 0 to INT_MAX) (default 0)
  // 采样大小(位深度)
  -sample_size       <int>        .D........ set audio sample size (from 0 to 16) (default 0)
  // 声道数
  -channels          <int>        .D........ set number of audio channels, such as 1 or 2 (from 0 to INT_MAX) (default 0)
  -audio_buffer_size <int>        .D........ set audio device buffer latency size in milliseconds (default is the device's default) (from 0 to INT_MAX) (default 0)
  // true表示列出dshow支持的所有设备
  -list_devices      <boolean>    .D........ list available devices (default false)
  // true表示列出特定设备支持的所有参数
  -list_options      <boolean>    .D........ list available options for specified device (default false)
  -video_device_number <int>        .D........ set video device number for devices with same name (starts at 0) (from 0 to INT_MAX) (default 0)
  -audio_device_number <int>        .D........ set audio device number for devices with same name (starts at 0) (from 0 to INT_MAX) (default 0)
  -crossbar_video_input_pin_number <int>        .D........ set video input pin number for crossbar device (from -1 to INT_MAX) (default -1)
  -crossbar_audio_input_pin_number <int>        .D........ set audio input pin number for crossbar device (from -1 to INT_MAX) (default -1)
  -show_video_device_dialog <boolean>    .D........ display property dialog for video capture device (default false)
  -show_audio_device_dialog <boolean>    .D........ display property dialog for audio capture device (default false)
  -show_video_crossbar_connection_dialog <boolean>    .D........ display property dialog for crossbar connecting pins filter on video device (default false)
  -show_audio_crossbar_connection_dialog <boolean>    .D........ display property dialog for crossbar connecting pins filter on audio device (default false)
  -show_analog_tv_tuner_dialog <boolean>    .D........ display property dialog for analog tuner filter (default false)
  -show_analog_tv_tuner_audio_dialog <boolean>    .D........ display property dialog for analog tuner audio filter (default false)
  -audio_device_load <string>     .D........ load audio capture filter device (and properties) from file
  -audio_device_save <string>     .D........ save audio capture filter device (and properties) to file
  -video_device_load <string>     .D........ load video capture filter device (and properties) from file
  -video_device_save <string>     .D........ save video capture filter device (and properties) to file

查看摄像头支持的参数

ffmpeg -f dshow -list_options true -i video="Integrated Camera"

// 结果如下:
DirectShow video device options (from video devices)
 Pin "捕获" (alternative pin name "捕获")
  vcodec=mjpeg  min s=1280x720 fps=30 max s=1280x720 fps=30
  vcodec=mjpeg  min s=320x180 fps=30 max s=320x180 fps=30
  vcodec=mjpeg  min s=320x240 fps=30 max s=320x240 fps=30
  vcodec=mjpeg  min s=352x288 fps=30 max s=352x288 fps=30
  vcodec=mjpeg  min s=424x240 fps=30 max s=424x240 fps=30
  vcodec=mjpeg  min s=640x360 fps=30 max s=640x360 fps=30
  vcodec=mjpeg  min s=640x480 fps=30 max s=640x480 fps=30
  vcodec=mjpeg  min s=848x480 fps=30 max s=848x480 fps=30
  vcodec=mjpeg  min s=960x540 fps=30 max s=960x540 fps=30
pixel_format=yuyv422  min s=1280x720 fps=10 max s=1280x720 fps=10
pixel_format=yuyv422  min s=320x180 fps=30 max s=320x180 fps=30
pixel_format=yuyv422  min s=320x240 fps=30 max s=320x240 fps=30
pixel_format=yuyv422  min s=352x288 fps=30 max s=352x288 fps=30
pixel_format=yuyv422  min s=424x240 fps=30 max s=424x240 fps=30
pixel_format=yuyv422  min s=640x360 fps=30 max s=640x360 fps=30
pixel_format=yuyv422  min s=640x480 fps=30 max s=640x480 fps=30
pixel_format=yuyv422  min s=848x480 fps=20 max s=848x480 fps=20
pixel_format=yuyv422  min s=960x540 fps=15 max s=960x540 fps=15
pixel_format=bgr24  min s=1280x720 fps=10 max s=1280x720 fps=10
pixel_format=bgr24  min s=320x180 fps=30 max s=320x180 fps=30
pixel_format=bgr24  min s=320x240 fps=30 max s=320x240 fps=30
pixel_format=bgr24  min s=352x288 fps=30 max s=352x288 fps=30
pixel_format=bgr24  min s=424x240 fps=30 max s=424x240 fps=30
pixel_format=bgr24  min s=640x360 fps=30 max s=640x360 fps=30
pixel_format=bgr24  min s=640x480 fps=30 max s=640x480 fps=30
 pixel_format=bgr24  min s=848x480 fps=20 max s=848x480 fps=20
 pixel_format=bgr24  min s=960x540 fps=15 max s=960x540 fps=15

录制

ffmpeg -f dshow -i video="Integrated Camera" out.yuv

// 结果如下:
Input #0, dshow, from 'video=Integrated Camera':
  Duration: N/A, start: 188460.155000, bitrate: N/A
// 输入参数 yuvj422p, 1280x720, 30 fps
    Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 10000k tbn, 10000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to 'out.yuv':
  Metadata:
    encoder         : Lavf58.45.100
// 输出参数 yuvj422p, 1280x720, 30 fps
    Stream #0:0: Video: rawvideo (Y42B / 0x42323459), yuvj422p, 1280x720, q=2-31, 442368 kb/s, 30 fps, 30 tbn, 30 tbc
    Metadata:
      encoder         : Lavc58.91.100 rawvideo

自定义参数录制

ffmpeg -f dshow -video_size 640x480 -pixel_format yuyv422 -framerate 30 -i video="Integrated Camera" out.yuv

播放

ffplay -video_size 640x480 -pixel_format yuyv422 -framerate 30 out.yuv

代码

// 1.注册设备 - 一个应用只注册一次
avdevice_register_all();

qDebug() << "开始录制视频";
// 2.获取输入格式
AVInputFormat * fmt = av_find_input_format(FOMART_NAME);
if(!fmt){
    qDebug() << "获取输入格式失败 -> " << FOMART_NAME;
    return;
}
// 3.打开设备
// 格式上下文
AVFormatContext * ctx = nullptr;
// 选项
AVDictionary * options;
av_dict_set(&options,"video_size","1280x720",0);
av_dict_set(&options,"pixel_format","yuyv422",0);
av_dict_set(&options,"framerate","30",0);
int ret = avformat_open_input(&ctx,DEVICE_NAME,fmt,&options);
if(ret < 0){
    char error_buff[1024] = {0};
    av_strerror(ret,error_buff,sizeof(error_buff));
    qDebug() << "打开设备失败 -> " << error_buff;
    return;
}
// 4.采集数据
// 数据包
AVPacket * pkt = av_packet_alloc();
// 文件名
QString file_name = FILE_NAME;
file_name += QDateTime::currentDateTime().toString("yyyy_MM_dd_HH_mm_ss");
file_name += ".yuv";
QFile file(file_name);
// 文件只写 - 文件不存在就创建,存在就清空,再开始写入    
if(!file.open(QIODevice::WriteOnly)){
    qDebug() << "文件打开失败 -> " << file_name;
    avformat_close_input(&ctx);
    return;
}

// 计算每一帧的大小
AVCodecParameters *params = ctx->streams[0]->codecpar;
int imageSize = av_image_get_buffer_size(
                    (AVPixelFormat) params->format,
                    params->width, params->height,
                    1);

// 保存数据
while(!isInterruptionRequested() && av_read_frame(ctx,pkt) == 0){
    file.write((const char *)pkt->data,pkt->size);
    qDebug() << "录制视频中...";
    // 回收资源
    av_packet_unref(pkt);
}

// 5.释放资源
av_packet_free(&pkt);
file.close();
avformat_close_input(&ctx);
qDebug() << "录制视频结束";

// 6.测试out.yuv文件
// ffplay -video_size 1280x720 -pixel_format yuyv422 -framerate 30 out.yuv
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值