Download Youtube Video dengan Shell Script V2

Aplikasi yang dibuat mas Trio ini (You2Pup) ini adalah aplikasi di Puppy Linux untuk mendownload video dari Youtube. Beberapa hari lalu baru diupdate dengan menggunakan shell script yt-download.sh, padahal script itu lumayan cupu. Jadi ini yang ngebuat saya jadi iseng ngoprek-ngoprek lagi.

Sama seperti aplikasi pendownload youtube yang lain, script yt-download yang lama masih bisa dijalankan mulus. Tapi, bedanya, dia hanya mendownload salah satu video saja (defaultnya), sementara di Youtube, satu video ada berbagai macam ukuran dan format. Ide dari mas Trio, di script yang baru, saya coba tambahkan beberapa fitur.

Shell ini masih sederhana seperti yang lama, hanya jauh lebih banyak barisnya. Ga jauh beda, cuma memanfaatkan fungsi-fungsi regular expression dan string manipulation untuk mendapatkan url video, format, dan ukuran. Variable tersebut ada di source html youtube dengan key “PLAYER_CONFIG” yang berupa array JSON. Dengan mendapatkan nilai “fmt_url_map” dan “fmt_map” sudah cukup menghasilkan url video, format, dan ukurannya. Kalo shell bisa ngebaca JSON, mungkin akan lebih mudah untuk parsingnya.

Script yt-download.sh bisa didownload di sini.

Change log

v2.35 – 2013-04-21

  • Fixed: Change from playerConfig to ytplayer.config.

v2.34 – 2013-01-24

  • Fixed: As always, reported by Abdulla, doraq. Some changes in ${UrlMap} loop.

v2.33 – 2012-11-08

  • Fixed: Youtube’s format changed, reported by ambassador. Reformat “signature”, line 78-83.

v2.32 – 2012-01-31

  • Fixed: Youtube’s format changed, reported by Abdulla. PLAYER_CONFIG to playerConfig, line 54-55.
  • Changed: Added tmp file with timestamp, and deleted as soon as it is processed, to prevent write problems.

v2.3 – 2011-08-29

  • Fixed: Youtube’s format changed. url_encoded_fmt_stream_map & fmt_list.
  • Fixed: Youtube’s url changed. Url decoded, remove some parameters, added ‘title’ parameter.

v2.2

  • Fixed: Youtube’s url changed. Remove o-o.preferred.matrix-cgk1
  • Fixed: Youtube’s url changed. \u0026 replaced with &.
  • Fixed: array sorting problem.

v2.1

  • Added: input for save directory, and some error handling if directory’s not found. (Thanks to Trio).
  • Added: abort option. (Thanks to Trio).
  • Added: wget parameter. (Thanks to Trio).
  • Added: new video format webm. Format list: flv, mp4, 3gp, and webm.
  • Changed: parsing algorithm based on Dougal’s Shell script. (Thanks to Dougal). But problem with array sorting.

v2.0

  • Added: automatic file naming based on page title.
  • Added: some error handling.
  • Added: allow user to choose available size and format. Format list: flv, mp4, and 3gp.
  • Changed: parsed portion. JSON array from youtube HTML source provides various video format. (Thanks to Trio).
  • Reported: problem with title and “/” character. Tested with Debian/Ubuntu & Puppy, no problem.
Share!
    This entry was posted in Free/Open Source, Iseng, Programming, Tutorial and tagged , , , , , , , . Bookmark the permalink.

    28 Responses to Download Youtube Video dengan Shell Script V2

    1. Abdulla says:

      the script need to be update again with youtube new rules

    2. abdulla says:

      the script cant download mp4 anymore it is returen forbidden message also flv some times working some times not

      • doraq says:

        merge 2.33 script with following part, youtube format changed, again.

        for i in ${UrlMap} ; do
        Fmt=${i#*itag=}
        Fmt=${Fmt%%\\u0026*}

        Sig=${i#*sig=}
        Sig=${Sig%%\\u0026*}

        Url=${i#*url=}
        Url=”$(echo $Url | echo -e $(sed ‘s/itag=/xtag=/g;’))” # double itag=
        Url=”$(echo $Url | echo -e $(sed ‘s/+//g; s/%/\\x/g’))”
        Url=${Url//\\\///}
        Url=${Url//\\\u0026/&}
        Url=${Url%%&sig=*}
        Url=”$(echo $Url | echo -e $(sed ‘s/;codecs=\”.*\”//g’))”
        Url=${Url// /}
        Url=${Url//\%252C/,}
        #Url=”$(echo $Url | sed -e s/\\http:[^\]]*\\.v/http:\\/\\/v/)”

        Url=$Url”&signature=”$Sig”&title=”$titlefn

        Url_array[$j]=$Url
        Fmt_array[$j]=$Fmt
        let j++
        done

      • abdulla says:

        I tried to replace it but i get error message could you upload it on some url or inform arif to update the new changes, thank you

      • Arif says:

        @doraq, @abdulla : Fixed v2.34 –for now… Sorry for the late reply, been busy lately.
        Thanks for the report. :D

    3. abbassador says:

      dont work with all vid id

      ./y jNQXAC9IVRw

      YT-DOWNLOAD.SH v2.32
      ———————————-
      Downloading HTML file and getting video formats, please wait…

      Download site(s) found!
      ———————————-
      Title: Me at the zoo
      ———————————-
      [0] 176×144 flv
      [1] 320×240 flv
      [2] 320×240 flv
      [3] 320×240 flv
      [4] 320×240 flv
      Select video format and press enter or just press enter to abort: 2
      Downloading video…
      ———————————-
      –2012-11-03 16:51:10– http://v2.cache8.c.youtube.com&sig=834565bc4402092f5ff13cee831d69073c53dbc6.3ca143340fbc1160357d209edcb038e30ddd2765&title=me%20at%20the%20zoo/
      Auflösen des Hostnamen v2.cache8.c.youtube.com&sig=834565bc4402092f5ff13cee831d69073c53dbc6.3ca143340fbc1160357d209edcb038e30ddd2765&title=me at the zoo… fehlgeschlagen: Der Name oder der Dienst ist nicht bekannt.
      wget: kann die Host-Adresse »v2.cache8.c.youtube.com&sig=834565bc4402092f5ff13cee831d69073c53dbc6.3ca143340fbc1160357d209edcb038e30ddd2765&title=me at the zoo« nicht auflösen
      ———————————-

    4. ambassador says:

      youtube changed the format & url again, need fix

      • Arif says:

        I just tested it, works fine.
        Please submit your video id, if still having problems.

        arif@ubuntu:~$ bash yt-download.sh jNQXAC9IVRw

        YT-DOWNLOAD.SH v2.32
        ———————————-
        Downloading HTML file and getting video formats, please wait…

        Download site(s) found!
        ———————————-
        Title: Me at the zoo
        ———————————-
        [0] 320×240 flv
        [1] 320×240 mp4
        [2] 320×240 flv
        [3] 320×240 webm
        Select video format and press enter or just press enter to abort: 0
        Downloading video…
        ———————————-
        –2012-05-16 21:23:56– http://v22.lscache6.c.youtube.com/videoplayback?upn=pBkdfDmjAmM&

        Saving to: `STDOUT’

        100%[===================================================================================================================>] 711,654 81.4K/s in 8.8s

        2012-05-16 21:24:06 (79.3 KB/s) – written to stdout [711654/711654]

        ———————————-

    5. abdulla says:

      Hi Arif the bash file no more working with youtube since 14 Jan i think it is need to be updated with the new changes in youtube

      • Arif says:

        Hi Abdulla, thanks for reporting the problem. I’ll fix this ASAP.
        [update] A litle fix line 54-55. I did a quick test with Ubuntu, should be ok now.

    6. sriviswajeet says:

      Getting error :
      yt-download.sh: line 105: {: syntax error: operand expected (error token is “{“)

      Code:

      99 echo “———————————-”
      100 # List all the elements in the array.
      101 arr_count=${#Geom_array[@]}
      102 i=0
      103 while [ "$i" -lt "$arr_count" ]
      104 do
      105 echo [$i] ${Geom_array[$i]} ${fmt[${Fmt_array[$i]}]} ${Fmt_array[$i]}
      106 let i++
      107 done

      Please check it

    7. Wah mas thx buat scriptnya :) Dari dulu pengguna keepvid nih, soalnya bisa dpt mp4 :D Sekarang berkat scriptnya sampean, jadi bisa download format mp4 lagi dr youtube :) Nih juga lagi dibaca2x scriptnya, pusing di regex :P

      Makasih buat upload scriptnya ya mas, ane pingin coba porting trs dibuat online supaya bisa dipake juga buat yg ga pake Linux :D

    8. trio says:

      Mas Arif,

      Ada laporan, youtube ganti lagi ….. Weleh weleh

      • Arif says:

        Iya mas. di urlnya di tambahin o-o.preferred.matrix-cg1… cape deh.
        Ntar difix deh.

        Update: dah difix mas. Line 70. Dah ditest, sementara gitu dulu. Coba ditest tempat mas.
        gw jg ga tau ngapain juga dia tambahin ‘o-o.preferred.matrix-cg1′

    9. trio says:

      Yup maz,

      you2pup updated, thanks

    10. Arif says:

      Iya tuh mas… Coba gw liat2 dulu yah. Kayaknya perubahannya sedikit doang nih….
      Update: sudah di fix & ditest dipuppy juga. Tinggal nambahin Url=${Url//\\\u0026/&}
      http://arif.suparlan.com/demo/yt-download/yt-download.sh.txt

    11. trio says:

      Mas,

      Kayaknya youtube ganti url download lagi … coba deh … it stopped working

    12. Arif says:

      Hello Dougal, sorry takes weeks to reply your comments hahaha…, thats because I’m quite busy lately. I have seen your shell codes here murga-linux.com, it’s amazing far more efficient, simple than mine. I did not see it can be parsed like that. Ok, I’m quite a noob in linux and shell scripting, there’s so much to learn. So I’m going to use yours, if you don’t mind. Thanks. :D

      Mas trio: gw udah cobain petnya di puppy. Testing saya biasa di debian/ubuntu, selalu berhasil, blm ketemu error. Apa beda di puppy? Nanti gw coba ubah scriptnya. :D . Yang pasti “rm wget-log*” dan “curl -s” bikin eror di ubuntu (default install).

    13. trio says:

      sementara ini sy modified hanya:

      1. DL html pake curl -s, jadi silent (agar user ndak bingung, dikira sdh start DL)
      2. Ada pilihan utk lokasi folder Dl
      3. Auto-naming DL file yg anda ndak berhasil (selalu file namenya 0 (mis: 0.360×780.flv), jadi sy gunakan grep utk dapat judulnya sbg nama file

      Trims

    14. Dougal says:

      …and your title finding code is broken (the blog swallowed it?):
      grep -o “” $1.tmp

    15. Dougal says:

      fmt 34/35 should probably get an extension of f4v:
      http://en.wikipedia.org/wiki/Flash_Video
      hat makes it easier to know if it’s h264 or the old (crap) Sorenson flash.

    16. Arif says:

      Dan seharusnya bs diefisienkan. Regex dan arraynya jg agak2 gmn gitu. Regex expr blm bs kalo yg non-greedy. :P

    17. Arif says:

      Wah cepet jg :D sep-sep…
      Coba2in ya mas, terutama di format listnya tuh.

    18. trio says:

      Modified and now used — Thanks!!!

    Leave a Reply

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

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>