Youtube Auto Download Script

My daughter is tracking some movie episodes in Youtube, but download speed is rather slow on peak hours.

So I made the following script, which, when scheduled with crontab in Linux, will automatically download the episodes automatically in off peak hours. It’s much faster this way, and she’s happy now 🙂

The script is also heavily commented, it covers most of the basic features in shell / Unix / bash scripting. I figure those still new in Unix scripting will find it useful, so I’m sharing it here.

Back to the script — It’s very easy in her part – (1) she just need to paste the Youtube URLs into /home/myuser/Desktop/download-list.txt
(2) And every morning she need to delete the URLs, which have been downloaded, from it — unfortunately, this is necessary since Youtube doesn’t allow us to resume downloads.
But basically that’s all that she needs to do.

Here’s how the crontab looks like :

58 23 * * * /master/scripts/download-otomatis.sh
1 6 * * * pkill -f download-otomatis
3 6 * * * killall wget

The script will be executed every day at 23:58.
Then to kill the script, we’ll need the next 2 lines, which will be executed at 06:01 and 06:03 respectively.

Voila – a happier daughter 🙂

Here’s the script :

#!/bin/bash

### Youtube Auto Download
### v1.03

### Requires : youtube-dl script
### http://www.arrakis.es/~rggi3/youtube-dl/

### -------------------------- Global variables
### change as necessary
YOUTUBE_SCRIPT=/master/scripts/youtube-dl
DL_FILE=/home/myuser/Desktop/download-list.txt
DL_LOC=/master/NewDownloads/
### ------------------------- stop changing here

## ganti direktori
cd $DL_LOC

## cari tahu jumlah baris
JML_BARIS=`cat $DL_FILE | wc -l`

CTR=1

## jika belum akhir file, proses setiap baris pada $DL_FILE
while [ $JML_BARIS -ge $CTR ]
do

### ambil satu baris dari file
URL_YOUTUBE=`cat $DL_FILE | sed $CTR'q;d'`
echo $URL_YOUTUBE


### dapatkan link untuk download
URL_DL2=`$YOUTUBE_SCRIPT -g2 "$URL_YOUTUBE"`

### baris pertama adalah nama file (lalu tambahkan extension FLV)
NAMA_FILE=`echo "$URL_DL2" | sed 1'q;d'`
NAMA_FILE=$DL_LOC${NAMA_FILE}.FLV
### baris kedua adalah URL_DL
URL_DL=`echo "$URL_DL2" | sed 2'q;d'`

### cek apakah $URL_DL kosong?
## jika ya, maka berarti ada error (url download tidak berhasil didapatkan)
if [ -z "$URL_DL" ]; then
echo ==== ada error ! ====
else

## OK !! download videonya sekarang !
wget -O "$NAMA_FILE" -c "$URL_DL"

fi

CTR=$((CTR+1))

done

exit 0

52 thoughts on “Youtube Auto Download Script

  1. cool!
    dimana ya cari binary wget untuk mac os x tiger (10.4.11)?

  2. @azmee – bisa coba install fink.
    .
    Setelah fink terpasang, buka terminal, lalu ketik apt-get install wget
    .
    Enak banget dengan fink, he he 🙂
    .
    @alva – thanks, maklum saya orangnya rada pemalas, he he.
    Maunya serba otomatis saja 😉

  3. Mas Harry,
    Aku ga nemuin dimana harusnya aku properly contact anda (maksud saya, saya ga temukan email anda). Jadinya nyasar lewat sini. Siapa tahu sampai ke anda juga.

    Ok nama saya Nalendra Wibowo, aku baru start new bisnis, namanya DCDA-Consulting Services (Data Center Design and Assessment Services), jadi purely lebih di physical infrastructure sidenya, bukan s/w atau aplikasi, who knows, suatu kali ur current or prospect client need my services.

    Saya di bintaro juga, bintaro ngelit… anyway, please do response to me at nalendra.wibowo@gmail.com

    Salam

  4. Selamat atas terbentuknya AOSI dan mas harry sebagai .. tim formaturnya … 🙂

    Semoga Opensource di INdonesia terus berjaya dan menggeser Mi kocok 😀

  5. @tintin – trims mas 🙂
    .
    Tujuannya sih bukan untuk menggeser Mi kocok lho 😉 sayang kalau inisiatif sebesar ini cuma untuk tujuan sekecil itu, he he.
    .
    Harapan kita adalah kita bisa menjadikan Indonesia sebagai negara maju di bidang IT dengan memanfaatkan berbagai teknologi open source.
    Maju secara teknologi dan dengan dampak ke bidang ekonominya juga (contoh: jadi pemasukan devisa dari luar negeri).
    .
    Mohon doa restunya. Terimakasih.

  6. It is so lucky to read your blog,it is full of useful message.I wish we both can do better in the future.It great honour if you can visit our website,and give us some suggession.

  7. Hello sufehmi.com administrator, You always provide great examples and real-world applications, thank you for your valuable contributions.

Leave a Reply

Your email address will not be published. Required fields are marked *