本格的なDIYビデオホスティングの作成(nginx + php5-fpm + ffmpeg + cumulusclips)

こんにちは、ハブロビテス!



最近、私たちの会社は独自のビデオリソースを作成する必要があります。それは閉じられていますが、同時に少し公開されています。 そして最後に、それは終了し、知識とアプリケーションを共有する準備ができました。



タスクは次のとおりです。

Webカメラおよび任意のファイル(直接ダウンロードから保護するためなど)から、マルチフォーマットの一方向ブロードキャストが可能なビデオリソースを作成します。ビデオボールは、さまざまな形式とビットレートでビデオを表示できます。



基本は無料のサーバーでした! それほど強力ではありませんが、非常に適しています。



Intel®Xeon®CPU L5520 @ 2.27GHz

コア数16

RAM 16372 Mb



ビデオをデコードするとき、プロセッサの負荷は500%(約6コア)に達します。



最初から始めましょう。OSからUbuntu Server 13.04 x64を選択しました。これは、より多くの時間を費やし、実際には他のLinuxファミリーよりもよく理解しているからです。



WEBサーバーとして、nginx + php5-fpmバンドルを選択しました。これは、nginxがビデオ出力だけでなく、負荷にも非常にうまく対処できるためです。



nginxはデフォルトでストリームモジュールなしでインストールされるため、ソートしません



パッケージのビルドに必要な依存関係:



apt-get install build-essential checkinstall subversion unzip yamdi imagemagick php5-curl libssl-dev zlib1g-dev libpcre3-dev rpl php5-fpm git
      
      







ソースをダウンロード:



 cd /tmp wget http://nginx.org/download/nginx-1.5.2.zip unzip nginx-1.5.2.zip -d nginx/ rm -f nginx-1.5.2.zip cd nginx
      
      







ストリーミングに必要なモジュールをダウンロードします。



 mkdir modules git clone https://github.com/masterzen/nginx-upload-progress-module.git modules/nginx-upload-progress-module wget http://www.kernel-video-sharing.com/files/nginx_mod_h264_streaming-2.3.2.zip unzip nginx_mod_h264_streaming-2.3.2.zip -d modules/ rm -f nginx_mod_h264_streaming-2.3.2.zip git clone https://github.com/arut/nginx-rtmp-module.git modules/nginx-rtmp-module
      
      







便宜上、インストールスクリプトを作成します。



 touch nginx.sh nano nginx.sh
      
      





コンテンツ付き

 ./configure \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/lib/nginx/body \ --http-proxy-temp-path=/var/lib/nginx/proxy \ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ --with-debug \ --with-http_stub_status_module \ --with-http_secure_link_module \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-http_mp4_module \ --with-http_flv_module \ --with-http_ssl_module \ --with-http_dav_module \ --with-md5=/usr/lib \ --add-module=modules/nginx-upload-progress-module \ --add-module=modules/nginx-rtmp-module \ --add-module=modules/nginx_mod_h264_streaming-2.3.2 make -j16 (16 -  .   .    "grep -c processor /proc/cpuinfo") checkinstall
      
      







おそらくコンパイルプロセス中にエラーが発生する可能性があります。 したがって、これを行います。



auto / cc / gccファイルで、次の行にコメントします。



 #CFLAGS="$CFLAGS -Werror"
      
      







以下を開始します。

 sh nginx.sh
      
      







インストール後、必要なシンボリックリンクとディレクトリを作成します(作成されていない場合):



 ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx mkdir -p /var/lib/nginx/body mkdir /var/lib/nginx/proxy mkdir /var/lib/nginx/fastcgi chown -R root /var/lib/nginx/ wget http://nginx-init-ubuntu.googlecode.com/files/nginx-init-ubuntu_v2.0.0-RC2.tar.bz2 tar -jxvf nginx-init-ubuntu_v2.0.0-RC2.tar.bz2 -C /etc/init.d/ chmod 715 /etc/init.d/nginx /usr/sbin/update-rc.d -f nginx defaults rm -f nginx-init-ubuntu_v2.0.0-RC2.tar.bz2 rpl 'DAEMON=/usr/local/sbin/nginx' 'DAEMON=/usr/local/nginx/sbin/nginx' /etc/init.d/nginx rpl 'NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"' 'NGINX_CONF_FILE="/etc/nginx/nginx.conf"' /etc/init.d/nginx
      
      







これで、nginxとphp5-fpmのインストールが完了しました。

後で設定に戻ります。



次の行はffmpegです。 apt-getをインストールすることはお勧めできません。プロジェクトは既に廃止されており、その多くは機能しません。 適切で最新の指導を求めて、私はほぼ2晩を過ごしました。 私は隠しません、インストールに非常に良いパックを見つけました。



奇妙なことに、このプロジェクトはwww.ffmpeginstaller.comと呼ばれ、パブリックドメインのすべてのインストーラーでさえ、50ドルのサービスを提供しています。



そして、すべてが非常に簡単に行われます。

パッケージをダウンロードします。



 cd /tmp wget http://mirror.ffmpeginstaller.com/old/scripts/ffmpeg7/ffmpeginstaller.7.4.tar.gz tar -xzf ffmpeginstaller.7.4.tar.gz ffmpeg cd ffmpeg
      
      







最初のインストール後、...十分なコーデックがないことに気付きました。 巻き戻し<<<インストール前にコーデックを配置します。



 apt-get install libvpx
      
      







ffmpeg.shを開き、コーデック--enable-libvpxをインストールに追加します。



 nano ffmpeg.sh ./configure --prefix=$INSTALL_DDIR --enable-shared --enable-nonfree \ --enable-gpl --enable-pthreads --enable-libopencore-amrnb --enable-decoder=liba52 \ --enable-libopencore-amrwb --enable-libfaac --enable-libmp3lame \ --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-libvpx \ --extra-cflags=-I/usr/local/cpffmpeg/include/ --extra-ldflags=-L/usr/local/cpffmpeg/lib \ --enable-version3 --extra-version=syslint
      
      







これで準備は完了です。 インストールできます:sh start.sh



鉄の能力に応じて、インストールには約15〜20分かかります。 お茶(またはコーヒー)を飲むことができます。



インストール後、次を実行します。



 hash x264 ffmpeg ffplay
      
      







PSでクリッピングを見る



それだけです おめでとう、私たちはそれをやった!



これらのツールを管理するには、CMSが必要です。



選択肢はほとんどありませんでしたが、2つしかありませんでした(私のニーズに応えたのは積雲クリップだけでした)。

ソースコードは明確であり、あまり面倒なことなくコンポーネントを把握しました。 プロジェクトをmysqlからmysqliに書き換える必要がありました。 すべてのCMSコードは構造化され、温度は個別に柔軟に設定されます。 疑似YouTubeテンプレートをベースとして選択しました。



プレーヤーを完全にひねりました。 jwplayerはビデオストリームを切り替えることができませんでした。 githubを少し登ると、jQplayerという単純な名前の単純なプレーヤーが見つかりました。

このプレーヤーは、ストリームを簡単に切り替えることができます。 確かに、マイナスが1つあります。 ファイルの再生は最初から始まります。 そして、これは問題ではありませんでした-ビデオファイルは簡単にすぐにnginxをカットできます。



次に、プロジェクトのWebホスティングを構成する必要があります。

このプロセスを自動化する小さなスクリプトを同封します。 .HtaccessはCMSにバンドルされており、nginxはそれを理解することを拒否しているため、このWebサーバーのニーズに合わせて書き直しました。

私の設定の例
 #!/bin/bash echo -n "   : " read host echo -n "   nginx: " read users sap=/etc/nginx/sites-available/$host.conf mkdir -p /var/hosting/ touch $sap chmod 777 $sap directives="upstream backend-${host} {server unix:/var/run/php5-${host}.sock;} server { listen 80; server_name ${host} www.${host}; root /var/hosting/${host}/www; access_log /var/log/nginx/${host}-access.log; error_log /var/log/nginx/${host}-error.log; index index.php; rewrite_log on; if ($host = '${host}' ) { rewrite ^/(.*)$ http://www.${host}/$1 permanent; } location /im { rewrite ^/im/(.*)$ /cc-core/controllers/thumbs.php?$1 last; } location /videos { rewrite ^/videos/([0-9]+)/(.*)$ /cc-core/controllers/play.php?vid=$1 last; rewrite ^/videos/page/([0-9]+)/$ /cc-core/controllers/videos.php?page=$1 last; rewrite ^/videos/(most-recent|most-viewed|most-discussed|most-rated)/$ /cc-core/controllers/videos.php?load=$1 last; rewrite ^/videos/(most-recent|most-viewed|most-discussed|most-rated)/page/([0-9]+)/$ /cc-core/controllers/videos.php?load=$1&page=$2 last; rewrite ^/videos/([a-zA-Z0-9\-]+)/$ /cc-core/controllers/videos.php?category=$1 last; rewrite ^/videos/([a-zA-Z-]+)/page/([0-9]+)/$ /cc-core/controllers/videos.php?category=$1&page=$2 last; rewrite ^/videos/([0-9]+)/comments/$ /cc-core/controllers/comments.php?vid=$1 last; rewrite ^/videos/([0-9]+)/comments/page/([0-9]+)/$ /cc-core/controllers/comments.php?vid=$1&page=$2 last; rewrite ^/videos/$ /cc-core/controllers/videos.php last; } location /private { rewrite ^/private/get/$ /cc-core/controllers/play.php?get_private=true last; rewrite ^/private/videos/([a-zA-Z0-9]+)/$ /cc-core/controllers/play.php?private=$1 last; rewrite ^/private/comments/([a-zA-Z0-9]+)/$ /cc-core/controllers/comments.php?private=$1 last; rewrite ^/private/comments/([a-zA-Z0-9]+)/page/([a-z0-9]+)/$ /cc-core/controllers/comments.php?private=$1&page=$2 last; } location /members { rewrite ^/members/page/([0-9]+)/$ /cc-core/controllers/members.php?page=$1 last; rewrite ^/members/([a-zA-Z0-9]+)/$ /cc-core/controllers/profile.php?username=$1 last; rewrite ^/members/([a-zA-Z0-9]+)/videos/$ /cc-core/controllers/member_videos.php?username=$1 last; rewrite ^/members/([a-zA-Z0-9]+)/videos/page/([0-9]+)/$ /cc-core/controllers/member_videos.php?username=$1&page=$2 last; rewrite ^/members/$ /cc-core/controllers/members.php last; } location /search { rewrite ^/search(/page/([0-9]+))?/$ /cc-core/controllers/search.php?page=$2 last; } location /login { rewrite ^(.*)$ /cc-core/controllers/login.php last; } location /login/forgot { rewrite ^(.*)$ /cc-core/controllers/login.php?action=forgot last; } location /logout { rewrite ^(.*)$ /cc-core/system/logout.php last; } location /register { rewrite ^(.*)$ /cc-core/controllers/register.php last; } location /activate { rewrite ^(.*)$ /cc-core/controllers/activate.php last; } location /opt { rewrite ^/opt-out/$ /cc-core/controllers/opt_out.php last; } location /contact { rewrite ^(.*)$ /cc-core/controllers/contact.php last; } location /embed { rewrite ^/embed/([0-9]+)/$ /cc-core/system/embed.php?vid=$1 last; } location /page { rewrite ^(.*)$ /cc-core/system/page.php last; } location /translation { rewrite ^(.*)$ /cc-core/system/translation.php last; } location /notify { rewrite ^(.*)$ /cc-core/system/notify.php last; } location /language/get { rewrite ^(.*)$ /cc-core/system/language.php?get last; } location /language { rewrite ^/language/set/(.*)/$ /cc-core/system/language.php?set&language=$1 last; } location /feed { rewrite ^/feed(/([a-zA-Z0-9]+))?/$ /cc-core/system/feed.php?username=$2 last; } location /video { rewrite ^/video-sitemap(-([0-9]+))?\.xml$ /cc-core/system/video_sitemap.php?page=$2 last; } location /myaccount/upload/avatar { rewrite ^(.*)$ /cc-core/system/avatar.ajax.php last; } location /myaccount/upload/validate { rewrite ^(.*)$ /cc-core/system/upload.ajax.php last; } location /myaccount/grab/validate { rewrite ^(.*)$ /cc-core/system/grab.ajax.php last; } location /actions/username { rewrite ^(.*)$ /cc-core/system/username.ajax.php last; } location /actions/flag { rewrite ^(.*)$ /cc-core/system/flag.ajax.php last; } location /actions/favorite { rewrite ^(.*)$ /cc-core/system/favorite.ajax.php last; } location /actions/subscribe { rewrite ^(.*)$ /cc-core/system/subscribe.ajax.php last; } location /actions/rate { rewrite ^(.*)$ /cc-core/system/rate.ajax.php last; } location /actions/comment { rewrite ^(.*)$ /cc-core/system/comment.ajax.php last; } location /actions/post { rewrite ^(.*)$ /cc-core/system/post.ajax.php last; } location /actions/stream { rewrite ^(.*)$ /cc-core/system/stream.ajax.php last; } location /actions { rewrite ^/actions/mobile-(videos|search)/$ /cc-core/system/mobile_$1.ajax.php?mobile last; } location /myaccount { rewrite ^/myaccount/upload/complete/$ /cc-core/controllers/myaccount/upload_complete.php last; rewrite ^/myaccount/upload/video/$ /cc-core/controllers/myaccount/upload_video.php last; rewrite ^/myaccount/upload/$ /cc-core/controllers/myaccount/upload.php last; rewrite ^/myaccount/myvideos(/page/([0-9]+))?/$ /cc-core/controllers/myaccount/myvideos.php?page=$2 last; rewrite ^/myaccount/myvideos/([0-9]+)/$ /cc-core/controllers/myaccount/myvideos.php?vid=$1 last; rewrite ^/myaccount/editvideo/([0-9]+)/$ /cc-core/controllers/myaccount/edit_video.php?vid=$1 last; rewrite ^/myaccount/myfavorites(/page/([0-9]+))?/$ /cc-core/controllers/myaccount/myfavorites.php?page=$2 last; rewrite ^/myaccount/myfavorites/([0-9]+)/$ /cc-core/controllers/myaccount/myfavorites.php?vid=$1 last; rewrite ^/myaccount/privacy-settings/$ /cc-core/controllers/myaccount/privacy_settings.php last; rewrite ^/myaccount/change-password/$ /cc-core/controllers/myaccount/change_password.php last; rewrite ^/myaccount/subscriptions(/([0-9]+))?/$ /cc-core/controllers/myaccount/subscriptions.php?id=$2 last; rewrite ^/myaccount/subscriptions(/page/([0-9]+))?/$ /cc-core/controllers/myaccount/subscriptions.php?page=$2 last; rewrite ^/myaccount/subscribers(/page/([0-9]+))?/$ /cc-core/controllers/myaccount/subscribers.php?page=$2 last; rewrite ^/myaccount/message/inbox(/page/([0-9]+))?/$ /cc-core/controllers/myaccount/message_inbox.php?page=$2 last; rewrite ^/myaccount/message/inbox/([0-9]+)/$ /cc-core/controllers/myaccount/message_inbox.php?delete=$1 last; rewrite ^/myaccount/message/read/([0-9]+)/$ /cc-core/controllers/myaccount/message_read.php?msg=$1 last; rewrite ^/myaccount/message/send/([a-zA-Z0-9]+)/$ /cc-core/controllers/myaccount/message_send.php?username=$1 last; rewrite ^/myaccount/message/reply/([0-9]+)/$ /cc-core/controllers/myaccount/message_send.php?msg=$1 last; rewrite ^/myaccount/$ /cc-core/controllers/myaccount/myaccount.php last; } location /myaccount/profile { rewrite ^(.*)$ /cc-core/controllers/myaccount/update_profile.php last; } location /myaccount/profile/reset { rewrite ^(.*)$ /cc-core/controllers/myaccount/update_profile.php?action=reset last; } location /myaccount/message/send { rewrite ^(.*)$ /cc-core/controllers/myaccount/message_send.php last; } location /m { rewrite ^/m/v/([0-9]+)/$ /cc-core/controllers/mobile/play.php?mobile&vid=$1 last; rewrite ^/m/v/$ /cc-core/controllers/mobile/videos.php?mobile last; rewrite ^/m/s/$ /cc-core/controllers/mobile/search.php?mobile last; rewrite ^/m/$ /cc-core/controllers/mobile/index.php?mobile last; } location /system { rewrite ^/system-error/$ /cc-core/controllers/system_error.php last; } location /t { rewrite ^/t/(.*)$ /cc-core/system/translation.php last; } location / { if (!-e $request_filename){ #rewrite ^/(.*)$ /$request_uri/ permanent; rewrite ^/(.*)$ /cc-core/system/page.php last; } } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass backend-${host}; } location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|bmp)$ { access_log off; expires 10d; break; } location ~ \.(flv|mp4|webm|ogg|ogv|mp3)$ { mp4; mp4_buffer_size 1m; mp4_max_buffer_size 5m; } location ~ /\. { deny all; } } " echo "$directives">$sap sap_poll=/etc/php5/fpm/pool.d/$host.conf touch $sap_poll chmod 777 $sap_poll directives_poll="[${host}] listen = /var/run/php5-${host}.sock listen.mode = 0666 user = ${users} group = ${users} chdir = /var/hosting/${host} php_admin_value[upload_tmp_dir] = /var/hosting/${host}/tmp php_admin_value[soap.wsdl_cache_dir] = /var/hosting/${host}/tmp php_admin_value[date.timezone] = Asia/Yekaterinburg pm = dynamic pm.min_spare_servers = 10 pm.max_spare_servers = 20 pm.start_servers = 10 pm.max_children = 40" echo "$directives_poll">$sap_poll ln -s /etc/nginx/sites-available/$host.conf /etc/nginx/sites-enabled/$host.conf mkdir -p /var/hosting/$host/www/ mkdir -p /var/hosting/$host/tmp/ dir=/var/hosting/$host/www chown -R $users:$users "$dir"; find "$dir" -type d -exec chmod 0755 '{}' \; find "$dir" -type f -exec chmod 0644 '{}' \; /etc/init.d/nginx restart /etc/init.d/php5-fpm restart
      
      







以上です。 この構成は、ストリームストリームだけでなく、さまざまな形式でビデオをエンコードできます。 この記事が誰にとっても興味深いものである場合、私は喜んでストリーミングの実例を挙げます。



残念ながら、完全な内容を表示することはできませんが、 stream.etagi.comで取得したものは次のとおりです。



ご清聴ありがとうございました。



PS1 ffmpegを構築するより穏やかな方法を見つけました。 以下で修正。



1. echo "deb www.deb-multimedia.org squeeze main non-free"»/ etc/apt/sources.list

2. apt-getアップデート

3. apt-get install deb-multimedia-keyring

4. apt-getアップデート

5. apt-get remove ffmpeg

6. apt-get purge ffmpeg

7. apt-get autoremove

8. apt-get install ffmpeg x264(インストールは必要な依存関係をすべて引き上げます)

9. ffmpeg -version

(出力は、ポイント5から9からではない場合、廃止されるべきではありません)



PS2メインデザイン機能

dotdebのPHP 5.3でのみ動作します



All Articles