0x00 Info
- 第一段脚本 cpu 占用和内存使用都比较低, 但会在一段时间后莫名断掉;
- 第二段脚本不会出现掉线问题, 但 cpu 占用一直在
100%左右, 内存占用 600m - 本人电脑是 MacBookPro2017款 macOS v10.13.2 FFmpeg v3.3.3
- 本人是不直播的, 但还是把直播间亮出来为敬: 哔哩哔哩直播,二次元弹幕直播平台
- 详细说明请查看 (貌似需要科学浏览): https://gist.github.com/jeasonstudio/914981b346746309828ae31ecda9264c
0x01 Usages
- 安装
FFmpeg自行 google - 先查看可用设备:
$ ffmpeg -f avfoundation -list_devices true -i ""ffmpeg version 3.3.3 Copyright (c) 2000-2017 the FFmpeg developersbuilt 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-vdalibavutil 55. 58.100 / 55. 58.100libavcodec 57. 89.100 / 57. 89.100libavformat 57. 71.100 / 57. 71.100libavdevice 57. 6.100 / 57. 6.100libavfilter 6. 82.100 / 6. 82.100libavresample 3. 5. 0 / 3. 5. 0libswscale 4. 6.100 / 4. 6.100libswresample 2. 7.100 / 2. 7.100libpostproc 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 有三个, 分别是摄像头和两个屏幕, 前面的数字是代号;
- 找到自己直播间的 rtmp 地址和秘钥, 并拼起来作为最后一个参数
执行命令 启动 FFmpeg 推流
$ ffmpeg \> -f avfoundation \> -re -i "2" \> -vcodec libx264 \> -preset ultrafast \> -acodec aac \> -ar 44100 \> -ac 1 \> -f flv "rtmp://example.com/path?key=xx"
或
$ 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 地址
打开直播页面验证
0x02 License
MIT LicenseCopyright (c) 2018 JeasonPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.
