2021年2月19日 星期五

Homebrew:Mac 必裝的套件管理工具

Homebrew 可以做什麼?

Homebrew 可以在 Mac 上安裝系統沒有的套件,例如 Wget 這種工程師常用的軟體。不過 Homebrew 不是只有工程師才用的到,一般常見的軟體、下載 YouTube 的程式,其實都可以透過 Homebrew 來取得。 Homebrew 也有點像是 Ninite 這套軟體,我們不需要搜尋軟體的官方網站,並手動下載、安裝軟體。尤其是許多需要註冊登入才能下載的軟體,例如:Adobe Creative Cloud、Microsoft Office 都可以透過 Homebrew 來搜尋並安裝。

安裝 Homebrew

在 Mac 的終端機貼上以下指令:
/usr/bin/ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

安裝套件

安裝套件的方式很簡單,例如安裝 Wget 一樣透過指令:
brew install wget

更新套件

brew update && brew upgrade && brew cleanup
一氣呵成的指令內容分別是: brew update:更新 Homebrew 及套件清單 brew upgrade:更新所有套件 brew cleanup:清除暫存檔 不過在 brew 軟體清單內可能大部分都是比較「工程」的套件,因此如果要安裝一些「尋常」的軟體,我們可以安裝 Homebrew-Cask 這套軟體,來取得更多的應用程式。

安裝 Homebrew-Cask

輸入指令:
brew tap caskroom/cask

搜尋可以安裝的軟體

假設我們想要尋找 Adobe 系列,可以輸入指令:
brew search adobe
Homebrew-Cask 就會列出所有可以安裝的 Adobe 應用程式。

安裝軟體

接著,安裝一般軟體就很簡單了,例如 Google 瀏覽器:
brew cask install google-chrome
網路上有許多教學特別註明,必須透過某些指令,去設定安裝軟體的路徑。筆者撰文時,測試了最新版 Homebrew-Cask 安裝軟體,跟安裝一般 Mac 軟體,預設的路徑就在「應用程式」資料夾,因此應該不需要額外修改。

更新軟體

列出需要更新的軟體
brew cask outdated

更新指定軟體

brew cask install --force google-chrome

一次找出需要更新的軟體並更新

brew cask install --force $(brew cask outdated | awk '{print $1}' | xargs) 

移除軟體

brew cask uninstall google-chrome

CD74HC4067 模組的使用教學

CD74HC4067 作用是通過選擇達成 1 對 16 組接腳信號的讀取與寫入。更詳細的說,根據晶片上 S0-S3 四個不同接腳的組合,讓 SIG 管腳和 C0-C15 導通。因此,最常見的用法是用來測試類比信號。比如,Arduino Uno 上面只有 6 個類比輸入,用一個 CD74HC4067 可以多擴展出來 16 個,於是可以支援 6+16-1=21 個類比接腳。













這個晶片的使用方法非常簡單,例如:S0-S3 分別是 0、0、0、0 時,SIG 就和 C0 是導通的。因此,依照個個原則,我們快速地寫出這段程式碼:

//-----Mux control pins
int s0=6;
int s1=5;
int s2=4;
int s3=3;

//-----Mux in "SIG" pin
int SIG_pin=0;
void setup()
{
        pinMode(s0,OUTPUT);
        pinMode(s1,OUTPUT);
        pinMode(s2,OUTPUT);
        pinMode(s3,OUTPUT);
        digitalWrite(s0,LOW);
        digitalWrite(s1,LOW);
        digitalWrite(s2,LOW);
        digitalWrite(s3,LOW);
        Serial.begin(9600);
}

void loop()
{
        int v;

        //-----Loop through and read all 16 values
        for(int i=0; i<16; i++)
        {
                Serial.print("Value at channel ");
                Serial.print(i);
                Serial.print(" is : ");
                v = readMux(i);
                Serial.println(v * 5.0 / 1024);
        }
        Serial.println(" ");
        delay(3000);
}

int readMux(int channel)
{
        int controlPin[] = {s0,s1,s2,s3};
        int muxChannel[16][4] =
        {
                {0, 0, 0, 0}, //-----channel 0
                {1, 0, 0, 0}, //-----channel 1
                {0, 1, 0, 0}, //-----channel 2
                {1, 1, 0, 0}, //-----channel 3
                {0, 0, 1, 0}, //-----channel 4
                {1, 0, 1, 0}, //-----channel 5
                {0, 1, 1, 0}, //-----channel 6
                {1, 1, 1, 0}, //-----channel 7
                {0, 0, 0, 1}, //-----channel 8
                {1, 0, 0, 1}, //-----channel 9
                {0, 1, 0, 1}, //-----channel 10
                {1, 1, 0, 1}, //-----channel 11
                {0, 0, 1, 1}, //-----channel 12
                {1, 0, 1, 1}, //-----channel 13
                {0, 1, 1, 1}, //-----channel 14
                {1, 1, 1, 1}  //-----channel 15
        };

        //-----loop through the 4 sig
        for(int i=0; i<4; i++)
        { 
                digitalWrite(controlPin[i], muxChannel[channel][i]);
        }

        //-----read the value at the SIG pin
        int val = analogRead(SIG_pin);

        //-----return the value
        return val;
}

發展過程中犯過的失誤或可以改進的部分

  • 可支援 digital 與 analog 的讀與寫,但必須接對接腳。例如:如果需要讀取類比訊號,那 SIG 接腳就必須要接到 A0~A5 等類比接腳。
  • 是否覺得這段程式太冗長了?
    int muxChannel[16][4] =
    {
            {0, 0, 0, 0}, //-----channel 0
            {1, 0, 0, 0}, //-----channel 1
            {0, 1, 0, 0}, //-----channel 2
            {1, 1, 0, 0}, //-----channel 3
            {0, 0, 1, 0}, //-----channel 4
            {1, 0, 1, 0}, //-----channel 5
            {0, 1, 1, 0}, //-----channel 6
            {1, 1, 1, 0}, //-----channel 7
            {0, 0, 0, 1}, //-----channel 8
            {1, 0, 0, 1}, //-----channel 9
            {0, 1, 0, 1}, //-----channel 10
            {1, 1, 0, 1}, //-----channel 11
            {0, 0, 1, 1}, //-----channel 12
            {1, 0, 1, 1}, //-----channel 13
            {0, 1, 1, 1}, //-----channel 14
            {1, 1, 1, 1}  //-----channel 15
     };
    
    //-----loop through the 4 sig
    for(int i=0; i<4; i++)
    { 
            digitalWrite(controlPin[i], muxChannel[channel][i]);
    }
    如果利用 bitRead() 我們就可以簡化程式碼。
    digitalWrite(s0,bitRead(c,0));
    digitalWrite(s1,bitRead(c,1));
    digitalWrite(s2,bitRea(c,2));
    digitalWrite(s3,bitRead(c,3));
    所以改寫後就成了這樣
    int channelSelect(int c)
    {
            //----- here is select what channel that we want to
            digitalWrite(s0,bitRead(c,0));
            digitalWrite(s1,bitRead(c,1));
            digitalWrite(s2,bitRead(c,2));
            digitalWrite(s3,bitRead(c,3));
        
            return c;
    }

2021年2月18日 星期四

How to Install and Use YouTube-DL on Ubuntu

YouTube-DL is a command line tool written in Python to help users download videos from YouTube, Dailymotion, Yahoo, Facebook, Flickr, PressTV and many other sites. YouTube-DL is a cross-platform piece of software, it runs on Windows, Linux/Unix, and macOS.

YouTube-DL support download from many sites in different formats, both as a video and audio files. By default, youtube-dl will pick the highest quality but you can obtain low quality if you have a slow internet connection by passing some options.

This tutorial will take you through how to install and use YouTube-DL on Ubuntu 18.04.

Other good features of youtube-dl include:
  • Resuming interrupted downloads
  • Extracting mp3 from video files
  • Downloading all video files from a playlist
  • Download only the videos uploaded in the last x days
  • Set Maximum download rate
  • Embed subtitle into the video while downloading
How to install youtube-dl on Ubuntu

There are three ways to install youtube-dl on Ubuntu 18.04 system. We will consider the installation of youtube-dl from all the three methods:

Install youtube-dl from apt

The first and easy method is installing youtube-dl from apt repository. For this, you just need to run the command:

$ sudo apt-get install youtube-dl

Install youtube-dl using pip

youtube-dl package can also be installed using pip. First, install packagepython-pip.

$ sudo apt-get install python-pip

Once pip is present on the system, use it to install youtube-dl:

$ sudo pip install youtube-dl

Install youtube-dl from binary

Youtube-dl is also distributed as a binary package which you can download and install it.

sudo wget https://yt-dl.org/latest/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+x /usr/local/bin/youtube-dl
hash -r

Any of the three methods should work fine for you.

Update YouTube-DL

You can always update youtube-dl to the latest release using the command below:

$ sudo youtube-dl -U
How to Use youtube-dl

Once you have installed youtube-dl , see below examples of how to download Videos and extract audio with youtube-dl.

Download highest quality video

To download the highest quality of a video from a URL, use the command:

$ youtube-dl example.com/watch?v=id

Check available video formats

YouTube-DL supports a multitude of formats, e.g Mp4, mkv, webm, FLV e.t.c. To list available video codes, use -F option. E.g

$ youtube-dl -F https://youtu.be/FLV1z9BWvyc
18 mp4 640x360 medium , avc1.42001E, mp4a.40.2@ 96k, 26.70MiB
43 webm 640x360 medium , vp8.0, vorbis@128k, 30.47MiB
22 mp4 1280x720 hd720 , avc1.64001F, mp4a.40.2@192k (best)

Take note of the format number - 18,43,22. This is used when downloading the video.

Download Specific video format

After getting a list of formats available, download specific format using -f format-number. E.g

$ youtube-dl -f 22 https://youtu.be/FLV1z9BWvyc

This will download format/codec1280x720 hd720.
Or …

$ youtube-dl https://www.youtube.com/watch?v=jNQXAC9IVRw -f mp4

And this will download mp4 format.

Download Audio

You can also download audio using youtube-dl like below:

youtube-dl --extract-audio --audio-format mp3 example.com/watch?v=id

This will extract audio from the video and save it to disk.

Download a video playlist

Youtube-dl saves all videos on a playlist by default. Just copy playlist URL and pass it to youtube-dl command line tool.

$ youtube-dl example.com/watch?v=id&list=listid

You can also start from a specified number.

$ youtube-dl --playlist-start 5 example.com/watch?v=id&list=listid

Force resume of partially downloaded files

To force resume of partially downloaded files without overriding completed, use -cwi options:

$ youtube-dl -cwi video-url

Use a proxy server to download files.

$ youtube-dl --proxy 127.0.0.1:3128

Download and embed subtitles to a video

Use the option --write-auto-sub to download Video subtitles if available.

$ youtube-dl --write-auto-sub <other-options> <url>
Install Youtube-dl GUI

If you are not a cli-centric person, you can as well install Youtube-dl GUI program for easy download of video files. youtube-dl-gui is a cross-platform front-end GUI of the popular youtube-dl written in wxPython.

sudo add-apt-repository ppa:nilarimogard/webupd8
sudo apt-get update && sudo apt-get install youtube-dlg

You should be able to launch the tool from cli or Search.