基于nginx+HLS的流媒体服务器搭建(Ubuntu 14.04系统)

来源:互联网 发布:淘宝加购物车软件 编辑:程序博客网 时间:2024/05/22 04:58

研究背景

公司转型做在线教育,在线的视频播放可以说是重要的事情,但是现有的视频只能在PC上播放,因此提出一个解决方案,搭建自己的流媒体服务器。

研究目标

(1)满足视频多平台播放,主要是移动端(apple);
(2)使用流媒体提高视频播放时的用户体验。

搭建过程

回顾搭建的过程,总结应该是分为两大步骤:
1.搭建nginx服务器,其中要添模块 nginx_mod_h264_streamingnginx-rtmp-module-masterhttp_mp4_module

重新源码编译nginx,添加flv,mp4支持模块,然后下载h264模块,源码编译 --add-module=./nginx_mod_h264_streaming-2.2.7/

2.视频转换
现有的视频格式为FLV,现在的做法是将视频转化为Hls流媒体格式。里面使用到FFmpeg,segment工具

ffmpeg --->flv--->.tssegment --->.ts--->.m3u8

关于使用ffmpeg视频转码保持原有的高分辨率,需要设置其参数,这里记录一下自己的参数设置

ffmpeg -y -i 原文件 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 转换后文件.tssegmenter -i 转换后文件.ts -p tt -m ceshi.m3u8 -u http://搭建的nginx的目录

测试结果

在firefox下打开上述的地址http://搭建的nginx的目录,现假设为 http://192.168.3.100:8000/hls/test,选择使用vlc打开视频,正常情况下可以看到视频播放;
在iPhone设备上,sarifa浏览器都可以正常播放;
在Android设备上,原生浏览器会因为机型的不同,有不同的情况,建议Android设备上使用UC浏览器。


自动化转换视频

整个流程导通后,就考虑视频自动转换为流媒体格式,并且生成到nginx设定的目录下。这里就使用shell编写自动化脚本。
暂时的设计是使用shell制作一个有menu交互的界面

这里写图片描述

在这里设计了5个选项:
(1)列出当前目录下的文件
(2)录入一个文件进行转换
(3)录入一个目录批量转换
(4)联系开发人员
(5)退出

当输入1时,会列出当前目录下的所有文件
这里写图片描述

当输入2和3时,会自动执行转换和生成,不做演示。


当输入4时,显示开发人员的联系方式
这里写图片描述


附上shell脚本做记录

# !/bin/sh#--------------全局变量设置--------------PROJPATH='/usr/local/nginx/html/hls/new'tmp='/'#----------------------------------------# 使用FFmpeg生成ts文件,segment切分成流文件,函数定义function test_fun(){echo "*********"echo $1mkdir -p ${PROJPATH}${tmp}${1}echo ${PROJPATH}${tmp}${1}echo ${PROJPATH}${tmp}${1}${tmp}${1}.tsecho $PWD}input=untilecho "----------------------------------"echo "please enter your choise:(1-5)"echo "(1) List you selected directory"echo "(2) Entry a File to format conversion (ts->.m3u8) "echo "(3) Entry a Dir to format conversion (ts->.m3u8)"echo "(4) Contact Developer(联系开发人员)"echo "(5) Exit Menu"echo "----------------------------------"read input #读入用户输入的内容,并存入变量test $input -eq 5 #若输入为5,则退出,否则继续docase $input in #开始CASE IN CASE结构1) ls;;2) echo "Entry a File to format conversion (*->.m3u8):"read fileif [ -f "$file" ]thennewfile=`basename $file`file_name=`echo ${newfile} | awk -F '.' '{ print $1 "" }'`  test_fun $file_nameelseecho "请输入正确的文件!"fi;;3) echo "Entry a Dir to format conversion (*->.m3u8):"read dirif [ -d "$dir" ]thenfor file_a in ${dir}/*; do      {     temp_file=`basename $file_a`      # 分离文件名和文件类型    file_name=`echo ${temp_file} | awk -F '.' '{ print $1 "" }'`    test_fun $file_name$file_a        }&    done        wait     elseecho "请输入正确的文件目录!"fi ;;4) echo "Contact Developer:"echo -e "\033[31m \033[05m开发人员:JamesCDD\033[0m"echo -e "\033[31m \033[05m联系方式:wwwknow@163.com\033[0m";;esac #结束CASE结构done

参考资料:
1.搭建nginx流媒体
http://www.cnblogs.com/fx2008/p/4227006.html
2.Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器
http://www.cnblogs.com/jerrychen/p/4705019.html
3.介绍nginx基本概念
http://blog.chinaunix.net/uid-25492475-id-195536.html
4.安装nginx_mod_h264_streaming-2.2.7
http://blog.csdn.net/sudoers/article/details/8990475
5.解决遇到的问题
http://blog.csdn.net/vblittleboy/article/details/40616405
6.HLS+FFmpeg
http://www.tuicool.com/articles/MFbeUn
7.shell学习基础
http://blog.csdn.net/sunboy8764/article/details/6892818
8.shell menu参考
http://www.linux521.com/2009/system/200912/9111.html
9.HLS系统搭建
http://blog.csdn.net/shishuo365/article/details/46440059

0 0