0%

用 ffmpeg 进行 rtmp 推流

前言

想学习一下视频这一块的技术,了解 iOS 上采集视频到播放的过程。本文先在 Mac 上用 ffmpeg 将本地 mp4 文件 rtmp 推流,本地起 nginx 并使用 VLC 播放,剩下的下次再继续。

步骤

  1. 安装 homebrew

  2. 安装 nginx 和 rtmp 模块 https://github.com/denji/homebrew-nginx

    1
    2
    brew tap denji/nginx
    brew install nginx-full --with-rtmp-module
  3. 运行 nginx

    网上说前两步执行后直接终端 nginx 就会起一个 server ,浏览器输入 http://localhost:8080 会出现 nginx 的欢迎页面,我这会提示 command not found ,遂修改 $PATH。

    终端 brew info nginx-full 找到 nginx 的安装路径

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    ➜  ~ brew info nginx-full
    denji/nginx/nginx-full: stable 1.12.2, devel 1.13.10, HEAD
    HTTP(S) server, reverse proxy, IMAP/POP3 proxy server
    https://nginx.org/
    Conflicts with:
    nginx (because nginx-full symlink with the name for compatibility with nginx)
    /usr/local/Cellar/nginx-full/1.12.2 (8 files, 1.2MB)
    Built from source on 2018-06-28 at 14:39:46 with: --with-rtmp-module
    From: https://github.com/denji/homebrew-nginx/blob/master/Formula/nginx-full.rb

    ...

    ==> Caveats
    Docroot is: /usr/local/var/www

    The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
    nginx can run without sudo.

    nginx will load all files in /usr/local/etc/nginx/servers/.

    - Tips -
    Run port 80:
    $ sudo chown root:wheel /usr/local/opt/nginx-full/bin/nginx
    $ sudo chmod u+s /usr/local/opt/nginx-full/bin/nginx
    Reload config:
    $ nginx -s reload
    Reopen Logfile:
    $ nginx -s reopen
    Stop process:
    $ nginx -s stop
    Waiting on exit process
    $ nginx -s quit

    To have launchd start denji/nginx/nginx-full now and restart at login:
    brew services start denji/nginx/nginx-full
    Or, if you don't want/need a background service you can just run:
    nginx

    修改 $PATH

    1
    vi ~/.bashrc

    文件里 export PATH=$PATH:/usr/local/Cellar/nginx-full/1.12.2/bin

    1
    source ~/.bashrc

    终端 nginx 后打开 http://localhost:8080

  4. 上面找到 nginx.conf 路径为 /usr/local/etc/nginx/nginx.conf 。在 conf 中 http 下面(括号外)添加

    rtmp 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    rtmp {
    server {
    listen 1990;
    application liveApp {
    live on;
    record off;
    }
    }
    }

    重新加载 nginx 的配置文件

    1
    nginx -s reload
  5. Mac 安装 ffmpeg

    1
    brew install ffmpeg
  6. 推流

    网上都是 localhost 我这走不通,改成 127.0.0.1 可以。

    在 a.mp4 所在目录下

    1
    ffmpeg -re -i a.mp4 -vcodec copy -f flv rtmp://127.0.0.1:1990/liveApp

  7. Mac 上使用 VLC 播放 rtmp 流

    下载 VLC 后 File -> Open Network 填入地址 rtmp://127.0.0.1:1990/liveApp

参考

https://blog.csdn.net/zcvbnh/article/details/79495285

https://www.jianshu.com/p/53059be61546