2017年4月30日 星期日

執行備份的 wine 應用程式

安裝完 wine 的應用程式後,預設全都會放在 ~/.wine 底下, 可以先切換到家目錄(~)再將 .wine 目錄打包備份,之後把 wine.tgz 移至別處加以收藏,在終端機輸入以下命令將 .wine 打包:
       cd  ~
       tar  -zcvf  wine.tgz  .wine

如果有任何意外破壞了 .wine 的目錄結構,可以先刪掉該目錄(rm -rf .wine)再藉由回存指令(tar  -zxvf wine.tgz), 就可以恢復該目錄所有檔案,並不需要將應用程式重頭安裝.至於剛安裝完 wine 執行 winecfg 時,一開始會要求從網路下載穩定版本的 gecko 及 mono, 但使用備份檔的好處是因為先前已安裝過並備份到 wine.tgz 裡面了, 因此不用再從網路下載資源浪費時間.假設 /path_to 是 wine.tgz 想要解壓縮之位置,而 /path_from 是備份檔案 wine.tgz 所收藏之地方,在終端機輸入以下命令,用打包的檔案還原檔案:
        sudo  mkdir   /path_to
        sudo  chown  mint   /path_to
        cd   /path_to
        tar  -zxvf   /path_from/wine.tgz

如果 .wine 的目錄不是放在家目錄(~/.wine)底下,可以透過 linux 的 env 命令(設定環境變數並執行後續命令,相關用法使用 man env 就可以瞭解),先將環境變數 WINEPREFIX 指向 .wine 所在的位置,接著用 wine去 載入應用程式來執行.想要執行 windows 內建的 notepad.exe,在終端機輸入:
        env  WINEPREFIX="/path_to/.wine"   wine    "c:\\windows\\notepad.exe"

如果覺得用 env 執行命令很繁瑣,也可以先切換到家目錄(~), 再用符號連結(symbolic link)的方式去建立連結,在終端機輸入命令:
        cd  ~
        ln   -sf   /path_to/.wine    .wine

之後在終端機用 wine 就可以載入應用程式並執行:
      wine    "c:\\windows\\notepad.exe"

另外用 wine regedit 去手動設定 windows 的內建環境變數, 可以在裡面添加 path:
       REGEDIT4

       [HKEY_CURRENT_USER\Environment]
       "path"="%path%;c:\windows"

或將上述內容存成 winenv.reg 檔案, 在終端機輸入 wine regedit 檔名,把要註冊的內容放進 windows 環境變數內:
       wine regedit  winenv.reg

往後就不用再打一長串的路徑,直接在終端機用 wine 載入應用程式,windows 會自動用 path 所指定的目錄去找尋執行檔來執行,因此要執行內建的 notepad.exe 只要在終端機輸入 :
      wine notepad

在 linux mint上有更直接的用法,去編輯一個檔案,將副檔名以 .desktop 命名,在桌面進入點的段落 [Desktop Entry] 敘述中包含 Exec 及 Type 這兩個關鍵字,其中 Exec 讓它等於上述 linux 的 env 命令,將需要執行 wine 程式的相關參數隨後填入,而 Type 則直接等於 Application 這個字, 類似像這樣:
     [Desktop Entry]
     Exec=env  WINEPREFIX="/home/mint/.wine"  wine  "c:\\windows\\notepad.exe"
     Type=Application

將它存檔,先隨便取個名字(例如 notepad.desktop), 之後用滑鼠右鍵將它改成中文名,要執行時只要用滑鼠左鍵雙擊該檔案就能直接執行,相當方便.

2017年4月23日 星期日

用 nodejs 內建的 Promise 語法執行 child_process 外部程式 ffmpeg


定義 function ffmpeg,記得要將可執行檔 ffmpeg 放在目前目錄底下:
1. 這是原始function的定義,傳進來參數命名為 options,  檔名: child.js
      function ffmpeg(options) {
          const child = require('child_process');    // 調用內建child_process程式庫
          var cmd =__dirname+'/ffmpeg ' + options;   // 傳給child_process 時所需要的參數
          child.exec(cmd,function (err, stdout, stderr) { // child_proces開始處理指令
                  if( err )   console.log('出錯了:'+err+'\nstderr:'+stderr);
                  else       console.log('成功了:'+stdout);
           });  // end child_process
       } // end of ffmpeg
       ffmpeg('-version'); // 呼叫 ffmpeg 函式:

2. 這是將上述 function ffmpeg(options )轉化成 Promise 的用法, 檔名: child.js
      function ffmpeg(options) {
           return new Promise(function (ok,ng) {//回傳 closure,將會有兩個callback function
               const child = require('child_process');
               var cmd =__dirname+'/ffmpeg ' + options;
               child.exec(cmd, function (err, stdout, stderr) {
                   if(  err )  ng('出錯了:'+err+'\nstderr:'+stderr);// 回調(callback) ng
                   else       ok('成功了:'+stdout);   // 回調(callback) ok
               }); // end child_process
           }); // end of closure
       } // end of ffmpeg
      // 轉化成 Promise,就可以呼叫 then( ) 及 catch( ), 執行指令:
       ffmpeg('-version').then(console.log).catch(console.log);
      // 其中 then( ) 裡面的 callback 對應到 ok, 而 catch( ) 裡面的 callback 對應到 ng

3.  存檔用 nodejs 執行
       js  child.js


p.s. 轉檔時, 避免操壞硬碟, 改用 RAMDISK, linux 的 ramdisk 位置在 /dev/shm, 參考範例:
    var fs = require('fs'),
    var dirRAM='/dev/shm/hls'; // create a directory in ramdisk
    fs.mkdir(dirRAM, function(err) {
        fs.symlink(dirRAM, __dirname + '/hls', function(errlink ) { // create a symbolic link
            process.chdir(__dirname + '/hls'); // enter to the directory
            ffmpeg('-i '+ __dirname + '/oceans.mp4  -hls_flags delete_segments playlist.m3u8').then(console.log).catch(console.log); // media file transcode
        })
    })

2017年4月20日 星期四

下載 ffmpeg-3.3 自行編譯,讓它支援 pthreads, librtmp, libx264, libvpx, libx265, libvorbis, libmp3lame, libass 方便轉換影音檔

1. 上官網先下載原始碼(source code) 並解壓縮:
    wget http://ffmpeg.org/releases/ffmpeg-3.3.tar.bz2
    tar -xvf ffmpeg-3.3.tar.bz2
    cd ffmpeg-3.3
或是利用 git 複製出整個 repository:
    git clone https://git.ffmpeg.org/ffmpeg.git

2. 下載編譯環所需要的標頭檔:
sudo apt-get install librtmp-dev libx264-dev libvpx-dev libx265-dev libvorbis-dev libmp3lame-dev libass-dev yasm

3. 設定環境並開始編譯:
  ./configure --enable-gpl --enable-pthreads --enable-librtmp  --enable-libx264 --enable-libvpx --enable-libx265 --enable-libvorbis --enable-libmp3lame --enable-libass
  make

4. 複製 ffmpeg 到 /usr/bin
   sudo cp ffmpeg /usr/bin


5. 一些使用 ffmpeg 的範例:
   列出 webcam 支援的格式:
       ffmpeg -f v4l2 -list_formats all -i /dev/video0
   
   讓 ffmpeg 在背景擷取 webcam 影像,轉換產生 http live streaming (HLS) 所需要的檔案:
   ffmpeg -f v4l2 -i /dev/video0 -hls_flags delete_segments playlist.m3u8

   讓 ffmpeg 在背景擷取mp4檔案,轉換產生每個ts檔大約是10秒的HLS檔案:
   ffmpeg -i oceans.mp4 -hls_time 10 playlist.m3u8

   
   架設 http live streaming server 只要架設好 http server,再將轉換完成的HLS檔案放在讓 HLS client 可以下載到的地方,一旦下載完 playlist.m3u8 就可以隨後播放裡面的ts影片擋了.

bash 變數, 錢字符號 $, ${ }, $( ), $(( )) 不同點

參考文章: http://stackoverflow.com/questions/27472540/difference-between-and-in-bash-scripting
設定 bash 的變數 var 成為一個字串, 例如:  
     var = 'This is a test'
若要將變數內容取出來,在變數名稱前面加上錢字符號 $ 就可:
     echo $var
如果變數後面有跟隨其它字元時,用大括號刮起來以便區別成變數,因此 $ 與 ${ } 是相同用法.
     echo ${var}string,  append some string behind
但 $( ) 在小括號裡的是一個指令, 會被 bash 先取出來執行,執行完的輸出就是變數內容, 簡稱指令變數, 例如:
     echo  目前目錄是:$(pwd)
用雙小括號變數可以做簡單整數的加減乘除(+-*/)四則運算,:
   echo $(( 5-19+2*3 + 3/3 ))

bash 命令列的管線

參考文章: http://stackoverflow.com/questions/24793069/what-does-do-in-bash
標準輸入(stdin)的內建編號等於 0
標準輸出(stdout)的內建編號等於 1
標準錯誤(stderr)的內建編號等於 2
語法:
1. 內建編號 >  檔案名稱    : 若忽略內建編號,指的是將標準輸出轉向到檔案名稱, 相當於:  1> 檔案名稱
2. 內建編號 &> 檔案名稱  : 若忽略內建編號,指的是將標準輸出及標準錯誤都同時轉向到檔案名稱
3. 內建編號 >& 內建編號或檔案名稱  : 若忽略內建編號,指的是將標準輸出及標準錯誤都同時轉向


至於 2 與 3 有何不同呢? 舉個例子來看,  先產生一個文字檔 t:
       echo  This is a test file > t
列出 t 及 txxx 檔案的內容, 將標準輸出轉到 a, 並將標準錯誤轉到 b
       cat t  txxxxx      1>a   2>b
再來將 t 及 txxx 檔案的內容列印出來, 管線將標準輸出及標準錯誤同時轉到檔案 c
無論用  cat  t  txxxxx  &>  c 或  cat  t  txxxxx   >&  c 都得到相同結果,但
用   cat t  txxxxx      &>  1 卻是產生一個案名稱 1, 不會輸出到螢幕上.
而   cat t  txxxxx     >&  1 則是輸出到標處準輸出(螢幕)上, 不會另外產生一個檔案.

因此要使用 &> 或 >& , 就看要輸出是新檔案還是內建編號(0,1,2), 若要輸出到一般檔案就用 &> , 若要輸出到內建編號就要另外使用 >&了.



2017年4月17日 星期一

玩玩 Gstreamer

先安裝: sudo apt-get install gstreamer-1.0  libgstreamer1.0-dev gstreamer-tools
參考網址:
1.  https://gstreamer.freedesktop.org/documentation/tutorials/basic/index.html
2.  https://gstreamer.freedesktop.org/documentation/tutorials/basic/gstreamer-tools.html
程式範例:
      git clone git://anongit.freedesktop.org/gstreamer/gst-docs
      cd ./gst-docs/examples/tutorials
      gcc basic-tutorial-1.c -o basic-tutorial-1 `pkg-config --cflags --libs gstreamer-1.0`
      ./basic-tutorial-1

3. Gstreamer 名詞解釋:
    Elements: Gstreamer 的基礎單元
        例如一些內建的基礎單元: videotestsrc, decodebin, videoconvert, xvimagesink 等等,
        用 gst-inspect-1.0|egrep 查看說明:
        gst-inspect-1.0|egrep "videotestsrc|decodebin|videoconvert|xvimagesink"

    Pipeline: 管線, gst-launch-1.0 用 ! 將處理單元串接在一起, 根據輸入的順序由左至右一一串接資料流, 這種將資料流與處理單元互相串接集合成套稱之為管線

    GstPad: Gstreamer 用來作為溝通的端口稱之為 pads,簡稱 GstPad, 分別有:
            Sink pad: 作為資料吸入端
            Source pad: 作為資料輸出端
 
    Filter: 過濾中間端,有兩個端口(GstPads),端口型態一邊是輸入端(sink pad),另一邊則是輸出端(source pad)
 
    Property: 屬性, 由成對的 key=value 所組成的敘述, 例如:
                  uri="http://127.0.0.1:8080/movie.mp4"
                  uri="file:///home/mint/movie.mp4"
                  location="movie.mp4"

    Muxed: 例如一個檔案帶有 video 及 audio 資料流的稱之為 multiplexed (簡稱 muxed) file,
           常見的資料流格式: Matroska(MKV), Quick Time(QT,MOV), Advanced System Format(ASF), MWV, WMA ...等等

    Demuxer:資料流的解開程序稱之為 demuxers, 有一個吸入端(single sink pad), 多個輸出端(multiple source pad)
        例如decodebin就有一個吸入端(sink pad)及兩個輸出端分別是audio及video(source pads),用來分離出影像及聲音用

4. Gstreamer 常用處理單元:
    展現聲音單元通常用 osssink, 其他還有 "directsoundsink", "esdsink", "alsasink", "osxaudiosink", or "artsdsink".
    展現影像單元通常用 xvimagesink, 其他還有  "d3dvideosink", "ximagesink", "sdlvideosink", "osxvideosink", or "aasink".
    當處理單元無法連結時,有可能是格式不相容,可以先行轉換:
    影像傳換用: videoconvert
    聲音傳換用: audioconvert,  聲音取樣率轉換用 audioresample

5. 播放範例:
    播放影像測試信號源, 花樣採用第 11 號影像源
    gst-launch-1.0  videotestsrc pattern=11 ! videoconvert ! autovideosink
 
    擷取檔案 oceans.mov,由 decodebin 先分離出聲音與影像, 再透過聲音/影像轉換器轉換後分別交由alsasink及xvimagesink播放出來
    gst-launch-1.0  filesrc location=oceans.mov ! decodebin name=decoder decoder. ! queue ! audioconvert ! audioresample ! alsasink decoder. ! videoconvert ! xvimagesink




 

2017年4月16日 星期日

讓 c++ 程式與 Javascript 做朋友

參考網址: https://nodejs.org/api/addons.html
node-gyp 是編譯 nodejs 模組的工具,用 c++ 語言寫模組的程式,經由node-gyp開發工具編譯成 .node 模組, javascript 程式則透過 require 將自訂模組引進加以運用:

1. 先安裝 node-gyp
    sudo npm install node-gyp -g

2. 用 JSON 語法,將目標(targets)需求(所要定義的模組名稱 target_name, 及原始c++檔案名稱 sources ),寫入 binding.gyp 檔案,例如:
// binding.gyp
    {  "targets": [
          {
                "target_name":  "hellocc",
                "sources":  [ "hello.cc" ]
          }                    
    ]}

3. 開始寫 c++ 程式,先引入標頭檔 node.h,所有取用 javascript 的部份,要使用node.h內所提供的方法及宣告, 最主要部份是編寫 Method這個c++程式,  而init()主要是將c++的程式模組名稱匯出,類似 module.exports = { } 這個動作,最後交由巨集 NODE_MODULE(hellocc,init) 將 javascript 模組名稱與c++程式相互連結.
 // hello.cc:
    #include < node.h >
    namespace demo {
          using v8::FunctionCallbackInfo;
          using v8::Isolate;
          using v8::Local;
          using v8::Object;
          using v8::String;
          using v8::Value;
          void Method(const FunctionCallbackInfo < Value >& args) {
                  Isolate* isolate = args.GetIsolate(); // 獲取輸出入參數的方法
                  args.GetReturnValue().Set(String::NewFromUtf8(isolate, " cc"));// 回傳字串 cc
          }
         void init(Local < Object > exports) {
                NODE_SET_METHOD(exports, "hello", Method); // exports.hello = Method
         }
        NODE_MODULE(hellocc, init)
   }

4. configure 讓 node-gyp 根據 binding.gyp 的內容,自動建立 node-gyp 編譯環境,並將檔案寫進 build 目錄內:
     node-gyp configure  

5. build 開始編譯 hello.cc,如果沒意外的話,將會產生 hellocc.node 模組
     node-gyp build

6. 寫一個  javascript 引進該模組使用,已經編譯好的模組通常放在 ./build/Release下面:
    // hello.js
    const addon = require('./build/Release/hellocc'); // 引入自訂模組
    console.log(addon.hello()); // 呼叫模組程式 hello( ), 並印出傳回來的字串

7. 執行 javascript, 印出  cc
    js hello.js

8. clean 刪除編譯環境,會將整個目錄 build 全部移除.
    node-gyp clean