一個簡單視頻網站開發小記

前言php

視頻格式轉換,視頻圖片截取,視頻存儲設計,大文件上傳處理以及相關配置,前端視頻播放,視頻播放流暢度,每個都不簡單,都須要花心思思考解決方法!基本上每一個問題,都是使用相應的開源庫!html

 

需求背景前端

手機拍攝的視頻愈來愈多,然而手機的容量有限,故想到開發一個手機視頻保存與播放的簡單網站,只須要實現個人需求便可!mysql

 

技術實現方案linux

VBox+LNMP+Laravel5.1+ffmpeg+ckplayer
網站運行於筆記本虛擬機中nginx

 

運行系統相關說明laravel

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.3c++

 

實現效果圖git

 

源代碼下載github

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

相關文章
相關標籤/搜索