0x00 Info

0x01 Usages

  • 安装 FFmpeg 自行 google
  • 先查看可用设备:
    1. $ ffmpeg -f avfoundation -list_devices true -i ""
    2. ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developers
    3. built with Apple LLVM version 8.1.0 (clang-802.0.42)
    4. configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
    5. libavutil 55. 58.100 / 55. 58.100
    6. libavcodec 57. 89.100 / 57. 89.100
    7. libavformat 57. 71.100 / 57. 71.100
    8. libavdevice 57. 6.100 / 57. 6.100
    9. libavfilter 6. 82.100 / 6. 82.100
    10. libavresample 3. 5. 0 / 3. 5. 0
    11. libswscale 4. 6.100 / 4. 6.100
    12. libswresample 2. 7.100 / 2. 7.100
    13. libpostproc 54. 5.100 / 54. 5.100
    14. [AVFoundation input device @ 0x7fdc10d04580] AVFoundation video devices:
    15. [AVFoundation input device @ 0x7fdc10d04580] [0] FaceTime HD Camera
    16. [AVFoundation input device @ 0x7fdc10d04580] [1] Capture screen 0
    17. [AVFoundation input device @ 0x7fdc10d04580] [2] Capture screen 1
    18. [AVFoundation input device @ 0x7fdc10d04580] AVFoundation audio devices:
    19. [AVFoundation input device @ 0x7fdc10d04580] [0] Built-in Microphone
    20. : Input/output error

可以看到 AVFoundation video devices 有三个, 分别是摄像头和两个屏幕, 前面的数字是代号;

  • 找到自己直播间的 rtmp 地址和秘钥, 并拼起来作为最后一个参数
  • 执行命令 启动 FFmpeg 推流

    1. $ ffmpeg \
    2. > -f avfoundation \
    3. > -re -i "2" \
    4. > -vcodec libx264 \
    5. > -preset ultrafast \
    6. > -acodec aac \
    7. > -ar 44100 \
    8. > -ac 1 \
    9. > -f flv "rtmp://example.com/path?key=xx"

    1. $ ffmpeg \
    2. > -f avfoundation \
    3. > -video_size 1920x1080 \
    4. > -framerate 30 \
    5. > -i "2:0" -ac 2 \ # 2:0 代表 第二块显示屏:内建麦克风
    6. > -vcodec libx264 -maxrate 2000k \
    7. > -bufsize 2000k -acodec libmp3lame -ar 44100 -b:a 128k \
    8. > -f flv "rtmp://example.com/path?key=xx" # rtmp 地址
  • 打开直播页面验证

后来又把摄像头叠加在了桌面, 看下效果吧, 就不放脚本了:
macOS 下使用 FFmpeg 推流在 B 站直播 - 图1

0x02 License

  1. MIT License
  2. Copyright (c) 2018 Jeason
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.