一个简单视频网站开发小记

前言

视频格式转换,视频图片截取,视频存储设计,大文件上传处理以及相关配置,前端视频播放,视频播放流畅度,每一个都不简单,都需要花心思思考解决方法!基本上每个问题,都是使用相应的开源库!

需求背景

手机拍摄的视频越来越多,然而手机的容量有限,故想到开发一个手机视频保存与播放的简单网站,只需要实现我的需求即可!

技术实现方案

VBox+LNMP+Laravel5.1+ffmpeg+ckplayer
网站运行于笔记本虚拟机中

运行系统相关说明

Virtual Box:4.3.18
Linux:CentOS Linux release 7.1.1503 (Core)
Nginx:nginx/1.8.0
MySQL:Ver 5.7.9 for Linux on x86_64 (MySQL Community Server (GPL))
PHP:PHP 5.6.16
ffmpeg:version 2.6.3

实现效果图

源代码下载

http://files.cnblogs.com/files/wadeyu/familyvideo.zip

问题总结之PHP

1)编译安装PHP
yum install gcc gcc-c++ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel curl curl-devel openssl openssl-devel
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr --with-mysqli=mysqlnd --with-iconv-dir=/usr --with-gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-soap --without-pear --enable-zip --with-bz2 --enable-calendar --enable-ftp --enable-ctype --enable-exif --disable-ipv6 --with-sqlite3 --enable-pdo --with-pdo-mysql=/usr --enable-phar --with-curl
2)yum install libmcrypt
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
No package libmcrypt available.
Error: Nothing to do
解决办法:
yum install epel-release //扩展包更新包
yum update //更新yum源
3)configure: error: Cannot find MySQL header files under /usr.
安装mysql-devel包 yum install mysql-devel
4)3.configure: error: Cannot find libmysqlclient under /usr.
find / -name '*libmysqlclient*'
/usr/lib64/mysql/libmysqlclient.so.18
/usr/lib64/mysql/libmysqlclient_r.so.18
/usr/lib64/mysql/libmysqlclient_r.so.18.1.0
/usr/lib64/mysql/libmysqlclient.so.20
/usr/lib64/mysql/libmysqlclient.so.18.1.0
/usr/lib64/mysql/libmysqlclient.so
/usr/lib64/mysql/libmysqlclient.a
/usr/lib64/mysql/libmysqlclient.so.20.0.9
发现其实保存在lib64那个目录下了,拷贝一份到/usr/lib/mysql目录下就可以了
cp /usr/lib64/mysql/* /usr/lib/mysql/
5)composer安装
https://getcomposer.org/download/
file_get_contents(): Failed to enable crypto
解决方法:
wget http://curl.haxx.se/ca/cacert.pem
curl -sS https://getcomposer.org/installer | php -- --cafile=cacert.pem
6)php-ffmpeg扩展编译安装
因为php-ffmpeg扩展很久没有更新,跟我使用的ffmpeg版本不兼容,编译安装过程中碰到不少坑,
我使用这个扩展最新版本+加上修改编译配置才能成功
从GIT下载兼容最新版本的代码
git clone https://github.com/tony2001/ffmpeg-php.git ffmpeg-php-0.6.0
源代码目录找到config.m4文件,ffmpeg头部检测的代码修改为:

  AC_MSG_CHECKING(for ffmpeg headers)
  for i in $INC_CHECK_DIRS ; do
    if test -f $i/include/ffmpeg/avcodec.h; then
      PHP_ADD_INCLUDE($i/include/ffmpeg)
      FFMPEG_INC_FOUND=$i/include/ffmpeg
      break
    elif test -f $i/include/avcodec.h; then
      PHP_ADD_INCLUDE($i/include)
      FFMPEG_INC_FOUND=$i/include
      break
    elif test -f $i/include/ffmpeg/libavcodec/avcodec.h; then
      dnl ffmpeg svn revision 12194 and newer put each header in its own dir
      dnl so we have to include them all.
      PHP_ADD_INCLUDE($i/include/ffmpeg)
      PHP_ADD_INCLUDE($i/include/ffmpeg/libavcodec/)
      PHP_ADD_INCLUDE($i/include/ffmpeg/libavformat/)
      PHP_ADD_INCLUDE($i/include/ffmpeg/libswscale/)
      PHP_ADD_INCLUDE($i/include/ffmpeg/libavfilter/)
      PHP_ADD_INCLUDE($i/include/ffmpeg/libavdevice/)
      FFMPEG_INC_FOUND=$i/include/ffmpeg/libavcodec
      break
    fi
  done

7)mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
如果是localhost mysql客户端会尝试通过SOCKET连接 所以连不上 换成Ip地址就可以了 127.0.0.1

问题总结之Nginx

1)[error] 3109#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.0.2.2, server: localhost, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://unix:/usr/local/php/run/php-fpm.socket:", host: "192.168.1.2"
nginx配置WEB根目录没有配置,在SERVER CONTEXT设置好通用的ROOT就可以了,还需要设置好SCRIPT_FILENAME这个参赛,需要包含完整的路径,有时候包含的fastcgi_params文件没有这个参数的设置,需要主动设置好
2)windows下编辑虚拟机共享目录下的静态文件,NGINX还是返回修改之前的文件
gzip on;开启gzip压缩解决了这个问题,为什么?
3)[error] 2126#0: *384 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.0.2.2, server: localhost, request: "POST /upload/ HTTP/1.1", upstream: "fastcgi://unix:/usr/local/php/run/php-fpm.socket", host: "192.168.1.2", referrer: "http://192.168.1.2/upload/"
fastcgi_read_timeout 600; # Set fairly high for debugging

问题总结之CentOS7

1)防火墙默认使用的是firewall不是iptables,需要开放端口外部才能访问
开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
2)开源视频库ffmpeg安装
因为用的最新的CentOS系统,官方仓库中没要找到现成的rpm包,费了一番周折找到第三方RPM仓库包含
先安装第三方仓库源 rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
再安装ffmpeg yum install ffmpeg ffmpeg-devel
3)ffmpeg命令使用
ffmpeg -y -i IMG_0071.MOV -f mp4 -async 1 -acodec aac -vcodec libxvid -qscale 7 -dts_delta_threshold 1 -strict -2 output.mp4
ffmpeg -i test.asf -y -f image2 -t 0.001 -s 352x240 a.jpg

问题总结之MySQL

1)phpmysqladmin ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

1 ALTER USER 'root'@'localhost' IDENTIFIED BY 'yh!2@#A33@@$$'
2 GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'yh!2@#A33@@$$' WITH GRANT OPTION;
3 FLUSH PRIVILEGES;

 2) Your password has expired.

MySQL5.7版本以及以上自动开启了用户密码过期功能,通过设置mysqld参数:

default_password_lifetime=0 禁用此功能
然后重启mysql服务器

其它

1. nginx php-fpm php配置好了相关上传大文件的配置还是上传失败
1)检查php临时目录大小是否比上传文件要大几倍
2)临时目录需要有可写可执行权限
3)如果是虚拟机,文件不要存储在共享目录下,通过PHP move_uploaded_file文件移动到共享目录很慢,而且碰到CGI程序中途退出

2.提供访问速度
1)laravel框架缓存
php artisan optimize --force
2)开启字节码缓存 opcache

后记

1.碰到的最大难点是ffmpeg库以及ffmpeg php扩展安装
2.视频需要加上标签分类
3.视频需要分片提高响应速度
4.使用虚拟机播放视频比较慢,后面尝试使用树莓派

参考资料

[1] CentOS yum安装mcrypt
http://www.cnblogs.com/linux-super-meng/archive/2014/12/08/4150987.html
[2] PHP FastCGI Example
https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
[3] FastCGI sent in stderr: “Primary script unknown”
http://stackoverflow.com/questions/17194196/1-fastcgi-sent-in-stderr-primary-script-unknown
http://serverfault.com/questions/517190/nginx-1-fastcgi-sent-in-stderr-primary-script-unknown/517207#517207
[4] CentOS7 firewalld设置端口
http://www.cnblogs.com/kootao/p/4752435.html
[5] fedora/centos7防火墙FirewallD详解
http://www.cnblogs.com/yudar/p/4294500.html
[6] CentOS修改系统环境变量
http://www.linuxidc.com/Linux/2012-07/64532.htm
[7] 安装composer SSL operation failed with code 1
http://www.cnblogs.com/brookin/p/4425950.html
[8] Packagist / Composer 中国全量镜像
http://pkg.phpcomposer.com/
http://www.phpcomposer.com/
[9] 更改yum源为163镜像站点
http://mirrors.163.com/.help/centos.html
[10] FFmpeg常用基本命令
http://www.cnblogs.com/dwdxdy/p/3240167.html
http://www.cnblogs.com/top5/archive/2009/12/30/1636349.html
[11] Unable to install FFMPEG PHP extension
https://community.centminmod.com/threads/unable-to-install-ffmpeg-php-extension.1519/page-2
[12] Compile against newer ffmpeg fails because of avutil/time.h
http://sourceforge.net/p/ffmpeg-php/bugs/59/
[13] ffmpeg官方网站
http://ffmpeg-php.sourceforge.net/index.php
[14] mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL'
http://stackoverflow.com/questions/13769504/mysqlimysqli-hy000-2002-cant-connect-to-local-mysql-server-through-sock
http://segmentfault.com/q/1010000000328531
[15] 解决大文件上传相关配置
http://blog.csdn.net/z_p_h/article/details/8995160

[16]mysql用户密码过期策略

http://dev.mysql.com/doc/refman/5.7/en/password-expiration-policy.html

作者:WadeYu
出处:http://www.cnblogs.com/wadeyu/
本文版权归本人和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/wadeyu/p/5092794.html