備忘録

備忘録

Pythonで画像をトリミングする方法

Ⅰ. はじめに

タイトルの通り「Pythonで画像をトリミングする方法」です。

Ⅱ. やり方

1. Pillow をインストールする
pip install Pillow
2. サンプルプログラムを書く
from PIL import Image

img = Image.open('lena.png')
# 画像サイズを取得する
# width, height = img.size
imgCrop = img.crop((180, 160, 400, 400))
imgCrop.save('out.png')

実行結果

lena.png

out.png

nginx でストリーミングサーバを作る方法

Ⅰ. はじめに

タイトルの通り「nginx でストリーミングサーバを作る方法」です。

Ⅱ. 環境

Ⅲ. やり方

1. nginxのビルドに必要なものをインストールする
yum -y install wget gcc pcre-devel openssl openssl-devel git libxslt-devel
# apt -y install wget gcc libpcre3 libpcre3-dev openssl libssl-dev git libxslt1-dev zlib1g-dev build-essential
2. nginx-rtmp-module を clone する
git clone https://github.com/arut/nginx-rtmp-module.git
3. nginx をダウンロードする

2020/12/21 時点で 1.18.0 が stable です。

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
4. nginx をビルドする
./configure --sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_xslt_module \
--add-module=/root/nginx-rtmp-module

make
make install

groupadd nginx
useradd -g nginx nginx
usermod -s /bin/false nginx
5. init scriptを作成する

以下を全てコピペする

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

vim /etc/init.d/nginx
chmod 755 /etc/init.d/nginx
  • systemd

https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

vim /lib/systemd/system/nginx.service
6. nginx の設定を変更する
$ vim /etc/nginx/nginx.conf

http {
  server {
    listen 80;
    root /var/www/html;
  }
}

rtmp {
    server {
        listen 1935;
        access_log /var/log/nginx/rtmp_access.log;
         application live001 {
            live on;
            wait_video on;
            hls on;
            hls_path /var/www/html;
            hls_fragment 1s;
            hls_type live;
            # 配信開始時にPOSTされる
            # on_publish http://localhost/api/rtmp/on_publish;
            # 配信終了時にPOSTされる
            # on_publish_done http://localhost/api/rtmp/on_publish_done;
        }
    }
}
7. nginx を起動する
service nginx start

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=1935/tcp --permanent
firewall-cmd --reload
8. 配信を開始する

OBS 等のRTMPに対応した配信クライアントを利用します

URL 6で設定した application の値と合わせます
ストリームキー 何でもOK


9. 閲覧用のWebページを作成する

index.html

<link href="https://vjs.zencdn.net/6.6.3/video-js.css" rel="stylesheet">
<div>
  <video id="player" width="960" height="540" class="video-js vjs-default-skin" controls>
    <source src="http://[your ip address]/[your stream key].m3u8" type="application/x-mpegURL">
  </video>
</div>

<script src="https://vjs.zencdn.net/6.6.3/video.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.14.1/videojs-contrib-hls.js"></script>
<script>
  let player = videojs('player')
  player.play()
</script>
10. 実行結果


その他

Q. 視聴者数をリアルタイムで知りたい

A. rtmpで接続されたクライアントのみ統計情報が取得できます。
https://github.com/arut/nginx-rtmp-module/wiki/Getting-number-of-subscribers

その他方法

Cordova + Vue.js で Cordova のイベントを拾う方法

Ⅰ. はじめに

タイトルの通り「Cordova + Vue.js で Cordovaのイベントを拾う方法」です。

Ⅱ. やり方

1. vue-cordova をインストールする
npm install vue-cordova
2. プログラムを書く

src/main.js

import Vue from 'vue'
import VueCordova from 'vue-cordova'
Vue.use(VueCordova)

Vue.cordova.on('deviceready', () => {
  alert('Cordova : device is ready !')
})
3. ビルドする
npm run build
cordova run android
実行結果

Cordovaで cordova-plugin-fcm を追加すると発生するエラーを消す方法

Ⅰ. はじめに

以下のコマンドでエラーを再現できます

$ cordova plugin add cordova-plugin-fcm
$ npm run build
$ cordova run android
Android Studio project detected
Invalid data, chunk must be a string or buffer, not object

Ⅱ. やり方

2. 以下のファイルに貼り付ける
YourApp\plugins\cordova-plugin-fcm\scripts\fcm_config_files_process.js
3.ビルドする
$ cordova run android
(省略)
BUILD SUCCESSFUL

その他

※2019/09/10 追記
cordova-plugin-fcm を使う理由がなければ、forkして更新され続けている以下のプラグインを利用したほうが良いです。
https://github.com/andrehtissot/cordova-plugin-fcm-with-dependecy-updated

NuGetのキャッシュを全て削除する方法

Ⅰ. はじめに

タイトルの通り「NuGetのキャッシュを全て削除する方法」です。

Ⅱ. やり方

以下のコマンドを実行するだけです。

nuget locals all -clear 

実行結果

私の環境の場合、約10GB容量が空きました。

Clearing NuGet HTTP cache: C:\Users\user01\AppData\Local\NuGet\v3-cache
Clearing NuGet global packages cache: C:\Users\user01\.nuget\packages\
Clearing NuGet Temp cache: C:\Users\user01\AppData\Local\Temp\NuGetScratch
Local resources cleared.