macOS 下使用 FFmpeg 推流在 B 站直播

0x00 Info

  • 第一段腳本 cpu 佔用和內存使用都比較低, 但會在一段時間後莫名斷掉;
  • 第二段腳本不會出現掉線問題, 但 cpu 佔用一直在 100% 左右, 內存佔用 600m
  • 本人電腦是 MacBookPro2017款 macOS v10.13.2 FFmpeg v3.3.3
  • 本人是不直播的, 但還是把直播間亮出來為敬: 嗶哩嗶哩直播,二次元彈幕直播平台
  • 詳細說明請查看 (貌似需要科學瀏覽):

https://gist.github.com/jeasonstudio/914981b346746309828ae31ecda9264cgist.github.com

0x01 Usages

  • 安裝 FFmpeg 自行 google
  • 先查看可用設備:

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

可以看到 AVFoundation video devices 有三個, 分別是攝像頭和兩個屏幕, 前面的數字是代號;

  • 找到自己直播間的 rtmp 地址和秘鑰, 並拼起來作為最後一個參數
  • 執行命令 啟動 FFmpeg 推流

$ ffmpeg n> -f avfoundation n> -re -i "2" n> -vcodec libx264 n> -preset ultrafast n> -acodec aac n> -ar 44100 n> -ac 1 n> -f flv "rtmp://example.com/path?key=xx"n

$ ffmpeg n> -f avfoundation n> -video_size 1920x1080 n> -framerate 30 n> -i "2:0" -ac 2 # 2:0 代表 第二塊顯示屏:內建麥克風n> -vcodec libx264 -maxrate 2000k n> -bufsize 2000k -acodec libmp3lame -ar 44100 -b:a 128k n> -f flv "rtmp://example.com/path?key=xx" # rtmp 地址n

  • 打開直播頁面驗證

後來又把攝像頭疊加在了桌面, 看下效果吧, 就不放腳本了:

0x02 License

MIT Licensenn Copyright (c) 2018 Jeasonnn Permission is hereby granted, free of charge, to any person obtaining a copyn of this software and associated documentation files (the "Software"), to dealn in the Software without restriction, including without limitation the rightsn to use, copy, modify, merge, publish, distribute, sublicense, and/or selln copies of the Software, and to permit persons to whom the Software isn furnished to do so, subject to the following conditions:nn The above copyright notice and this permission notice shall be included in alln copies or substantial portions of the Software.nn THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORn IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEn AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERn LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THEn SOFTWARE. n

推薦閱讀:

FFmpeg任意文件讀取漏洞分析
yuv和rgb各有什麼優點?

TAG:FFmpeg | 哔哩哔哩直播 | MacOSX使用技巧 |