Skip to content

Instantly share code, notes, and snippets.

@jeasonstudio
Last active July 29, 2023 19:32
Show Gist options
  • Save jeasonstudio/914981b346746309828ae31ecda9264c to your computer and use it in GitHub Desktop.
Save jeasonstudio/914981b346746309828ae31ecda9264c to your computer and use it in GitHub Desktop.
Use FFmpeg rtmp streaming in macOS
#! /bin/bash
#
# Diffusion bilibili live avec ffmpeg
# Make sure you have FFmpeg installed in your mac
# list avfoundation devices
ffmpeg -f avfoundation -list_devices true -i ""
# change the param after `-i` and `-f flv`
# use 140m mermory
ffmpeg \
-f avfoundation \
-re -i "2" \
-vcodec libx264 \
-preset ultrafast \
-acodec aac \
-ar 44100 \
-ac 1 \
-f flv "rtmp://example.com/path?key=xx"
# 560m mermory 90% cpu
ffmpeg \
-f avfoundation \
-video_size 1920x1080 \
-framerate 30 \
-i "2:0" -ac 2 \
-vcodec libx264 -maxrate 2000k \
-bufsize 2000k -acodec libmp3lame -ar 44100 -b:a 128k \
-f flv "rtmp://example.com/path?key=xx"
@jeasonstudio
Copy link
Author

jeasonstudio commented Jan 9, 2018

0x00 Info

  • 第一段脚本 cpu 占用和内存使用都比较低, 但会在一段时间后莫名断掉;
  • 第二段脚本不会出现掉线问题, 但 cpu 占用一直在 100% 左右, 内存占用 600m
  • 本人电脑是 MacBookPro2017款 macOS v10.13.2 FFmpeg v3.3.3

0x01 Usages

  1. 安装 FFmpeg 自行 google
  2. 先查看可用设备:
$ ffmpeg -f avfoundation -list_devices true -i ""

ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.42)
  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
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
[AVFoundation input device @ 0x7fdc10d04580] AVFoundation video devices:
[AVFoundation input device @ 0x7fdc10d04580] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7fdc10d04580] [1] Capture screen 0
[AVFoundation input device @ 0x7fdc10d04580] [2] Capture screen 1
[AVFoundation input device @ 0x7fdc10d04580] AVFoundation audio devices:
[AVFoundation input device @ 0x7fdc10d04580] [0] Built-in Microphone
: Input/output error

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

  1. 找到自己直播间的 rtmp 地址和秘钥, 并拼起来作为最后一个参数
  2. 执行命令 启动 FFmpeg 推流
$ ffmpeg \
>   -f avfoundation \
>   -video_size 1920x1080 \
>   -framerate 30 \
>   -i "2:0" -ac 2 \ # 2:0 代表 第二块显示屏:内建麦克风
>   -vcodec libx264 -maxrate 2000k \
>   -bufsize 2000k -acodec libmp3lame -ar 44100 -b:a 128k \
>   -f flv "rtmp://example.com/path?key=xx" # rtmp 地址
  1. 打开直播页面验证

0x02 License

  MIT License

  Copyright (c) 2018 Jeason

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

@gMan1990
Copy link

不行,直播间一直转圈圈,黑屏。

  1. frame= 149 fps=3.6 q=14.0 size= 14235kB time=00:00:38.53 bitrate=3026.2kbits/s speed=0.919x
    frame= 384 fps=5.6 q=8.0 size= 22174kB time=00:01:05.60 bitrate=2769.0kbits/s speed=0.962x
  2. frame= 659 fps= 26 q=30.0 size= 3231kB time=00:00:25.70 bitrate=1030.0kbits/s speed=1.01x
    frame= 931 fps= 27 q=29.0 size= 4681kB time=00:00:35.16 bitrate=1090.4kbits/s speed=1.01x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment