前言
在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇)
开发一款直播app,肯定需要流媒体服务器,本篇主要讲解直播中流媒体服务器搭建
,并且讲解了如何利用FFMPEG编码和推流,并且介绍了FFMPEG常见命令。
如果喜欢我的文章,可以关注我微博:袁峥Seemygo
效果
Snip20161018_20.png一、安装Homebrew
Homebrew简称brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件。
1、打开终端, 查看是否已经安装了Homebrew
, 直接终端输入命令
* man命令:manual(手册)的缩写,可以查看某一命令的帮助信息,比如git,brew,顺便可以查看有没有按照这个命令.
man brew
man brew.png
2、 执行命令,安装Homebrew
- 执行命令后,需要按回车,并且需要输入电脑密码。
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Homebrew.png
二、利用安装nginx
Nginx:Nginx
是一个非常出色的HTTP服务器,其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
- 从github
下载
Nginx到本地,增加home-brew对nginx的扩展
brew tap homebrew/nginx
brew tap.png
2.安装Nginx服务器和rtmp模块
brew install nginx-full --with-rtmp-module
Snip20161017_6.png
3.查看是否安装成功
在终端开启nginx服务器,输入一下命令
nginx
-
nginx
:开启nginx服务器
在浏览器地址栏输入:http://localhost:8080 (直接点击)
如果出现下图, 则表示安装成功
三、配置rtmp
1.查看nginx配置文件安装在哪
brew info nginx-full
Snip20161022_3.png
Snip20161022_4.png
2.用xcode打开配置文件,滚动到最后面(
最后一个}后面即可,不能在{}里面
),添加一下代码,进行配置,最后记得保存
Snip20161022_5.png
rtmp {
server {
listen 1990;
application liveApp {
live on;
record off;
}
}
}
Snip20161017_11.png
-
application
:流媒体上应用名称,可以随意填 -
record off
:图片有误,不记录数据
3.重新加载nginx的配置文件
nginx -s reload
四、安装ffmepg进行推流
brew install ffmpeg
Snip20161017_10.png
五、使用ffmepg推流测试
ffmpeg -re -i (视频全路径) -vcodec copy -f flv (rtmp路径
ffmpeg -re -i /Users/yuanzheng/Desktop/02-如何学习项目.mp4 -vcodec copy -f flv rtmp://localhost:1990/liveApp/room
- 需要跟配置的一一对应,端口,应用名称,room可以随便写
- 延时:发送流媒体的数据的时候需要延时。不然的话,FFmpeg处理数据速度很快,瞬间就能把所有的数据发送出去,
流媒体服务器是接受不了的
。因此需要按照视频实际的帧率
发送数据 - -re: 一定要加,代表按照帧率发送,否则ffmpeg会一股脑地按最高的效率发送数据
- -i : 输入文件
- -vcodec copy: 强制使用codec编解码方式,要加,否则ffmpeg会重新编码输入的H.264裸流
- -f 强制转换为什么格式,后接格式
- ffmpeg参数中文详细解释
六、使用VLC播放rtmp推流
1.下载VLC
2.打开VLC,输入直播地址,cmd + N
Snip20161018_15.png Snip20161018_16.png ![Uploading Snip20161018_20_411856.png . . .]七、用ffmpeg抓取桌面以及摄像头推流进行直播
1.首先查看ffmpeg是否支持对应的设备,在OSX下面,Video和Audio设备使用的是avfoundation,所以可以使用avfoundation来查看
ffmpeg -f avfoundation -list_devices true -i ""
Snip20161018_13.png
2.抓取桌面和摄像头进行推流
ffmpeg -f avfoundation -framerate 30 -i "1:0" -f avfoundation -framerate 30 -video_size 640x480 -i "0" -c:v libx264 -preset slow -filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1990/liveApp/room
-
-f avfoundation
转换为avfoundation -
-framerate 30
: 设置帧率 30 -
-i "1:0"
: 设置输出,视频:Capture screen
音频:Built-in Microphone
-
-f avfoundation -framerate 30 -video_size 640x480
: 设置帧率和视频尺寸 -
-c:v libx264
设置视频编码,H.264编码 优点是同等清晰度,视频文件更小 缺点就是转换慢 -
-c:v flv
标准FLV编码 这个好处是速度快 清晰度高的话 视频文件会比较大 -
-preset slow
使用慢速模式 延迟长 清晰度高 - ffmpeg的转码延时测试与设置优化
-
-filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10'
:给视频打水印 -
-acodec libmp3lame
強制指定音频处理模式 -
-ac 1
声道选择 -
-ar 44100
音频赫兹
网友评论
一直爆这个错误:
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
[avfoundation @ 0x7fb166000000] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7fb166000000] Supported pixel formats:
[avfoundation @ 0x7fb166000000] uyvy422
[avfoundation @ 0x7fb166000000] yuyv422
[avfoundation @ 0x7fb166000000] nv12
[avfoundation @ 0x7fb166000000] 0rgb
[avfoundation @ 0x7fb166000000] bgr0
[avfoundation @ 0x7fb166000000] Overriding selected pixel format to use uyvy422 instead.
[avfoundation @ 0x7fb166000000] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '1:0':
Duration: N/A, start: 22354.938000, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1920x1080, 1000k tbr, 1000k tbn, 1000k tbc
Stream #0:1: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
[avfoundation @ 0x7fb16687a000] Selected framerate (30.000000) is not supported by the device
[avfoundation @ 0x7fb16687a000] Supported modes:
[avfoundation @ 0x7fb16687a000] 160x120@[29.970000 29.970000]fps
怎么处理?
rtmp://localhost:3017/liveApp/room/123: Input/output error,我也尝试重启Nginx,但也没用,前面的配置都是正常的,这一步不知为什么,能否帮我分析下
nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied)
2017/02/15 16:18:37 [emerg] 2214#0: unknown directive "rtmp" in /usr/local/etc/nginx/nginx.conf:119这应该怎么弄啊 我手动输入也不行啊
那要如何用手机这边录,另外一端看呢?
[flv @ 0x7ff049800600] FLV does not support sample rate 48000, choose from (44100, 22050, 11025)
[flv @ 0x7ff049800600] Audio codec mp3 not compatible with flv
Could not write header for output file #0 (incorrect codec parameters ?): Function not implemented
怎么解决
12 年 的MBA不支持 30 帧,降到25 也不行。烦请看看问题在哪,谢谢:
$ ffmpeg -f avfoundation -framerate 30 -i "1:0" -f avfoundation -framerate 14.999993 -video_size 160x120 -i "0" -c:v libx264 -preset slow -filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1990/liveApp/room
[avfoundation @ 0x7fd7c8221a00] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[avfoundation @ 0x7fd7c8000000] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[flv @ 0x7fd7c9087400] Packets are not in the proper order with respect to DTS/s speed=0.829x
av_interleaved_write_frame(): Invalid argument
[flv @ 0x7fd7c9087400] Failed to update header with correct duration.
[flv @ 0x7fd7c9087400] Failed to update header with correct filesize.
[avfoundation @ 0x7ff651008000] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[avfoundation @ 0x7ff652008600] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[flv @ 0x7ff65200c600] Packets poorly interleaved, failed to avoid negative timestamp -63 in stream 0.
Try -max_interleave_delta 0 as a possible workaround.
[flv @ 0x7ff65200c600] Packets are not in the proper order with respect to DTS
av_interleaved_write_frame(): Invalid argument
[flv @ 0x7ff65200c600] Failed to update header with correct duration.
[flv @ 0x7ff65200c600] Failed to update header with correct file size.
...
Conversion failed!
Linking /usr/local/Cellar/pcre/8.39...
Error: Could not symlink bin/pcre-config
Target /usr/local/bin/pcre-config
is a symlink belonging to pcre. You can unlink it:
brew unlink pcre
To force the link and overwrite all conflicting files:
brew link --overwrite pcre
To list all files that would be deleted:
brew link --overwrite --dry-run pcre