[关闭]
@rg070836rg 2017-02-12T17:55:36.000000Z 字数 6230 阅读 1577

流播放

未分类


1.更新ubuntu源

  1. sudo apt-get update
  2. 目的是为了 保持ubuntu的依赖正确

2.安装必要lib

  1. sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
  2. sudo apt-get install gcc make automake bzip2 unzip patch subversion libjpeg62-dev
  1. cd ~
  2. mkdir working
  3. wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
  4. tar zxvf yasm-1.2.0.tar.gz
  5. cd yasm-1.2.0
  6. ./configure
  7. make && make install
  8. cd ~/working
  9. 如果遇到权限不够 切换root chmod 777 加权限
  10. 保证下面的安装都在 working目录下

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

  1. apt-get remove ffmpeg x264

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

  1. wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2
  2. tar xvjf essential-amd64-20071007.tar.bz2
  3. mkdir /usr/local/lib/codecs
  4. cp -Rvp essential-amd64-20071007/* /usr/local/lib/codecs/
  5. 编辑下面文件
  6. vim /etc/ld.so.conf
  7. 添加以下两行到上面的文件里
  8. /usr/lib
  9. /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. sudo git clone git://git.videolan.org/x264.git
  2. cd x264
  3. sudo ./configure --enable-shared --prefix=/usr/local
  4. sudo make
  5. sudo make install
  6. cd ~/working

7 安装libvpx:

  1. cd ~/working
  2. 下载 libvpx-1.4.0
  3. http://download.csdn.net/download/lsc1818/8929949
  4. tar xvjf libvpx-1.4.0.tar.bz2
  5. cd libvpx-1.4.0
  6. sudo ./configure --enable-shared --prefix=/usr/local
  7. make && make install
  8. cd ~/working

8 安装FFmpeg

  1. cd ~/working
  2. wget http://ffmpeg.org/releases/ffmpeg-2.7.2.tar.bz2
  3. tar xvjf ffmpeg-2.7.2.tar.bz2
  4. cd ffmpeg-2.7.2
  5. ./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
  6. make && make install
  7. cd ~/working
  8. 让动态链接库被系统共享
  9. ldconfig

9 安装mplayer and mencoder 不必须

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

10 安装flvtool2: 不必须

  1. sudo apt-get install ruby
  2. gem install flvtool2

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

  1. sudo apt-get install mencoder
  2. 查看所有所支持的音频编码
  3. mencoder -oac help
  4. 查看所有所支持的视频编码
  5. 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. wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
  2. tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
  3. 注意:先要修改一下源码,注释掉
  4. nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
  5. 158161
  6. sudo gedit nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
  7. /* TODO: Win32 */
  8. //if (r->zero_in_uri)
  9. // {
  10. // return NGX_DECLINED;
  11. // }
  12. 第二个是nginx-rtmp-module,让nginx支持rtmp/hls协议
  13. wget -O nginx-rtmp-module.zip https://github.com/arut/nginx-rtmp-module/archive/master.zip
  14. unzip nginx-rtmp-module.zip
  15. 下载清缓存的模块
  16. wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
  17. unzip ngx_cache_purge.zip
  18. 下载安装nginx:
  19. wget http://nginx.org/download/nginx-1.8.0.tar.gz
  20. tar zxvf nginx-1.8.0.tar.gz
  21. cd nginx-1.8.0
  22. ./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
  23. make && make install

13 设置nginx为系统服务

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

14 各种配置nginx:

编辑/usr/local/nginx/conf/nginx.conf文件

  1. sudo gedit /usr/local/nginx/conf/nginx.conf
  2. #user nobody;
  3. worker_processes 1;
  4. error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. pid logs/nginx.pid;
  8. events {
  9. use epoll;
  10. worker_connections 1024;
  11. }
  12. rtmp {
  13. server {
  14. listen 1935;
  15. chunk_size 4000;
  16. # video on demand
  17. application vod {
  18. play /mnt/media/vod;
  19. }
  20. # HLS
  21. # HLS requires libavformat & should be configured as a separate
  22. # NGINX module in addition to nginx-rtmp-module:
  23. # ./configure … –add-module=/path/to/nginx-rtmp-module/hls …
  24. # For HLS to work please create a directory in tmpfs (/tmp/app here)
  25. # for the fragments. The directory contents is served via HTTP (see
  26. # http{} section in config)
  27. #
  28. # Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264
  29. # profile (see ffmpeg example).
  30. # This example creates RTMP stream from movie ready for HLS:
  31. #
  32. # ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
  33. # -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
  34. # -f flv rtmp://localhost:1935/hls/movie
  35. #
  36. # If you need to transcode live stream use ‘exec’ feature.
  37. #
  38. application hls {
  39. hls on;
  40. hls_path /mnt/media/app;
  41. hls_fragment 10s;
  42. }
  43. }
  44. }
  45. http {
  46. include mime.types;
  47. default_type application/octet-stream;
  48. sendfile on;
  49. keepalive_timeout 65;
  50. gzip on;
  51. #log format
  52. log_format access $remote_addr $remote_user [$time_local] $request
  53. $status $body_bytes_sent $http_referer
  54. ‘”$http_user_agent $http_x_forwarded_for’;
  55. #定义一个名为addr的limit_zone,大小10M内存来存储session
  56. limit_conn_zone $binary_remote_addr zone=addr:10m;
  57. server {
  58. listen 8080;
  59. server_name localhost;
  60. # HTTP can be used for accessing RTMP stats
  61. # This URL provides RTMP statistics in XML
  62. location /stat {
  63. rtmp_stat all;
  64. rtmp_stat_stylesheet stat.xsl;
  65. }
  66. location /stat.xsl {
  67. root /mnt/soft/nginx-rtmp-module-master;
  68. }
  69. location /control {
  70. rtmp_control all;
  71. }
  72. location / {
  73. root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;
  74. }
  75. }
  76. server {
  77. listen 80;
  78. server_name localhost;
  79. location / {
  80. root /mnt/wwwroot;
  81. index index.html;
  82. }
  83. location ~ \.flv$ {
  84. root /mnt/media/vod;
  85. flv;
  86. limit_conn addr 20;
  87. limit_rate 200k;
  88. }
  89. location ~ \.mp4$ {
  90. root /mnt/media/video;
  91. mp4;
  92. limit_conn addr 20;
  93. limit_rate 200k;
  94. }
  95. location /hls {
  96. # Serve HLS fragments
  97. alias /mnt/media/app;
  98. }
  99. access_log logs/nginxflv_access.log access;
  100. }
  101. }
  102. 注意 如果启动不了,是文件权限错误。手动建立文件夹
  103. /mnt/media/video -> 存放原始视频
  104. /mnt/media/vod -> 存放转换后的flvmp4视频,供httprtmp访问

15 m3u8切片

  1. git clone https://github.com/johnf/m3u8-segmenter.git
  2. 直接找到m3u8-segmenter.c 所在目录
  3. cd m3u8-segmenter/
  4. gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat -lavcodec -lavutil
  5. 拷贝至软件目录
  6. cp segmenter /usr/bin/
  7. 切片势力 原文件为 vp5.flv 转换为vp5.ts
  8. ffmpeg -i vp5.flv -acodec copy -vcodec libx264 vp5.ts
  9. segmenter 转换为m3u8 :
  10. segmenter -i vp5.ts -d 10 -p onedemo -m vp5.m3u8 -u http://localhost/videox/
  11. -p one demo 是分片的名字
  12. -u 是访问的链接 比如现在的网站ip 192.168.2.107 那么填写 http://192.168.2.107/video/
  13. 最后把ts文件与M3U8文件拷贝到网站对应的目录下 即可

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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注