2015年11月7日 星期六

調整 mp4 檔案的音量 (normalize)

雖然,avidemux 也可以做 normalize,但不會用,後來找到用 ffmpeg 就可以了。

參考連結
How can I normalize audio using ffmpeg?   其中 [Manually normalizing audio with ffmpeg]
Normalize audio in an avi file

Manually normalizing audio with ffmpeg

In ffmpeg you can use the volume filter to change the volume of a track. Make sure you download a recent version of the program.
This guide is for peak normalization, meaning that it will make the loudest part in the file sit at 0 dB instead of something lower. There is also RMS-based normalization which tries to make the average loudness the same across multiple files. To do that, do not try to push the maximum volume to 0 dB, but the mean volume to the dB level of choice (e.g. -26 dB).

Find out the gain to apply

First you need to analyze the audio stream for the maximum volume to see if normalizing would even pay off:
ffmpeg -i video.avi -af "volumedetect" -vn -sn -dn -f null /dev/null
Replace /dev/null with NUL on Windows.
The -vn-sn, and -dn arguments instruct ffmpeg to ignore non-audio streams during this analysis. This drastically speeds up the analysis.
This will output something like the following:
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] mean_volume: -16.0 dB
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] max_volume: -5.0 dB
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] histogram_0db: 87861
As you can see, our maximum volume is -5.0 dB, so we can apply 5 dB gain. If you get a value of 0 dB, then you don't need to normalize the audio.

Apply the volume filter:

Now we apply the volume filter to an audio file. Note that applying the filter means we will have to re-encode the audio stream. What codec you want for audio depends on the original format, of course. Here are some examples:
  • Plain audio file: Just encode the file with whatever encoder you need:
    ffmpeg -i input.wav -af "volume=5dB" output.mp3
    
    Your options are very broad, of course.
  • AVI format: Usually there's MP3 audio with video that comes in an AVI container:
    ffmpeg -i video.avi -af "volume=5dB" -c:v copy -c:a libmp3lame -q:a 2 output.avi
    
    Here we chose quality level 2. Values range from 0–9 and lower means better. Check the MP3 VBR guide for more info on setting the quality. You can also set a fixed bitrate with -b:a 192k, for example.
  • MP4 format: With an MP4 container, you will typically find AAC audio. We can use ffmpeg's build-in AAC encoder.
    ffmpeg -i video.mp4 -af "volume=5dB" -c:v copy -c:a aac -b:a 192k output.mp4
    
    Here you can also use other AAC encoders. Some of them support VBR, too. See this answer and the AAC encoding guide for some tips.
In the above examples, the video stream will be copied over using -c:v copy. If there are subtitles in your input file, or multiple video streams, use the option -map 0 before the output filename.

步驟摘要整理

找出聲音的大小,峰值為 -7.5 dB
ffmpeg -i v1.mp4 -af "volumedetect" -f null /dev/null
[Parsed_volumedetect_0 @ 0x1589580] n_samples: 397301760
[Parsed_volumedetect_0 @ 0x1589580] mean_volume: -40.3 dB
[Parsed_volumedetect_0 @ 0x1589580] max_volume: -7.5 dB

 將聲音提升 7.5 dB
ffmpeg -i v1.mp4 -af "volume=7.5dB" -c:v copy -c:a aac -strict experimental -b:a 128k v1n.mp4

處理後,得到的值
[Parsed_volumedetect_0 @ 0x2159720] n_samples: 397303808
[Parsed_volumedetect_0 @ 0x2159720] mean_volume: -32.9 dB
[Parsed_volumedetect_0 @ 0x2159720] max_volume: -0.2 dB

分離 mp3,另用 audacity 處理
ffmpeg -i ch1.mp4 -q:a 0 -map a ch1.mp3

https://www.youtube.com/watch?v=NgedD2zKhdc
Audacity Tutorial: Amplification, Compression, Normalizing and Exporting Your Podcast

使用 compressor 將大小聲做調整,不致動態太大。
http://manual.audacityteam.org/o/man/compressor.html

2015年11月5日 星期四

PHP -- 程式員的萬用瑞士刀

一般,談到 PHP,就是用來開發網頁吧。是的,尤其,PHP 中內建了伺服器,還可以用來當臨時的網頁伺服器,直接可以用來傳送檔案。
在Windows的環境下,相對於大部分的語言,都要安裝後才能用,PHP 則是下載,解壓縮後,即可使用。熟悉它的設定後,隨時隨地都以使用。
尤其,在Windows的環境下,要寫出功能強大的 batch 程式,幾乎是不可能任務。PHP 的 command mode 可用來執行 batch 程式。因此,PHP 真的可以說是程式人員的萬用工具。

2015年10月26日 星期一

Laravel 5.0 安裝與目錄調整

建立新專案的指令如下
composer create-project laravel/laravel laravel-5.0-empty  "~5.0.0" --prefer-dist
rm vendor/compiled.php
composer update
php artisan fresh (remove this scaffolding)
安裝後,共 19.3 MB,其中 vendor 下,有4,526 個檔案,共 18.4 MB。

記得設定 config/app.php 中的時區,
// 'timezone' => 'UTC',
 'timezone' => 'Asia/Taipei',


在使用 Laravel 5.0 開發網頁時,每次要備份,vendor 目錄下,檔案很多,要備份時,花很多時間來掃描。因此希望將 vendor 由應用程式分開,可以減少備份時間。另外,storage目錄下的東西,也是無需備分的。

更進一步,再調整一下目錄,也有可能多個 App 共用 vendor。

此外,index.php 原先是放在 public 的目錄下,目錄不好設定。因而,個人習慣是,將 index.php 改為放在 App 的根目錄下,然後將程式放在子目錄下,例如 app-src 下。

調整後,希望目錄長成這樣子

[myapp]
   |
   +-- [app-src]
   |      |
   |      +- [app], [bootstrap], ...
   |  
   +-- [L5-vendor] /* this can be moved out the project */
   |  
   +-- [storage]
   |
   +-- index.php, favicon.ico, ...

在此,使用 IIS 6 的 web server,網頁的根目錄為 "\Inetpub\wwwroot"

要修改的檔案內容如下
1. 修改 la-tst/myapp/index.php
  define('BASEDIR', __DIR__.'/app-src');
  define('VENDORDIR', __DIR__.'/L5-vendor');

  require BASEDIR.'/bootstrap/autoload.php'; 

  $app = require_once BASEDIR.'/bootstrap/app.php';
  $app->useStoragePath(__DIR__.'/storage');
 
     若要將 vender 目錄移到專案外,讓多個專案共用。必須修改 config/session.php,設定不同的cookie 名稱,不然會出現 "TokenMismatchException in VerifyCsrfToken.php" 的錯誤。


2. 修改  app-src/bootstrap/autoload.php
  require VENDORDIR.'/autoload.php';

3. 修改 L5-vendor/composer 下的 4 個 autoload 檔,包括 autoload_classmap.php、autoload_files.php、autoload_namespaces.php、autoload_psr4.php
  $vendorDir = VENDORDIR;
  $baseDir = BASEDIR;

調整好後,有可能要重新啟動 web server,才能正常運作。

瀏覽的 url:
http://10.161.86.192/la-tst/myapp/index.php

在此稍加說明,我是用 composer 安裝一個空的 Laravel 5.0 專案。啟動一個新的 AP 時,建立一個新的 App 目錄,直接 copy 整個目錄,然後,進行開發。
因此,在正規的作法,composer 目錄下的檔案是以 composer 來產生。但在此,直接手動改內容,是有點不太正規,但我是打定主意,不 update 系統。因為,目前已是 Laravel 5.1 是主流,5.0 已不太會更新。

另一調整目錄方式

在使用一段時間後,還是有必要 update。就是把根目錄下的全部東西,都移到 app-把 public 目錄下的東西,提到根目錄下

調整後,希望目錄長成這樣子

[myapp]
   |
   +-- [app-src]
   |      |
   |      +- [app], [bootstrap], [vendor]...
   |
   +-- [storage]
   |
   +-- index.php, favicon.ico, ...


這樣,只要修改 index.php 的內容,能夠 include 到檔案即可,然後再改一下 storage 的目錄。這樣的目錄安排,不用修改伺服器設定,也可以 update package,缺點是,有些 artisan 的指令不能用,只能自行取捨了。

2015年10月21日 星期三

Synology NAS RS810+ 升級 8TB 硬碟


Synology NAS RS810+,購入時間為 2011年11月,當時最大的 3.5 吋 SATA 硬碟為 2TB,而且,為了省錢,只用 WD 綠標的硬碟。在 2014 年時,4TB 硬碟的價格已降至可接受的範圍,升級為 WD 紅標 4TB 硬碟。到 2015年8月,Seagate 出的 8T 硬碟,價格已降至和去年 4TB 的價格差不多了。

由於在群暉的規格中,並未明確說明 8TB 的硬碟是否可用。因此先買一顆來試用,結果可以正常抓到,且可正常運作。確定之後,再買齊其他的 3 顆,目前還在同位元資料一致性檢查中,不曉得還要跑多久。

安裝時,有一點不順之處,不曉得是不是 Seagate 為了塞下更多的 disk,少了兩個螺絲孔,因此,只好只鎖兩顆螺絲。還好,裝在機殼內,不會隨便亂動。

由於,為了儘可能有最大的可用空間,只設成一顆容錯。因此,拆下硬碟後,要儘快裝回去。硬碟裝回去,且等系統抓到硬碟後,要選擇「儲存空間1 」,再點選「管理」,然後選擇執行修復

修復的過程包括
  1.  重建,這一步驟蠻快的,恢復有一顆硬碟的容錯保護
  2.  同位元資料一致性 (consistency check),很久,而且有資料異動,又會從 0.00%從頭開始。
  3. 新增硬碟 ,好像也要很久,超過24小時。
4 顆 4TB 硬碟,可用容量 10.73TB。換上2顆 8TB硬碟後,變成 2 顆 4TB + 2 顆 8TB 時,可用容量會變成 14.31T。換到第三顆 8TB 時,在修復時,會顯示可用容量為 21.46TB,但修復後,卻仍為 14.31TB。換到四顆都是 8TB 硬碟時,修復時,提示的容量會增為 23.10TB,但在修復及一致性檢查之後,可用容量仍為 14.31TB。所以,很明顯的,這一型機器,最大容量就只能到 14.31TB。

既然,無法有更多的硬碟空間,乾脆打掉重建,變成有2顆容錯的組態,可用容量約為 14TB左右,這樣比較安心。折騰了半天,可用空間沒有增加多少,換來的只是多一點安心而已。

驚! 才剛把設定改成有2顆容錯的組態,磁碟陣列重建完,開始把資料 copy 回去,就發生抓不到硬碟的錯誤。將硬碟拔出來,重新插入,可以抓到。但是,在進行修復不到一個小時,硬碟又消失了。才用不到兩個月的硬碟,就發生這種情形,真是讓人驚出一身冷汗。還好,有多買一顆,換上去看看,能撐多久,這次就當白老鼠吧。

2016-08-05
又再來試一次, 這次買的是 WD 的紅標 NAS 碟, 一顆1萬1左右。螺絲孔位與Seagate 的8TB硬碟一樣,只能鎖2顆螺絲。
換完以後,仍然顯示只有 14.31TB。在資料備份之後,重新設定為2顆硬碟容錯,直至目前,1個月左右,還好,沒有硬碟消失的情形。還在持續觀察中。

2015年10月9日 星期五

Laravel 3.2 與 5.0 的粗略比較

在 2015 年 8 月以前,都還是用 Laravel 3.2.14 來寫網頁程式。直到最近,在 Laravel 5.1 出來之後,想要把程式做比較好的架構,才開始使用 Laravel 5.0。只用 Laravel 5.0,是因為 Laravel 5.1 需求為 PHP 5.5.9 以上,但目前主要還是使用 Windows 2003 Server,只能安裝 PHP 5.4,因而只好使用 Laravel 5.0。
一些小小的比較,列表如下
Laravel 3.2   Laravel 5.0
source files439 files4595 files
source-size3MB15MB
cache scripts151 scripts587 scripts
cache used-memory   7.6 MB13.29 MB
Requests per second   290.33 [#/sec]  122.94 [#/sec]

其中,針對安裝後,空的 framework,source files 為其所包含檔案數,source-size 為其使用的硬碟空間。然後在執行時,使用 Zend OPcache 加速,得到其統計資料,scripts 為 cache 住的 script 數目,userd_memory為其使用的記憶體大小。

速度比較,使用 Apache 的 ab 程式,簡單的測試其處理效能,程式只是傳回 5 byte 的 "Hello" 字串,Requests per second 為每秒回應的 request 數。不過,在效能上,通常程式並非是決定性的影響,反而是資料庫的存取決定了應用程式的效能,因此,要提升程式的效能,應對資料庫的調校下一些功夫。

從以上的粗略比較,得知,不論檔案數和使用的空間,Laravel 5.0 都大約是 Laravel 3.2.14 的 2 倍以上;而相對的,在效能上,前者大約為後者的一半以下。隨著硬體效能的進步,寫程式越來越不需要在意效能的問題,包括大小與速度。在意的是,開發的速度和所花的精力,還有日後在維護程式上的方便。這也是使用較新的 framework 的主要考量。

2015年9月28日 星期一

MVC 使用心得

從知道 CodeIgniter 之後,使用 MVC 架構來開發網頁也好一段時間了。到現在,若沒有 MVC framework,就不曉得怎麼動手了。

剛開始時,會想要遵循 Model/View/Controller 的拆解原則來安排程式。可是在移轉一些舊有的 ASP 或 PHP 程式之後,發現最基本的架構,只用到 Controller + View,然後把一些共用的程式碼抽出來,放到 Lib 下。而資料庫存取,大都用 DB query builder 來完成。因此,那個 Model 就變得很奇怪的角色,不曉得該如何歸類,或該放些什麼進去。

到了開始使用 Laravel 5.0 時,發現它把 Model 的目錄給拿掉了。有明確目錄位置的是 Controller 和 View,其餘的自己看著辦。看來大勢所趨,網頁程式,大抵是就是,Route + Contoller 扮演 dispatcher 的功能,View 輸出結果。其餘的,就是 Bussiness Logic 了,大概就是建一堆 Class,擺在適當的目錄下。

2015年9月12日 星期六

通識教育,是營養學分?

通識教育,是營養學分?
其實,在社會上工作之後,會發現,一個人所學範圍越廣,知識越廣,瞭解的層面越寬廣,發展可能性就會越好。
另一方面,依據大部人的經驗,很多人在畢業之後就業,和他在校所學,百分之七八十都沒關係。
所以,通識課程,在廣面來講,對於個人來講是有幫助的。

引述 臺灣大學,江才健老師的 <科學在文化中的定位和挑戰>
連結 -- http://get.aca.ntu.edu.tw/getcdb/handle/getcdb/253687

網誌存檔