網頁

2015年5月4日 星期一

ffmpeg create thumbnail from local or remote video. with Ruby on Rails example.

ffmpeg create thumbnail from local or remote video.
Important point is -ss is before input file. If you place after input file And remote video. create thumbnail will very slow.

local video

$ ffmpeg -ss 5.3 -i "/home/ubuntu/video.mpg" -vframes 1 -f image2 "/home/ubuntu/thumbnail.jpg"

remote video

$ ffmpeg -ss 5.3 -i "http://example.com/video.mpg" -vframes 1 -f image2 "/home/ubuntu/thumbnail.jpg"

Ruby on Rails example

hr = %x[ffmpeg -loglevel error -ss #{m[:time_of]} -i \"#{input_file}\" -y -vframes 1 -vf scale='min(#{size}\\, iw):-1' -f image2 \"#{output_file}\" 2>&1]
  • -loglevel error: when error show message.
  • -ss: time offset. need before input file
  • -i: input file
  • -y: force overwrite output file
  • -vframe: Set the number of video frames to output. This is an alias for -frames:v
  • -vf scale='min(#{size}\\, iw):-1': set thumbnail output size.
  • -f image2: For extracting images from a video
  • "output_file": thumbnail output file
  • 2>&1: return error message

2013年12月4日 星期三

FreeBSD console顯示中文

資料來源:http://www.wretch.cc/blog/iantsai/13572128

裝好 FreeBSD 後因為使用 SSH 或 Console 遠端登入,中文顯示會亂碼,所以必須要修改 LOCALE 設定,才能正常顯示出中文編碼。

2. 使用csh /etc/csh.cshrc 加入下面三行後重開機即可顯示中文。

#vi etc/csh.cshrc
setenv LC_CTYPE en_US.ISO8859-1
setenv LC_ALL zh_TW.UTF-8
setenv LANG zh_TW.UTF-8

3. 若使用 BASH ,則在 /etc/profile 中加入下面三行後重開機就可顯示中文。

LC_CTYPE=en_US.ISO8859-1 ; export LC_CTYPE
LC_ALL=zh_TW.UTF-8 ; export LC_ALL
LANG=zh_TW.UTF-8 ; export LANG

2013年11月26日 星期二

Pound HTTPS Configuration

轉貼自:http://www.project-open.org/en/howto_pound_https_configuration

Pound HTTPS Configuration

The following steps will guide you through the generation of a self-signed certificate for your ]project-open[ server.
During the process you will create:
  • server.key: This is a 1024 bit random string ("private key") that uniquely identifies your server
  • server.csr: This is a "Certificate Signing Request" file. You can send this to a Certificate Authorities (CA), or sign it yourself.
  • server.crt: This is a "certificate" that certifies that server.key belongs to you.
  • server.pem: This is the file that Pound needs to work correctly. A PEM file is a bundle of a the "server.key" priviate key and a certificate.
  1. Generate an RSA private key for the server:
    openssl genrsa -out server.key 1024
  2. Remove the passphrase from the key. Please make sure that nobody will have access to this file except for you. Otherwise the security of your server is at risk:
    cp server.key server.key.org
    openssl rsa -in server.key.org -out server.key
  3. Create the Certificate Signing Request file, or CSR:
    openssl req -new -key server.key -out server.csr
    You will have to provide certain information for your CSR. Here are some sample values for ]project-open[:

    Country Name (2 letter code) [GB]: ES
    State or Province Name (full name) [Berkshire]: Catalonia
    Locality Name (eg, city) [Newbury]: Barcelona
    Organization Name (eg, company) [My Company Ltd]: Project Open Business Solutions S.L.
    Organizational Unit Name (eg, section) []:
    Common Name (eg, your name or your server's hostname) []: www.project-open.org
    Email Address []:webmaster@project-open.com
    A challenge password []:
    An optional company name []:
  4. Now you could go to some Certificate Authority in the Web (for example: http://www.instantssl.com/ currently offers free certificates for 90 days) and sign your key there. As a result, you will receive a "certificate" file that you can save as "server.crt".
  5. As an alternative you can sign the key yourself.
    The server.crt certificate will be technically valid. However, your browser will show a security warning if it encounters such a self-signed certificate:
    openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  6. Verify your certificat. The following command should output some data, and not an error message:
    openssl x509 -in server.crt -text
  7. Create a PEM file:
    openssl x509 -in server.crt -out server.pem
    openssl rsa -in server.key >> server.pem
    ­
  8. Now you can add a HTTPS listener configuration to your pound.cfg configuration file:
    ListenHTTPS
      Address 0.0.0.0
      Port    443
      Cert    "/etc/pound/server.pem"
    End

    Older versions of Pound (<2.2) may require a different listener configuration, please consult the man-page of your installed version of Pound.
The new configuration will be come active after restarting Pound (/etc/init.d/pound restart).
You can execute "netstat -nlp" to list all network connections. In the upper part your should see something like this:
...
tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      24804/nsd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      375/pound
...
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      375/pound
...­
  • ­ 0.0.0.0:8000: This is the AOLserver itself
  • 0.0.0.0:80: This is Pound listening for unincrypted HTTP connections
  • 0.0.0.0.443: This is the Pound HTTPS listener.
Pound will write any errors into /var/log/messages.

2013年9月3日 星期二

Exim4設定smtp伺服器與檢測

Debian 預設使用Exim4,當伺服器IP為外部IP時,預設值即可使用localhost的smtp來發送信件。但若為內部IP機器時,需進行設定如下,才可正確發信。
發信的成功失敗,可查看/var/log/exim4/maillog。


以下轉貼自【安裝筆記】Exim4 郵件伺服簡單設定與檢測

Debian Etch 套件預設是使用 Exim4,在安裝 base system 套件後即自動啟動,但無法寄出到 Internet,察看 /var/log/exim4/mainlog 訊息後:
 ** xxx@gmail.com R=nonlocal: Mailing to remote domains not supported

1.編輯並修改 /etc/exim4/update-exim4.conf.conf 下列兩行:
 dc_eximconfig_configtype=’internet’ #預設為 local
 dc_smarthost=’msxx.hinet.com’ #遠端的SMTP主機,利用它來發送郵件

2.修改 /etc/mailname ,改成你所在寄送郵件的領域(domain)。

3.執行 exim4 -bV 可測試 exim4 的配置有否出錯。

4.重新啟動 exim4:
 # /etc/init.d/exim4 restart

5.外部郵件發送測試
 # exim4 -bt kenming.wang@gmail.com

使用rsync建立長時間且不佔硬碟空間的備份

這裏不說明太多rsync其它的使用方式,只重點在“使用rsync建立長時間且不佔硬碟空間的備份”。

測試時可加上-n, --dry-run參數,就不會實際同步大量資料
rsync -n

重點指令為:
rsync -a -e ssh --link-dest=/backup/2013-09-02 x.x.x.x:/path/to/backup /backup/2013-09-03

這個方式會備份IP:x.x.x.x 的資料path/to/backup,備份到本機的/backup/2013-09-03,並參考前一天/backup/2013-09-02的資料,如此已存在的資料就用hard link的方式,從2013-09-02建立到2013-09-03中,就不會實際佔用空間,只有新檔案才會佔用空間。

第一次同步時,2013-09-02不在在,只會出現提示訊息,如
--link-dest arg does not exist: /backup/2013-09-02
往後只要前一天存在就不會重覆佔用空間。
而為了避免意外,如中間可能因故漏了幾天沒備份,因此設定--link-dest的路徑時,可往前查找幾天,如2013-09-02不存在,就再往前找2013-09-01,如此才不會因--link-dest設定路徑不存在,又重覆佔用了一次空間。

而確認是否為hard link,可使用ls -i (檢查檔案的inode number),比對兩個日期中的同一檔案,若相同則代表為hard link,實體為同一個,不會多佔硬碟空間。

若要保留一個月的備份資料,只要刪除2013-08-02之前的資料夾即可。

stderr and stdout to file (sh / bash / csh / tcsh)



stderr and stdout to file

sh: (time date) > wa.txt 2>&1
bash: (time date) >& wa.txt or (time date) &> wa.txt or (time date) > wa.txt 2>&1
csh/tcsh: (time date) >& wa.txt

There are 3 file descriptors, stdin(0), stdout(1) and stderr(2) (std=standard).

2013年6月7日 星期五

在Javascript中更好的計時器,取代Date,用來計算執行時間等應用

一般在javascript中要計算某段程式的執行時間,可以使用Date物件。
用法是Date.now() 或 new Date().getTime(),用以取得microsecond(毫秒)的目前時間數值。
在執行函數的前後取得並相減,以取得執行時間,例如:
var ticks_start = Date.now();
RunTestFunction();
var ticks_end = Date.now();
alert(ticks_end - ticks_start);

但一些指令的時間小於毫秒時,根本結果不是0就是1,看不出所以然。
從這篇文章“A better timer for JavaScript”得到了更小單位時間的方法。
window.performance = window.performance || {};
performance.now = (function() {
  return performance.now       ||
         performance.mozNow    ||
         performance.msNow     ||
         performance.oNow      ||
         performance.webkitNow ||
         function() { return new Date().getTime(); };
})();

再將程式改為以下,即可測得0.x毫秒的執行時間:
var ticks_start = performance.now();
RunTestFunction();
var ticks_end = performance.now();
alert(ticks_end - ticks_start);