参考地址:http://note.youdao.com/noteshare?id=a15b392a329fcf83bedf50d2a85fffec&sub=E2472051547B4383BE193A3CFA0FDE76
本章中为虚拟环境所以防火墙是关闭的,线上的防火墙可添加或酌情修改
-A INPUT -m state --state NEW -m tcp -p tcp --dport 1935 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
1.升级系统
yum install epel-release -y
yum update -y
reboot
2.环境
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# hostname -I
192.168.1.96
[root@localhost ~]# hostname
localhost.localdomain
3.下载yum依赖包
yum -y install gcc gcc-c++ autoconf automake make openssl openssl-devel pcre-devel git
4.nginx-rtmp-module安装
cd /opt
git clone https://github.com/arut/nginx-rtmp-module.git
5.nginx 安装略,编译时需加上此模块:--add-module=/root/nginx-rtmp-module
6.ffmpeg安装
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
7.安装ffmpeg和ffmpeg开发包
yum install ffmpeg ffmpeg-devel -y
8.测试
ffmpeg
ffmpeg version 2.6.8 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-16)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libdc1394 --enable-libfaac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil 54. 20.100 / 54. 20.100
libavcodec 56. 26.100 / 56. 26.100
libavformat 56. 25.101 / 56. 25.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 11.102 / 5. 11.102
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Use -h to get full help or, even better, run 'man ffmpeg'
9.nginx配置
worker_processes 1;
events {
worker_connections 1024;
}
rtmp { #RTMP 服务 此处共添加9行
server {
listen 1935; #服务端口
chunk_size 4096; #数据传输块的大小
application vod {
play /opt/video/vod; #视频文件存放位置。需要创建此目录(可放入视频进行播放)
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
10.新建存放视频的位置并放视频
mkdir -p /opt/video/vod
/etc/init.d/nginx restart重启nginx服务
11.添加nginx配置两个server并重启服务
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /opt/video/vod;
}
application live { #第一处添加的直播字段 此处共添加3行
live on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /stat { #第二处添加的 location 字段。 此处共添加7行
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl { #第二处添加的 location 字段。
root /usr/local/nginx/nginx-rtmp-module/; ##注意需要把这个模块放到 nginx 目录中
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
12.使用ffmpeg推送流
ffmpeg -re -i next.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://192.168.1.96:1935/live
显示如下:
ffmpeg version 2.6.8 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-16)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libdc1394 --enable-libfaac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil 54. 20.100 / 54. 20.100
libavcodec 56. 26.100 / 56. 26.100
libavformat 56. 25.101 / 56. 25.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 11.102 / 5. 11.102
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'next.mp4':
Metadata:
major_brand : mp42
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2016-08-09 09:35:02
encoder : HandBrake 0.10.5 2016021100
Duration: 00:00:05.00, start: 0.000000, bitrate: 739 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 960x540 [SAR 1:1 DAR 16:9], 735 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2016-08-09 09:35:02
handler_name : VideoHandler
[libx264 @ 0x214c520] using SAR=1/1
[libx264 @ 0x214c520] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x214c520] profile High, level 3.1
[libx264 @ 0x214c520] 264 - core 142 r2438 af8e768 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://192.168.1.96:1935/live':
Metadata:
major_brand : mp42
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.25.101
Stream #0:0(und): Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 960x540 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 1k tbn, 25 tbc (default)
Metadata:
creation_time : 2016-08-09 09:35:02
handler_name : VideoHandler
encoder : Lavc56.26.100 libx264
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[flv @ 0x214ba60] Failed to update header with correct duration.ate= 719.0kbits/s
[flv @ 0x214ba60] Failed to update header with correct filesize.
frame= 125 fps= 25 q=-1.0 Lsize= 221kB time=00:00:04.92 bitrate= 368.7kbits/s
video:219kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.284308%
[libx264 @ 0x214c520] frame I:1 Avg QP:21.33 size: 17488
[libx264 @ 0x214c520] frame P:45 Avg QP:17.88 size: 3758
[libx264 @ 0x214c520] frame B:79 Avg QP:17.46 size: 463
[libx264 @ 0x214c520] consecutive B-frames: 11.2% 9.6% 12.0% 67.2%
[libx264 @ 0x214c520] mb I I16..4: 22.8% 67.5% 9.8%
[libx264 @ 0x214c520] mb P I16..4: 6.9% 11.8% 0.3% P16..4: 25.2% 4.4% 1.3% 0.0% 0.0% skip:50.0%
[libx264 @ 0x214c520] mb B I16..4: 0.1% 0.2% 0.0% B16..8: 9.3% 0.4% 0.0% direct: 0.5% skip:89.6% L0:30.0% L1:67.6% BI: 2.4%
[libx264 @ 0x214c520] 8x8 transform intra:62.4% inter:89.6%
[libx264 @ 0x214c520] coded y,uvDC,uvAC intra: 19.3% 46.4% 6.9% inter: 3.7% 10.0% 0.2%
[libx264 @ 0x214c520] i16 v,h,dc,p: 32% 34% 11% 23%
[libx264 @ 0x214c520] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 23% 31% 2% 4% 3% 4% 2% 2%
[libx264 @ 0x214c520] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 25% 16% 5% 8% 5% 7% 4% 4%
[libx264 @ 0x214c520] i8c dc,h,v,p: 61% 21% 15% 3%
[libx264 @ 0x214c520] Weighted P-Frames: Y:33.3% UV:33.3%
[libx264 @ 0x214c520] ref P L0: 75.2% 16.2% 6.7% 1.9% 0.0%
[libx264 @ 0x214c520] ref B L0: 91.7% 7.7% 0.7%
[libx264 @ 0x214c520] ref B L1: 94.2% 5.8%
[libx264 @ 0x214c520] kb/s:357.07
RTMP #clients Video Audio In bytes Out bytes In bits/s Out bits/s State Time
Accepted: 0 codec bits/s size fps codec bits/s freq chan 0 KB 0 KB 0 Kb/s 0 Kb/s 41m 25s
vod
vod streams 0
live
live streams 0
Generated by nginx-rtmp-module 1.1.4, nginx 1.8.0, pid 1767, built Nov 16 2017 12:39:43 gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
转码mpg4转h264
ffmpeg -i next.mp4 -r 25 -s 1920*1080 -aspect 16:9 -b:v 2000K -bufsize 2000k -maxrate 2500k -vcodec h264 -strict -2 -acodec aac -ab 48k -ar 48000 -ac 2 n2.mp4
next.mp4 #源视频
n2.mp4 #转后的视频名
用以下链接中的方式查看转码前后对比,视觉上就能看出来,^^
http://note.youdao.com/noteshare?id=5658883c3a21467b6aaf3a9a171922fb&sub=8A7FC7CBE8E547908C1948143B859DFA