OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播

参考博客

http://blog.chinaunix.net/uid-11344913-id-4665455.html

 

在osx系统下通过ffmpeg查看设备

ffmpeg -f avfoundation -list_devices true -i ""

 显示结果如下:

SingerdeMacBook-Pro:~ Singer$ ffmpeg -f avfoundation -list_devices true -i ""
ffmpeg version 3.0 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 7.0.2 (clang-700.1.81)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.0 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --enable-vda
  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
[AVFoundation input device @ 0x7f9a2bc1b6e0] AVFoundation video devices:
[AVFoundation input device @ 0x7f9a2bc1b6e0] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7f9a2bc1b6e0] [1] Capture screen 0
[AVFoundation input device @ 0x7f9a2bc1b6e0] AVFoundation audio devices:
[AVFoundation input device @ 0x7f9a2bc1b6e0] [0] Built-in Microphone
: Input/output error

 从上面我们可以看到设备如下:

AVFoundation video devices:
[0] FaceTime HD Camera
[1] Capture screen 0
AVFoundation audio devices:
[0] Built-in Microphone

如果希望将桌面录制或者分享,可以使用命令行如下:

ffmpeg -f avfoundation -i "1" -vcodec libx264 -preset ultrafast -acodec libfaac -f flv rtmp://192.168.1.105:1935/live1/room1

 如果需要桌面+麦克风,比如一般做远程教育分享ppt或者桌面,有音频讲解 命令行如下:

ffmpeg -f avfoundation -i "1:0" -vcodec libx264 -preset ultrafast -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://192.168.1.105:1935/live1/room1

如果需要桌面+麦克风,并且还要摄像头拍摄到自己,比如一般用于互动主播,游戏主播,命令行如下

ffmpeg -f avfoundation -framerate 30 -i "1:0" \
-f avfoundation -framerate 30 -video_size 640x480 -i "0" \
-c:v libx264 -preset ultrafast \
-filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1  -f flv rtmp://192.168.1.105:1935/live1/room1

 然后你就可以用过支持rtmp协议的播放软件(例如VCL播放器)测试观看了

最终效果

 

 

 目前用命令录屏推流 我测试的时候遇到一点问题,以上几个命令 在推了一段时间后会报一个错

av_interleaved_write_frame(): Broken pipe

我也刚开始接触ffmpeg,不知道怎么回事,如果有知道的大神,还请在评论中回复,指点下~。

服务器用的是nginx的rtmp模块搭建的,不知道是否还需要做额外配置)谢谢

ffmpeg -f avfoundation -framerate 30 -i "1:0" \
> -f avfoundation -framerate 30 -video_size 640x480 -i "0" \
> -c:v libx264 -preset ultrafast \
> -filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1  -f flv rtmp://192.168.1.105:1935/live1/room1
ffmpeg version 3.0 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 7.0.2 (clang-700.1.81)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.0 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --enable-vda
  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
[avfoundation @ 0x7fc549000400] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7fc549000400] Supported pixel formats:
[avfoundation @ 0x7fc549000400]   uyvy422
[avfoundation @ 0x7fc549000400]   yuyv422
[avfoundation @ 0x7fc549000400]   nv12
[avfoundation @ 0x7fc549000400]   0rgb
[avfoundation @ 0x7fc549000400]   bgr0
[avfoundation @ 0x7fc549000400] Overriding selected pixel format to use uyvy422 instead.
[avfoundation @ 0x7fc549000400] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '1:0':
  Duration: N/A, start: 21897.641167, bitrate: 2822 kb/s
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 2880x1800, 1000k tbr, 1000k tbn, 1000k tbc
    Stream #0:1: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
[avfoundation @ 0x7fc54a086800] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7fc54a086800] Supported pixel formats:
[avfoundation @ 0x7fc54a086800]   uyvy422
[avfoundation @ 0x7fc54a086800]   yuyv422
[avfoundation @ 0x7fc54a086800]   nv12
[avfoundation @ 0x7fc54a086800]   0rgb
[avfoundation @ 0x7fc54a086800]   bgr0
[avfoundation @ 0x7fc54a086800] Overriding selected pixel format to use uyvy422 instead.
Input #1, avfoundation, from '0':
  Duration: N/A, start: 21898.372233, bitrate: N/A
    Stream #1:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 640x480, 29.97 tbr, 1000k tbn, 1000k tbc
[libx264 @ 0x7fc54995de00] MB rate (20340000000) > level limit (2073600)
[libx264 @ 0x7fc54995de00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x7fc54995de00] profile Constrained Baseline, level 5.2
[libx264 @ 0x7fc54995de00] 264 - core 148 r2601 a0cd7d3 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, flv, to 'rtmp://192.168.1.105:1935/live1/room1':
  Metadata:
    encoder         : Lavf57.25.100
    Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 2880x1800, q=-1--1, 1000k fps, 1k tbn, 1000k tbc (default)
    Metadata:
      encoder         : Lavc57.24.102 libx264
    Side data:
      unknown side data type 10 (24 bytes)
    Stream #0:1: Audio: mp3 (libmp3lame) ([2][0][0][0] / 0x0002), 44100 Hz, mono, fltp
    Metadata:
      encoder         : Lavc57.24.102 libmp3lame
Stream mapping:
  Stream #0:0 (rawvideo) -> overlay:main (graph 0)
  Stream #1:0 (rawvideo) -> overlay:overlay (graph 0)
  overlay (graph 0) -> Stream #0:0 (libx264)
  Stream #0:1 -> #0:1 (pcm_f32le (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
[avfoundation @ 0x7fc54a086800] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
frame=    1 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   [avfoundation @ 0x7fc549000400] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
frame=    5 fps=5.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   frame=    6 fps=4.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   frame=    9 fps=4.5 q=0.0 size=       0kB time=00:00:01.11 bitrate=   2.7kbits/sframe=   14 fps=5.6 q=19.0 size=     676kB time=00:00:01.86 bitrate=2964.4kbits/frame=   16 fps=5.3 q=17.0 size=     945kB time=00:00:01.86 bitrate=4143.2kbits/frame=   20 fps=5.7 q=19.0 size=    1659kB time=00:00:01.92 bitrate=7074.0kbits/frame=   26 fps=6.4 q=19.0 size=    2386kB time=00:00:02.69 bitrate=7257.9kbits/frame=   29 fps=6.4 q=21.0 size=    2718kB time=00:00:03.41 bitrate=6520.5kbits/frame=   32 fps=6.3 q=18.0 size=    2907kB time=00:00:03.41 bitrate=6973.6kbits/frame=   38 fps=6.8 q=17.0 size=    3503kB time=00:00:04.16 bitrate=6891.4kbits/frame=   41 fps=6.8 q=20.0 size=    3874kB time=00:00:04.95 bitrate=6399.7kbits/frame=   43 fps=6.5 q=18.0 size=    4013kB time=00:00:04.95 bitrate=6629.3kbits/frame=   49 fps=6.9 q=18.0 size=    4502kB time=00:00:06.25 bitrate=5894.7kbits/frame=   53 fps=7.0 q=21.0 size=    5001kB time=00:00:06.46 bitrate=6332.6kbits/frame=   57 fps=7.1 q=18.0 size=    5438kB time=00:00:07.07 bitrate=6296.3kbits/frame=   64 fps=7.5 q=18.0 size=    6019kB time=00:00:07.82 bitrate=6301.4kbits/frame=   65 fps=7.2 q=22.0 size=    6135kB time=00:00:08.00 bitrate=6281.8kbits/frame=   69 fps=7.2 q=20.0 size=    6386kB time=00:00:08.63 bitrate=6061.0kbits/frame=   74 fps=7.3 q=19.0 size=    6822kB time=00:00:08.79 bitrate=6351.1kbits/frame=   75 fps=7.1 q=23.0 size=    6877kB time=00:00:08.79 bitrate=6402.7kbits/frame=   80 fps=7.2 q=18.0 size=    7259kB time=00:00:09.54 bitrate=6228.4kbits/frame=   86 fps=7.4 q=18.0 size=    7776kB time=00:00:10.25 bitrate=6213.4kbits/frame=   87 fps=7.2 q=22.0 size=    7843kB time=00:00:10.95 bitrate=5867.4kbits/frame=   92 fps=7.3 q=18.0 size=    8121kB time=00:00:11.01 bitrate=6042.1kbits/frame=   98 fps=7.5 q=18.0 size=    8698kB time=00:00:11.78 bitrate=6047.1kbits/frame=  100 fps=7.3 q=17.0 size=    8898kB time=00:00:12.52 bitrate=5817.7kbits/frame=  102 fps=7.2 q=20.0 size=    9030kB time=00:00:13.30 bitrate=5561.6kbits/frame=  106 fps=7.2 q=20.0 size=    9411kB time=00:00:13.87 bitrate=5555.8kbits/frame=  108 fps=7.1 q=20.0 size=    9557kB time=00:00:14.10 bitrate=5551.7kbits/frame=  111 fps=7.1 q=20.0 size=    9877kB time=00:00:14.65 bitrate=5522.7kbits/frame=  116 fps=7.2 q=19.0 size=   10305kB time=00:00:15.40 bitrate=5481.7kbits/frame=  119 fps=7.1 q=22.0 size=   10536kB time=00:00:15.43 bitrate=5591.1kbits/frame=  122 fps=7.1 q=19.0 size=   10822kB time=00:00:15.61 bitrate=5677.1kbits/frame=  129 fps=7.3 q=21.0 size=   11436kB time=00:00:16.36 bitrate=5725.4kbits/frame=  133 fps=7.3 q=21.0 size=   11816kB time=00:00:17.14 bitrate=5645.2kbits/frame=  136 fps=7.3 q=18.0 size=   12159kB time=00:00:17.90 bitrate=5563.2kbits/frame=  141 fps=7.3 q=17.0 size=   12530kB time=00:00:18.64 bitrate=5506.2kbits/frame=  144 fps=7.3 q=21.0 size=   12835kB time=00:00:18.66 bitrate=5632.2kbits/frame=  146 fps=7.2 q=17.0 size=   12984kB time=00:00:19.13 bitrate=5558.5kbits/frame=  153 fps=7.4 q=18.0 size=   13696kB time=00:00:19.42 bitrate=5777.3kbits/frame=  156 fps=7.3 q=21.0 size=   14008kB time=00:00:19.92 bitrate=5760.2kbits/frame=  160 fps=7.4 q=18.0 size=   14397kB time=00:00:20.67 bitrate=5705.4kbits/frame=  167 fps=7.5 q=16.0 size=   15053kB time=00:00:22.23 bitrate=5545.9kbits/frame=  169 fps=7.4 q=18.0 size=   15205kB time=00:00:22.23 bitrate=5601.6kbits/frame=  172 fps=7.4 q=17.0 size=   15421kB time=00:00:22.40 bitrate=5639.0kbits/frame=  178 fps=7.5 q=20.0 size=   15991kB time=00:00:23.01 bitrate=5692.4kbits/frame=  180 fps=7.4 q=17.0 size=   16226kB time=00:00:23.01 bitrate=5776.1kbits/frame=  184 fps=7.4 q=16.0 size=   16612kB time=00:00:23.28 bitrate=5844.9kbits/frame=  190 fps=7.5 q=20.0 size=   17139kB time=00:00:24.04 bitrate=5839.8kbits/frame=  192 fps=7.4 q=17.0 size=   17348kB time=00:00:24.53 bitrate=5792.2kbits/frame=  195 fps=7.4 q=16.0 size=   17554kB time=00:00:24.81 bitrate=5794.9kbits/frame=  199 fps=7.4 q=18.0 size=   17979kB time=00:00:25.58 bitrate=5756.1kbits/frame=  202 fps=7.4 q=16.0 size=   18201kB time=00:00:26.10 bitrate=5712.6kbits/frame=  205 fps=7.4 q=17.0 size=   18445kB time=00:00:26.79 bitrate=5639.9kbits/frame=  209 fps=7.4 q=18.0 size=   18808kB time=00:00:27.56 bitrate=5589.6kbits/frame=  212 fps=7.3 q=16.0 size=   19082kB time=00:00:27.56 bitrate=5671.0kbits/frame=  215 fps=7.3 q=16.0 size=   19413kB time=00:00:27.93 bitrate=5694.0kbits/frame=  219 fps=7.3 q=18.0 size=   19873kB time=00:00:28.70 bitrate=5672.1kbits/frame=  221 fps=7.3 q=16.0 size=   20000kB time=00:00:29.16 bitrate=5616.8kbits/frame=  225 fps=7.3 q=17.0 size=   20443kB time=00:00:29.87 bitrate=5606.3kbits/frame=  229 fps=7.3 q=17.0 size=   20875kB time=00:00:30.23 bitrate=5656.0kbits/frame=  232 fps=7.3 q=17.0 size=   21093kB time=00:00:30.63 bitrate=5641.1kbits/frame=  237 fps=7.3 q=17.0 size=   21625kB time=00:00:30.75 bitrate=5760.9kbits/frame=  241 fps=7.3 q=16.0 size=   22111kB time=00:00:31.52 bitrate=5746.1kbits/frame=  243 fps=7.3 q=18.0 size=   22327kB time=00:00:32.17 bitrate=5684.3kbits/frame=  248 fps=7.3 q=15.0 size=   22659kB time=00:00:32.94 bitrate=5633.7kbits/frame=  251 fps=7.3 q=21.0 size=   23012kB time=00:00:33.72 bitrate=5590.6kbits/frame=  254 fps=7.3 q=21.0 size=   23227kB time=00:00:33.72 bitrate=5642.7kbits/frame=  260 fps=7.3 q=21.0 size=   23630kB time=00:00:34.03 bitrate=5687.9kbits/frame=  263 fps=7.3 q=20.0 size=   23967kB time=00:00:34.61 bitrate=5672.3kbits/av_interleaved_write_frame(): Broken pipe
    Last message repeated 1 times
[flv @ 0x7fc54995be00] Failed to update header with correct duration.
[flv @ 0x7fc54995be00] Failed to update header with correct filesize.
Error writing trailer of rtmp://192.168.1.105:1935/live1/room1: Broken pipeframe=  264 fps=7.3 q=18.0 Lsize=   25180kB time=00:00:34.80 bitrate=5927.3kbits/s speed=0.967x    
video:25152kB audio:20kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.027315%
[libx264 @ 0x7fc54995de00] frame I:2     Avg QP:17.00  size:952357
[libx264 @ 0x7fc54995de00] frame P:262   Avg QP:18.57  size: 94027
[libx264 @ 0x7fc54995de00] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x7fc54995de00] mb P  I16..4:  1.4%  0.0%  0.0%  P16..4: 10.8%  0.0%  0.0%  0.0%  0.0%    skip:87.8%
[libx264 @ 0x7fc54995de00] coded y,uvDC,uvAC intra: 68.5% 67.1% 65.3% inter: 7.5% 9.0% 7.1%
[libx264 @ 0x7fc54995de00] i16 v,h,dc,p: 33% 27% 27% 12%
[libx264 @ 0x7fc54995de00] i8c dc,h,v,p: 58% 20% 16%  5%
[libx264 @ 0x7fc54995de00] kb/s:5865.17
Conversion failed!

 

posted @ 2016-03-02 00:33 嗷大喵 阅读(...) 评论(...) 编辑 收藏