流播放


1.更新ubuntu源

1
2
3
sudo apt-get update

目的是为了 保持ubuntu的依赖正确

2.安装必要lib

1
2
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
sudo apt-get install gcc make automake bzip2 unzip patch subversion libjpeg62-dev
1
2
3
4
5
6
7
8
9
10
11
12
13
cd ~
mkdir working
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar zxvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make && make install
cd ~/working


如果遇到权限不够 切换root chmod 777 加权限

保证下面的安装都在 working目录下

#3. 卸载系统原有的ffmgeg和x.264,没有则跳过:

1
apt-get remove ffmpeg x264

#4 先安装一些Mplayer编码库(仅限于64位系统):

1
2
3
4
5
6
7
8
9
10
11
12
wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2


tar xvjf essential-amd64-20071007.tar.bz2
mkdir /usr/local/lib/codecs
cp -Rvp essential-amd64-20071007/* /usr/local/lib/codecs/

编辑下面文件
vim /etc/ld.so.conf
添加以下两行到上面的文件里
/usr/lib
/usr/local/lib

#5 再安装依赖库

1
sudo apt-get install make automake g++ bzip2 python unzip patch subversion ruby build-essential git-core checkinstall yasm texi2html libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libvdpau-dev libvorbis-dev libvpx-dev libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev

#6 安装 x264

1
2
3
4
5
6
sudo git clone git://git.videolan.org/x264.git
cd x264
sudo ./configure --enable-shared --prefix=/usr/local
sudo make
sudo make install
cd ~/working

#7 安装libvpx:

1
2
3
4
5
6
7
8
9
cd ~/working
下载 libvpx-1.4.0,
http://download.csdn.net/download/lsc1818/8929949

tar xvjf libvpx-1.4.0.tar.bz2
cd libvpx-1.4.0
sudo ./configure --enable-shared --prefix=/usr/local
make && make install
cd ~/working

#8 安装FFmpeg

1
2
3
4
5
6
7
8
9
10
cd ~/working
wget http://ffmpeg.org/releases/ffmpeg-2.7.2.tar.bz2

tar xvjf ffmpeg-2.7.2.tar.bz2
cd ffmpeg-2.7.2
./configure --enable-gpl --enable-version3 --enable-shared --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libxvid --enable-libx264 --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib
make && make install
cd ~/working
让动态链接库被系统共享
ldconfig

#9 安装mplayer and mencoder 不必须

1
2
3
4
5
apt-get install libmp3lame-dev 
svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
cd mplayer/
./configure
make && make install

#10 安装flvtool2: 不必须

1
2
sudo apt-get install ruby
gem install flvtool2

#11 查看一下已经安装好的音频和视频编码器 不必须

1
2
3
4
5
6
sudo apt-get install mencoder
查看所有所支持的音频编码
mencoder -oac help

查看所有所支持的视频编码
mencoder -ovc help

#12 安装配置Nginx:
先安装各种依赖(nginx需要pcre支持,yamdi用来为flv创建关键帧才能随意拖动)

1
sudo apt-get install gcc g++ libssl-dev zlib1g-dev libpcre3-dev yamdi

下载所需的nginx模块

第一个是nginx_mod_h264_streaming,让nginx支持flv/mp4流播放

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
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

注意:先要修改一下源码,注释掉
nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
的158到161行

sudo gedit nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c

/* TODO: Win32 */
//if (r->zero_in_uri)
// {
// return NGX_DECLINED;
// }

第二个是nginx-rtmp-module,让nginx支持rtmp/hls协议
wget -O nginx-rtmp-module.zip https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip nginx-rtmp-module.zip

下载清缓存的模块
wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
unzip ngx_cache_purge.zip

下载安装nginx:
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --user=daemon --group=daemon --prefix=/usr/local/nginx/ --add-module=../nginx-rtmp-module-master --add-module=../ngx_cache_purge-master --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_flv_module
make && make install

#13 设置nginx为系统服务

1
2
3
4
5
6
7
sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
sudo service nginx start//开启
sudo service nginx stop //停止,列出

开启后,浏览localhost

#14 各种配置nginx:
编辑/usr/local/nginx/conf/nginx.conf文件

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
sudo gedit /usr/local/nginx/conf/nginx.conf


#user nobody;
worker_processes 1;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;


events {
use epoll;
worker_connections 1024;
}

rtmp {
server {
listen 1935;
chunk_size 4000;

# video on demand
application vod {
play /mnt/media/vod;
}

# HLS
# HLS requires libavformat & should be configured as a separate
# NGINX module in addition to nginx-rtmp-module:
# ./configure … –add-module=/path/to/nginx-rtmp-module/hls …
# For HLS to work please create a directory in tmpfs (/tmp/app here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
#
# ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
# -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
# -f flv rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use ‘exec’ feature.
#
application hls {
hls on;
hls_path /mnt/media/app;
hls_fragment 10s;
}
}
}

http {

include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;

#log format

log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;

#定义一个名为addr的limit_zone,大小10M内存来存储session
limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
listen 8080;
server_name localhost;

# HTTP can be used for accessing RTMP stats
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /mnt/soft/nginx-rtmp-module-master;
}
location /control {
rtmp_control all;
}
location / {
root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;
}
}

server {
listen 80;
server_name localhost;

location / {
root /mnt/wwwroot;
index index.html;
}

location ~ \.flv$ {
root /mnt/media/vod;
flv;
limit_conn addr 20;
limit_rate 200k;
}
location ~ \.mp4$ {
root /mnt/media/video;
mp4;
limit_conn addr 20;
limit_rate 200k;
}

location /hls {
# Serve HLS fragments
alias /mnt/media/app;
}

access_log logs/nginxflv_access.log access;
}


}


注意 如果启动不了,是文件权限错误。手动建立文件夹


/mnt/media/video -> 存放原始视频
/mnt/media/vod -> 存放转换后的flv和mp4视频,供http或rtmp访问

#15 m3u8切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
git clone https://github.com/johnf/m3u8-segmenter.git

直接找到m3u8-segmenter.c 所在目录
cd m3u8-segmenter/
gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat -lavcodec -lavutil

拷贝至软件目录
cp segmenter /usr/bin/


切片势力 原文件为 vp5.flv 转换为vp5.ts
ffmpeg -i vp5.flv -acodec copy -vcodec libx264 vp5.ts

segmenter 转换为m3u8 :
segmenter -i vp5.ts -d 10 -p onedemo -m vp5.m3u8 -u http://localhost/videox/


-p one demo 是分片的名字
-u 是访问的链接 比如现在的网站ip是 192.168.2.107 那么填写 http://192.168.2.107/video/

最后把ts文件与M3U8文件拷贝到网站对应的目录下 即可

未解决问题:
1 网页播放m3u8,原计划使用ckplayer 但是一直有错,没有成功
2 据观察,m3u8同样能够下载?