FFMPEG的功能强大是毋庸置疑的,那么录屏的需求大家在某些时候大家可能是非常需要的,例如,现有的项目需要演示,因此录制一段演示视频;亦或者做内容分发直播的,比如游戏主播,需要录制在玩某个游戏的精彩片段,以创建一个后期的视频素材库;亦或者通过FFMPEG抓取摄像头的实时画面,满足某些人的疯狂的偷窥欲(友情提醒:大家还是需要做一个守法公民,不要看了我的文章然后去做坏事!!!!!);亦或者遇到了某些非常好听或者好看的视频,但无法保存,此时就需要录屏功能来弥补这些遗憾了;亦或者在服务器运维的时候,需要做运维审计工作等等录屏需求
而在FFMPEG面前,这些功能的实现通常都是一条简简单单的命令就可以完成了
下面将就如何录屏,如何确保录屏的质量以及网络推流的一些基本概念做一个简单的介绍
电脑录屏指的是在一定的时间内,录制电脑上的所有活动,比如,打开网页,播放视频,播放音乐等等动作,统统都记录,最后将这些实时产生的音视频数据流写入指定的文件,这些都是本地的活动,通常电脑端的录屏活动生成的本地文件是作为视频素材库使用的,也就是说后期还需要通过音视频剪辑工具按需裁剪,最后按需求分发到网络或者保留在本地自己欣赏
网络推流指的是通过特定软件比如利用摄像头实时捕捉音视频数据流,将这些数据主动推送到远程的指定的网络端,一般情况下,直播,实时摄像头这些最终都是网络推流,本地是不保存音视频数据的(后台本地有时候可能会保存,毕竟直播什么的有时候还需要一些审计,或者比如交通摄像头,事故发生的时候可能需要视频回溯)
网络推流和拉流需要一些相应的网络协议支持,比如rmtp协议,这里就不展开说了
这里说的基本录屏是指的Windows10,win7,或者win11 ,无需其它驱动,基于Windows操作系统,直接录屏,但录制的只有视频,没有音频
无疑的,这样的方式是比较简单的,但缺点也是比较大的:没有声音,只能录制Windows内的整个桌面
命令如下:
fmpeg -f gdigrab -i desktop -f mp4 d:/out.mp4 -y
注意,这个命令生成的out.mp4文件可能Windows media可能打不开,需要使用vlc播放器播放
输出支持的格式也可以是wma,flv 按q退出录屏
一般不需要特别设置,比如视频的比特率什么的,直接默认就可以了,在现在的硬件条件下,清晰度基本可以满足,录屏主要是CPU运算
注意:经过实践,在本次实验中,mp4格式的视频清晰度最高,效果最好
ffmpeg -f gdigrab -i desktop d:/out.flv -y
ffmpeg -f gdigrab -i desktop d:/out.wma -y
,这个基本录屏只能录制视频,有点不太能满足需求,比如翻录某些视频的情况下,下面就来一个进阶录屏:可以同时录制高清视频和高清音频
此种方式需要安装虚拟屏幕驱动并启用声卡录音功能
常用的驱动如UScreenCapture,screen-capture-recorder
本例中使用screen-capture-recorder,该驱动程序下载地址:http://sourceforge.net/projects/screencapturer/files/
###注意,该驱动更新频率比较低,直接选择最新版本就可以了,本例使用的是Setup Screen Capturer Recorder v0.12.11
此程序默认安装就可以了,安装完毕后,会有一个新的FFMPEG,这个FFMPEG版本是比较低的,但后面的录屏还只能用这个低版本的FFMPEG,高版本的FFMPEG好像用不了:
查询dshow驱动执行情况:
ffmpeg -list_devices true -f dshow -i dummy
旧新版本的FFMPEG查询现在支持的录屏驱动,新的FFMPEG可能是不支持的,现在暂时还没有办法(第一个命令是旧版FFMPEG,第二个命令是新版FFMPEG):
C:\Users\Administrator>"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -list_devices true -f dshow -i dummy
ffmpeg version N-44080-gdfb4757 Copyright (c) 2000-2012 the FFmpeg developers
built on Aug 31 2012 16:58:51 with gcc 4.7.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runtime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 51. 70.100 / 51. 70.100
libavcodec 54. 55.100 / 54. 55.100
libavformat 54. 25.104 / 54. 25.104
libavdevice 54. 2.100 / 54. 2.100
libavfilter 3. 15.101 / 3. 15.101
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
libpostproc 52. 0.100 / 52. 0.100
[dshow @ 0292bc80] DirectShow video devices
[dshow @ 0292bc80] "screen-capture-recorder"
[dshow @ 0292bc80] DirectShow audio devices
[dshow @ 0292bc80] "楹﹀厠椋?(Realtek(R) Audio)"
[dshow @ 0292bc80] "virtual-audio-capturer"
[dshow @ 0292bc80] "绔嬩綋澹版贩闊?(Realtek(R) Audio)"
dummy: Immediate exit requested
C:\Users\Administrator>ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version 6.1.1-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --pkg-config=pkgconf --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 58. 29.100 / 58. 29.100
libavcodec 60. 31.102 / 60. 31.102
libavformat 60. 16.100 / 60. 16.100
libavdevice 60. 3.100 / 60. 3.100
libavfilter 9. 12.100 / 9. 12.100
libswscale 7. 5.100 / 7. 5.100
libswresample 4. 12.100 / 4. 12.100
libpostproc 57. 3.100 / 57. 3.100
[dshow @ 000001d0b92ba200] "UScreenCapture" (video)
[dshow @ 000001d0b92ba200] Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\UScreenCapture"
[dshow @ 000001d0b92ba200] "screen-capture-recorder" (video)
[dshow @ 000001d0b92ba200] Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\{4EA69364-2C8A-4AE6-A561-56E4B5044439}"
[dshow @ 000001d0b92ba200] "麦克风 (Realtek(R) Audio)" (audio)
[dshow @ 000001d0b92ba200] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{9A0CE0E2-9E7D-4F81-857D-CF4BA1D888E3}"
[dshow @ 000001d0b92ba200] "virtual-audio-capturer" (audio)
[dshow @ 000001d0b92ba200] Alternative name "@device_sw_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\{8E146464-DB61-4309-AFA1-3578E927E935}"
[dshow @ 000001d0b92ba200] "立体声混音 (Realtek(R) Audio)" (audio)
[dshow @ 000001d0b92ba200] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{E3B2D1FC-6202-4AFE-AC1C-2BD8DD95EB72}"
[in#0 @ 000001d0b92ba080] Error opening input: Immediate exit requested
Error opening input file dummy.
Setup Screen Capturer Recorder v0.12.11安装完毕后,不需要重启服务器,立刻就生效了
这个时候还需要安装Java环境,jdk下载地址:https://www.java.com/en/download/ ,如何安装jdk就不用说了,有手就可以安装了,没办法jdk是Setup Screen Capturer Recorder v0.12.11的强依赖
还需要打开Windows的录制功能:
,现在可以开始录制带音频的录屏文件了:
"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -f dshow -rtbufsize 1202000k -i video="screen-capture-recorder":audio="virtual-audio-capturer" -sameq -ar 22050 d:\out1.flv -y
"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -f dshow -rtbufsize 1202000k -i video="screen-capture-recorder":audio="virtual-audio-capturer" -sameq -ar 22050 d:\out1.wma -y
指定输出格式为mp4的时候报错了,报错如下:
看样子mp4编码转码的时候失败了,也就是说该驱动不支持mp4,或者和操作系统有关吧???
C:\Users\Administrator>"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -f dshow -rtbufsize 1202000k -i video="screen-capture-recorder":audio="virtual-audio-capturer" -sameq -ar 22050 -f mp4 d:\out1.mp4 -y
ffmpeg version N-44080-gdfb4757 Copyright (c) 2000-2012 the FFmpeg developers
built on Aug 31 2012 16:58:51 with gcc 4.7.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runtime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 51. 70.100 / 51. 70.100
libavcodec 54. 55.100 / 54. 55.100
libavformat 54. 25.104 / 54. 25.104
libavdevice 54. 2.100 / 54. 2.100
libavfilter 3. 15.101 / 3. 15.101
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
libpostproc 52. 0.100 / 52. 0.100
leaving aero on[dshow @ 029cbe40] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.1 : stereo
Input #0, dshow, from 'video=screen-capture-recorder:audio=virtual-audio-capturer':
Duration: N/A, start: 30738.314000, bitrate: 1536 kb/s
Stream #0:0: Video: rawvideo, bgra, 2560x1440, 30 tbr, 10000k tbn, 30 tbc
Stream #0:1: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
[libx264 @ 04065b40] using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.2 AVX AVX2 FMA3 BMI1 BMI2
[libx264 @ 04065b40] profile High, level 5.0
[libx264 @ 04065b40] 264 - core 125 r2208 d9d2288 - H.264/MPEG-4 AVC codec - Copyleft 2003-2012 - 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=24 lookahead_threads=4 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 'd:\out1.mp4':
Metadata:
encoder : Lavf54.25.104
Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 2560x1440, q=-1--1, 10000k tbn, 30 tbc
Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 22050 Hz, stereo, s16, 128 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo -> libx264)
Stream #0:1 -> #0:1 (pcm_s16le -> libvo_aacenc)
Press [q] to stop, [?] for help
x264 [error]: malloc of size 15786496 failedime=00:00:27.74 bitrate= 109.3kbits/s dup=3 drop=0
Video encoding failed
Received stop event after 2795 passes
换成flv格式的输出文件,第二次在执行录屏的时候报错了:
[dshow @ 02a2be40] real-time buffer 100% full! frame dropped!
这个报错说的是buffer溢出了,根本原因是输出的文件里有内容了,录制的视频不知道如何把数据流写到文件的哪个位置了,很简单,换一个输出文件名或者把原来的输出文件删除就可以继续录制了
C:\Users\Administrator>"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -f dshow -rtbufsize 1202000k -i video="screen-capture-recorder":audio="virtual-audio-capturer" -sameq -ar 22050 -vol 200 d:\out1.flv -y
ffmpeg version N-44080-gdfb4757 Copyright (c) 2000-2012 the FFmpeg developers
built on Aug 31 2012 16:58:51 with gcc 4.7.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runtime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 51. 70.100 / 51. 70.100
libavcodec 54. 55.100 / 54. 55.100
libavformat 54. 25.104 / 54. 25.104
libavdevice 54. 2.100 / 54. 2.100
libavfilter 3. 15.101 / 3. 15.101
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
libpostproc 52. 0.100 / 52. 0.100
leaving aero on[dshow @ 02a2be40] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.1 : stereo
Input #0, dshow, from 'video=screen-capture-recorder:audio=virtual-audio-capturer':
Duration: N/A, start: 31001.957000, bitrate: 1536 kb/s
Stream #0:0: Video: rawvideo, bgra, 2560x1440, 30 tbr, 10000k tbn, 30 tbc
Stream #0:1: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
File 'd:\out1.flv' already exists. Overwrite ? [y/N] real-time buffer 63% full! frame dropped!
[dshow @ 02a2be40] real-time buffer 66% full! frame dropped!
[dshow @ 02a2be40] real-time buffer 68% full! frame dropped!
[dshow @ 02a2be40] real-time buffer 71% full! frame dropped!
[dshow @ 02a2be40] real-time buffer 73% full! frame dropped!
[dshow @ 02a2be40] real-time buffer 76% full! frame dropped!
Last message repeated 1 times
[dshow @ 02a2be40] real-time buffer 78% full! frame dropped!
Last message repeated 1 times
[dshow @ 02a2be40] real-time buffer 80% full! frame dropped!
Last message repeated 19 times
[dshow @ 02a2be40] real-time buffer 83% full! frame dropped!
Last message repeated 1 times
[dshow @ 02a2be40] real-time buffer 85% full! frame dropped!
Last message repeated 1 times
[dshow @ 02a2be40] real-time buffer 88% full! frame dropped!
Last message repeated 2 times
[dshow @ 02a2be40] real-time buffer 90% full! frame dropped!
Last message repeated 2 times
[dshow @ 02a2be40] real-time buffer 93% full! frame dropped!
Last message repeated 2 times
[dshow @ 02a2be40] real-time buffer 95% full! frame dropped!
Last message repeated 32 times
[dshow @ 02a2be40] real-time buffer 98% full! frame dropped!
Last message repeated 2 times
[dshow @ 02a2be40] real-time buffer 100% full! frame dropped!
录制好的视频播放后,可以看到确实录制到声音了,我的QQ音乐播放的歌曲完全录制进视频内了,开心…………^……^
那么,到这里就很清楚了,如果只需要录制视频,那就把audio="virtual-audio-capturer"这一段删除,反之亦然!!!!!
上面的录屏参数说明:
"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -f dshow -rtbufsize 1202000k -i video="screen-capture-recorder":audio="virtual-audio-capturer" -sameq -ar 22050 -vol 200 -s 1920*1440 d:\out1.flv -y
但这样的参数位置将会报错,需要特别注意哦:
C:\Users\Administrator>"C:\Program Files (x86)\Screen Capturer Recorder\configuration_setup_utility\vendor\ffmpeg\bin\ffmpeg.exe" -f dshow -rtbufsize 1202000k -ar 22050 -i video="screen-capture-recorder":audio="virtual-audio-capturer" -sameq -vol 200 -s 1920*1440 d:\out1.flv -y
ffmpeg version N-44080-gdfb4757 Copyright (c) 2000-2012 the FFmpeg developers
built on Aug 31 2012 16:58:51 with gcc 4.7.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-pthreads --enable-runtime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 51. 70.100 / 51. 70.100
libavcodec 54. 55.100 / 54. 55.100
libavformat 54. 25.104 / 54. 25.104
libavdevice 54. 2.100 / 54. 2.100
libavfilter 3. 15.101 / 3. 15.101
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
libpostproc 52. 0.100 / 52. 0.100
leaving aero on[dshow @ 0286bf20] Could not set audio options
video=screen-capture-recorder:audio=virtual-audio-capturer: Input/output error